From 73f77d58f8e8df98f679a5c067b6f298bb35a558 Mon Sep 17 00:00:00 2001 From: "p.belezov" Date: Thu, 9 Jan 2025 17:08:44 +0800 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=BE?= =?UTF-8?q?=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D1=83=D1=8E=20=D1=87=D0=B0=D1=81?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B1=D1=80=D0=BE=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/WishesController.php | 15 +++++ app/Models/Wish.php | 2 +- resources/store/wish.js | 20 ++++++ resources/views/Auth/Login.vue | 4 +- resources/views/Public.vue | 26 +++++++- .../views/PublicWishlist/ShowWhishlist.vue | 64 +++++++++++++++---- routes/api.php | 1 + 7 files changed, 116 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/WishesController.php b/app/Http/Controllers/WishesController.php index 2a5635f..f7a0828 100644 --- a/app/Http/Controllers/WishesController.php +++ b/app/Http/Controllers/WishesController.php @@ -84,4 +84,19 @@ class WishesController extends Controller Wish::destroy($request->get('id')); return response()->json(null, 204); } + + public function book(Request $request) + { + $request->validate([ + 'id' => 'required|exists:wishes,id', + 'user_id' => 'required|exists:users,id' + ]); + $wish = Wish::find($request->get('id')); + if (isset($wish->book_user_id)){ + return response()->json(["error" => 'Уже забронировано' ], 409); + } + $wish->book_user_id = $request->get('user_id'); + $wish->save(); + return response()->json($wish, 200); + } } diff --git a/app/Models/Wish.php b/app/Models/Wish.php index 6f2b42d..b4e20d4 100644 --- a/app/Models/Wish.php +++ b/app/Models/Wish.php @@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model; class Wish extends Model { use HasFactory; - protected $fillable = ['user_id', 'name', 'price', 'url']; + protected $fillable = ['user_id', 'name', 'price', 'url', 'book_user_id']; public function user(){ return $this->belongsTo(User::class); diff --git a/resources/store/wish.js b/resources/store/wish.js index a5ea493..5fcbabe 100644 --- a/resources/store/wish.js +++ b/resources/store/wish.js @@ -84,5 +84,25 @@ export const useWishStore = defineStore('wish', { }); return result; }, + async book(id, user_id, token){ + let result = null; + await axios.post(`/api/wish/book`, + { + id: id, + user_id: user_id + }, + { + headers: { + Authorization: `Bearer ${token}`, + token: token + }, + } + ).then((response)=>{ + result = response; + }).catch((error)=>{ + result = error; + }); + return result; + } }, }) diff --git a/resources/views/Auth/Login.vue b/resources/views/Auth/Login.vue index 59018e9..d234192 100644 --- a/resources/views/Auth/Login.vue +++ b/resources/views/Auth/Login.vue @@ -47,7 +47,9 @@ export default { if (isLogged){ this.errorMessage = ''; this.errorMessageContainerStyle = 'display: none;'; - this.$router.push('/'); + // this.$router.push('/'); + // console.log(window.location.href); + // window.location.replace(window.location.href); } else { this.errorMessage = 'Authentication error'; this.errorMessageContainerStyle = ''; diff --git a/resources/views/Public.vue b/resources/views/Public.vue index d6d11e5..b195dc6 100644 --- a/resources/views/Public.vue +++ b/resources/views/Public.vue @@ -13,11 +13,25 @@
Вход/Регистрация
- + + + + Вход + Регистрация + + + + + + + + + + @@ -33,18 +47,24 @@ import ShowWhishlist from "./PublicWishlist/ShowWhishlist.vue"; import {useUserStore} from "../store/user.js"; import { watch } from "vue"; +import Login from "./Auth/Login.vue"; +import Registration from "./Auth/Registration.vue"; export default { name: "Public", - components: {ShowWhishlist}, + components: {Registration, Login, ShowWhishlist}, data: ()=>({ isAuthenticated: false, isWide: window.innerWidth >= 800, userStore: useUserStore(), - showAuthDialog: false + showAuthDialog: false, + tab: null }), mounted() { watch(this.userStore, (newStore, oldStore)=>{ this.isAuthenticated = newStore.user !== null && newStore.user !== undefined; + if (this.isAuthenticated){ + this.showAuthDialog = false; + } }); useUserStore().checkUser(); }, diff --git a/resources/views/PublicWishlist/ShowWhishlist.vue b/resources/views/PublicWishlist/ShowWhishlist.vue index fb50275..a6691ac 100644 --- a/resources/views/PublicWishlist/ShowWhishlist.vue +++ b/resources/views/PublicWishlist/ShowWhishlist.vue @@ -4,10 +4,13 @@ import { useUserStore } from "../../store/user.js"; import DeleteWish from "../Wishlist/DeleteWish.vue"; import CreateWish from "../Wishlist/CreateWish.vue"; import EditWish from "../Wishlist/EditWish.vue"; +import {watch} from "vue"; export default { name: "ShowWhishlist", components: {EditWish, CreateWish, DeleteWish}, data: () => ({ + isAuthenticated: false, + isSameUser: false, wishes: [], wishStore: useWishStore(), userStore: useUserStore(), @@ -16,7 +19,10 @@ export default { username: '', bookConfirmationDialog: false, bookItemId: '', - bookItemName: '' + bookItemName: '', + bookError: false, + bookErrorText: '', + bookLoading: false }), methods: { bookDialog(id, name){ @@ -26,14 +32,41 @@ export default { }, closeBookDialog(){ this.bookConfirmationDialog = false; + }, + book(){ + this.bookLoading = true; + this.wishStore.book(this.bookItemId, this.userStore.user["id"], this.userStore.token).then((response)=>{ + this.bookError = false; + if (response.request.status == 409){ + this.bookErrorText = response.response.data.error; + this.bookError = true; + } else { + this.bookConfirmationDialog = false; + this.fetching = true; + let urlArray = window.location.href.split('/'); + let user_id = urlArray[urlArray.length - 1]; + this.wishStore.getUserWishes(user_id).then((res)=>{ + this.wishes = res; + this.fetching = false; + }); + } + this.bookLoading = false; + }).catch(()=>{ + this.bookLoading = false; + }); } }, mounted() { let urlArray = window.location.href.split('/'); let user_id = urlArray[urlArray.length - 1]; + watch(this.userStore, (newStore, oldStore)=>{ + this.isAuthenticated = newStore.user !== null && newStore.user !== undefined; + if (newStore.user !== null && newStore.user !== undefined){ + this.isSameUser = user_id == this.userStore.user['id']; + } + }); this.fetching = true; this.userStore.getUsername(user_id).then((res)=>{ - console.log(res); this.username = res.data.username; this.wishStore.getUserWishes(user_id).then((responce)=>{ this.wishes = responce; @@ -64,19 +97,26 @@ export default { {{ wish['price'] }} {{ wish['url'] }} - Забронировать + + Забронировать + Нет + - - Хотите забронировать {{ bookItemName }}? -
- Да - Нет +
+ Хотите забронировать {{ bookItemName }}? +
+ Да + Нет +
+
+
+
@@ -91,18 +131,20 @@ export default {
Цена: {{ wish['price'] }}₽ Забронировано:  - Нет + + Забронировать + Нет + Да
- Хотите забронировать {{ bookItemName }}?
- Да + Да Нет
diff --git a/routes/api.php b/routes/api.php index 18556e0..95e146d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -38,5 +38,6 @@ Route::group(['prefix' => 'wish'], function () { Route::post('update', [WishesController::class, 'update']); Route::post('destroy', [WishesController::class, 'destroy']); Route::get('by_id/{id}', [WishesController::class, 'getWishById']); + Route::post('book', [WishesController::class, 'book']); }); });