From fc491543c2f0183fc9db51b30b21cd700a5d0e47 Mon Sep 17 00:00:00 2001 From: merdan Date: Sun, 12 Mar 2023 12:56:05 +0500 Subject: [PATCH] Folders --- .../Sarga/API/Http/Controllers/Wishlists.php | 56 ++++++++++++++++--- .../Resources/Customer/FolderResource.php | 3 +- packages/Sarga/API/Http/routes.php | 1 + 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/Sarga/API/Http/Controllers/Wishlists.php b/packages/Sarga/API/Http/Controllers/Wishlists.php index 34cf04c54..bbaadcb64 100644 --- a/packages/Sarga/API/Http/Controllers/Wishlists.php +++ b/packages/Sarga/API/Http/Controllers/Wishlists.php @@ -3,6 +3,7 @@ namespace Sarga\API\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; use Sarga\API\Http\Requests\FolderRequest; use Sarga\API\Http\Resources\Checkout\CartResource; use Sarga\API\Http\Resources\Customer\FolderResource; @@ -26,8 +27,11 @@ class Wishlists extends WishlistController { $customer = $request->user(); + $wishlist = $request->has('folder') ? $customer->wishlist_items()->where('folder_id',$request->folder) + :$customer->wishlist_items(); + return response([ - 'data' => WishListResource::collection($customer->wishlist_items()->paginate(10)), + 'data' => WishListResource::collection($wishlist->paginate(10)), ]); } @@ -64,7 +68,7 @@ class Wishlists extends WishlistController 'product_id' => $id, 'customer_id' => $customer->id, 'price' => $product->min_price ?? $product->price, - 'folder_id' => $request->folder_id +// 'folder_id' => $request->folder_id ]); return response([ @@ -114,17 +118,52 @@ class Wishlists extends WishlistController ], 400); } + public function moveToFolder(Request $request, $folder_id){ + $customer = $request->user(); + + if($this->wishlistRepository->where('customer_id',$customer->id) + ->whereIn('id',$request->wishlist) + ->update(['folder_id' => $folder_id])){ + +// $folder = app(FolderRepository::class)->with('wishlist')->find($folder_id); + + return response([ +// 'data' => FolderResource::make($folder), + 'message' => __('sarga-api::app.wishlist.success-add'), + ]); + } + + return response([ +// 'data' => FolderResource::make($folder), + 'message' => __('sarga-api::app.wishlist.success-add'), + ]); + + } + public function folders(FolderRepository $folderRepository){ $customer = request()->user(); - return FolderResource::collection($folderRepository->findWhere(['customer_id' => $customer->id])); + + $folders = $folderRepository->where('customer_id', $customer->id) + ->with(['wishlist'=> function($q){ + $q->take(4); + }]) + ->withCount('wishlist') + ->get(); + + return FolderResource::collection($folders); } public function getFolder($id) { - if($folder = app(FolderRepository::class)->with('wishlist')->find($id)) - { - return FolderResource::make($folder); + if($wishlist = $this->wishlistRepository->findWhere([ + 'channel_id' => core()->getCurrentChannel()->id, + 'folder_id' => $id + ])->paginate(10)){ + return response([ + 'data' => WishListResource::collection($wishlist), + ]); } + return response(['message' => 'Folder not found'],404); } @@ -133,7 +172,10 @@ class Wishlists extends WishlistController $folderRepo = app(FolderRepository::class); if($folder = $folderRepo->create(['name' => $request->name, 'customer_id' => $request->user()->id])){ - return FolderResource::make($folder); + return response([ + 'message' => 'Successfully created folder', + 'data'=>FolderResource::make($folder) + ],200); } return response(['message' => 'Folder creation failed'],400); diff --git a/packages/Sarga/API/Http/Resources/Customer/FolderResource.php b/packages/Sarga/API/Http/Resources/Customer/FolderResource.php index 4db38dcf7..45866e6cd 100644 --- a/packages/Sarga/API/Http/Resources/Customer/FolderResource.php +++ b/packages/Sarga/API/Http/Resources/Customer/FolderResource.php @@ -18,7 +18,8 @@ class FolderResource extends JsonResource return [ 'id' => $this->id, 'name' => $this->name, - 'items' => WishlistResource::collection($this->wishlist) + 'items' => WishlistResource::collection($this->wishlist), + 'items_count' => $this->wishlist_count ?? 0 ]; } diff --git a/packages/Sarga/API/Http/routes.php b/packages/Sarga/API/Http/routes.php index 6701ff372..0f9ac1b09 100644 --- a/packages/Sarga/API/Http/routes.php +++ b/packages/Sarga/API/Http/routes.php @@ -91,6 +91,7 @@ Route::group(['prefix' => 'api'], function () { Route::get('wishlist', [Wishlists::class, 'index']); Route::post('wishlist/{id}', [Wishlists::class, 'addOrRemove']); Route::post('wishlist/{id}/move-to-cart', [Wishlists::class, 'moveToCart']); + Route::post('folder/{id}/move-to-folder', [Wishlists::class, 'moveTofolder']); Route::get('folders',[Wishlists::class, 'folders']); Route::post('folder',[Wishlists::class, 'createFolder']);