From 4455613180df7a64eff18d9ce769ab0d0d23e129 Mon Sep 17 00:00:00 2001 From: prashant-webkul Date: Sat, 24 Nov 2018 15:26:31 +0530 Subject: [PATCH] Customer Wishlist API implemented --- .../Customer/AddressController.php | 9 ++- .../Controllers/Customer/AuthController.php | 13 ++-- .../Customer/CustomerController.php | 18 ++--- .../Customer/WishlistController.php | 66 ++++++++++++++++++- .../Http/Controllers/Shop/CartController.php | 17 ++++- packages/Webkul/API/Http/api.php | 22 +++++-- .../Http/Controllers/WishlistController.php | 6 ++ 7 files changed, 121 insertions(+), 30 deletions(-) diff --git a/packages/Webkul/API/Http/Controllers/Customer/AddressController.php b/packages/Webkul/API/Http/Controllers/Customer/AddressController.php index 850ad197a..633ce6dec 100644 --- a/packages/Webkul/API/Http/Controllers/Customer/AddressController.php +++ b/packages/Webkul/API/Http/Controllers/Customer/AddressController.php @@ -24,17 +24,22 @@ class AddressController extends Controller public function __construct(CustomerAddress $customerAddress) { - $this->middleware('customer'); + $this->middleware('auth:customer'); $this->customerAddress = $customerAddress; if(auth()->guard('customer')->check()) { $this->customer = auth()->guard('customer')->user(); } else { - $this->customer = false; + $this->customer['message'] = 'unauthorized'; + $this->unAuthorized(); } } + public function unAuthorized() { + return response()->json($this->customer, 401); + } + /** * To get the details of user to display on profile * diff --git a/packages/Webkul/API/Http/Controllers/Customer/AuthController.php b/packages/Webkul/API/Http/Controllers/Customer/AuthController.php index a22c61aaf..d0b1976b0 100644 --- a/packages/Webkul/API/Http/Controllers/Customer/AuthController.php +++ b/packages/Webkul/API/Http/Controllers/Customer/AuthController.php @@ -28,20 +28,17 @@ class AuthController extends Controller if(!auth()->guard('customer')->check()) { if(!auth()->guard('customer')->attempt($data)) { - return response()->json(['unauthenticated', 'invalid creds'], 401); + return response()->json(['message' => 'unauthenticated', 'details' => 'invalid creds'], 401); } else { - return response()->json(['authenticated'], 200); + return response()->json(['message' => 'authenticated', 'user' => auth()->guard('customer')->user()], 200); } } else { - return response()->json(['already authenticated'], 200); + return response()->json(['message' => 'already authenticated'], 200); } } public function destroy() { - if(auth()->guard('customer')->logout()) { - return response()->json(['logged out'], 200); - } else { - return response()->json(['already logged out'], 200); - } + auth()->guard('customer')->logout(); + return response()->json(['message' => 'logged out'], 200); } } \ No newline at end of file diff --git a/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php b/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php index ab43cfe28..01b6b0656 100644 --- a/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php +++ b/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php @@ -26,23 +26,23 @@ class CustomerController extends Controller if(auth()->guard('customer')->check()) { $this->customer = auth()->guard('customer')->user(); } else { - $this->customer = 'unauthorized'; + $this->customer['message'] = 'unauthorized'; + $this->unAuthorized(); } } + public function unAuthorized() { + return response()->json($this->customer, 401); + } + /** * To get the details of user to display on profile + * Only accepts the id of simple or configurable product * * @return response JSON */ public function getProfile() { - if($this->customer == 'unauthorized') { - return response()->json($this->customer, 401); - } else { - $customer = auth()->guard('customer')->user(); - } - - return response()->json($customer, 200); + return response()->json($this->customer, 200); } public function updateProfile($id) { @@ -73,6 +73,8 @@ class CustomerController extends Controller } } + dd($data); + $result = $this->customer->update($data); if($result) { diff --git a/packages/Webkul/API/Http/Controllers/Customer/WishlistController.php b/packages/Webkul/API/Http/Controllers/Customer/WishlistController.php index 5848b051f..a74189487 100644 --- a/packages/Webkul/API/Http/Controllers/Customer/WishlistController.php +++ b/packages/Webkul/API/Http/Controllers/Customer/WishlistController.php @@ -5,6 +5,8 @@ namespace Webkul\API\Http\Controllers\Customer; use Webkul\API\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Webkul\Product\Repositories\ProductRepository as Product; +use Webkul\Customer\Repositories\WishlistRepository as Wishlist; use Auth; use Cart; @@ -17,16 +19,25 @@ use Cart; class WishlistController extends Controller { protected $customer; + protected $product; + protected $wishlist; - public function __construct() + public function __construct(Product $product, Wishlist $wishlist) { if(auth()->guard('customer')->check()) { + $this->product = $product; + $this->wishlist = $wishlist; $this->customer = auth()->guard('customer')->user(); } else { - $this->customer = false; + $this->customer['message'] = 'unauthorized'; + $this->unAuthorized(); } } + public function unAuthorized() { + return response()->json($this->customer, 401); + } + public function getWishlist() { if($this->customer == false) { @@ -37,8 +48,57 @@ class WishlistController extends Controller if($wishlist->count() > 0) { return response()->json($wishlist, 200); } else { - return response()->json('Wishlist Empty', 200); + return response()->json(['message' => 'Wishlist Empty', 'Items' => null], 200); } } } + + /** + * Function to add item to the wishlist. + * Only accepts the id of simple or configurable product + * + * @param integer $productId + */ + public function add($productId) + { + $product = $this->product->findOneByField('id', $productId); + + $data = [ + 'channel_id' => core()->getCurrentChannel()->id, + 'product_id' => $productId, + 'customer_id' => auth()->guard('customer')->user()->id + ]; + + //accidental case if some one adds id of the product in the anchor tag amd gives id of a variant. + if($product->parent_id != null) { + $data['product_id'] = $productId = $product->parent_id; + } + + $checked = $this->wishlist->findWhere(['channel_id' => core()->getCurrentChannel()->id, 'product_id' => $productId, 'customer_id' => auth()->guard('customer')->user()->id]); + + if($checked->isEmpty()) { + if($wishlistItem = $this->wishlist->create($data)) { + return response()->json(['message' => 'Successfully Added Item To Wishlist', 'items' => $wishlistItem], 200); + } else { + return response()->json(['message' => 'Error! Cannot Add Item To Wishlist', 'items' => null], 401); + } + } else { + return response()->json(['message' => trans('customer::app.wishlist.already'), 'items' => null], 200); + } + } + + /** + * Function to remove item to the wishlist. + * + * @param integer $itemId + */ + public function delete($itemId) { + $result = $this->wishlist->deleteWhere(['customer_id' => auth()->guard('customer')->user()->id, 'channel_id' => core()->getCurrentChannel()->id, 'id' => $itemId]); + + if($result) { + return response()->json(['message' => 'Item Successfully Removed From Wishlist', 'status' => $result]); + } else { + return response()->json(['message' => 'Error! While Removing Item From Wishlist', 'status' => $result]); + } + } } \ No newline at end of file diff --git a/packages/Webkul/API/Http/Controllers/Shop/CartController.php b/packages/Webkul/API/Http/Controllers/Shop/CartController.php index 47b975ce7..b27298b33 100644 --- a/packages/Webkul/API/Http/Controllers/Shop/CartController.php +++ b/packages/Webkul/API/Http/Controllers/Shop/CartController.php @@ -6,9 +6,8 @@ use Webkul\API\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Event; -use Auth; -// use Cart; use Webkul\Checkout\Repositories\CartRepository; +use Auth; /** * Cart controller for the APIs of User Cart @@ -29,10 +28,15 @@ class CartController extends Controller if(auth()->guard('customer')->check()) { $this->customer = auth()->guard('customer')->user(); } else { - return response()->json('Unauthorized', 401); + $this->customer['message'] = 'unauthorized'; + $this->unAuthorized(); } } + public function unAuthorized() { + return response()->json($this->customer, 401); + } + public function getAllCart() { $carts = $this->customer->carts; @@ -46,4 +50,11 @@ class CartController extends Controller public function getActiveCart() { return $this->customer->cart; } + + /** + * Add a new item in the cart + */ + public function add($id) { + dd('add to cart', $id); + } } \ No newline at end of file diff --git a/packages/Webkul/API/Http/api.php b/packages/Webkul/API/Http/api.php index 545e6d9fa..dcf4af342 100644 --- a/packages/Webkul/API/Http/api.php +++ b/packages/Webkul/API/Http/api.php @@ -1,6 +1,6 @@ 'Webkul\API\Http\Controllers\Customer', 'prefix' => 'api/customer'], function ($router) { +Route::group(['middleware' => 'api','namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => 'api/customer'], function ($router) { Route::post('/register', 'RegistrationController@create'); }); @@ -16,7 +16,14 @@ Route::group(['namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => Route::put('update/profile/{id}', 'CustomerController@updateProfile'); //wishlist + //get wishlist Route::get('get/wishlist', 'WishlistController@getWishlist'); + //add the item in the wishlist + Route::get('add/wishlist/{id}', 'WishlistController@add'); + //delete the item from the wishlist + Route::get('delete/wishlist/{id}', 'WishlistController@delete'); + //Move the item from the wishlist to cart + // Route::get('delete/wishlist/{id}', 'WishlistController@delete'); //address Route::get('get/address', 'AddressController@get'); @@ -24,21 +31,24 @@ Route::group(['namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => Route::post('create/address', 'AddressController@create'); Route::put('make/default/address/{id}', 'AddressController@makeDefault'); Route::delete('delete/address/{id}', 'AddressController@delete'); -}); - -Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'api/cart'], function ($router) { //cart //active + inactive instances of cart for the current logged in user Route::get('get/all', 'CartController@getAllCart'); - //active instances of cart for the current logged in user Route::get('get/active', 'CartController@getActiveCart'); - //inactive instances of cart for the current logged in user Route::get('get/inactive', 'CartController@getInactiveCart'); }); +Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'api/cart'], function ($router) { + //cart + //add item in the cart + // Route::get('add/{id}', 'CartController@add'); + //remove item to the cart + // Route::get('remove/{id}', 'CartController@add'); +}); + Route::group(['namespace' => 'Webkul\API\Http\Controllers\Product', 'prefix' => 'api/product'], function ($router) { //product //to fetch the new product diff --git a/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php b/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php index 25e8a3d95..a7fb2fdd0 100644 --- a/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php @@ -75,6 +75,12 @@ class WishlistController extends Controller $checked = $this->wishlist->findWhere(['channel_id' => core()->getCurrentChannel()->id, 'product_id' => $itemId, 'customer_id' => auth()->guard('customer')->user()->id]); + //accidental case if some one adds id of the product in the anchor tag amd gives id of a variant. + if($product->parent_id != null || $product->parent_id != 'null') { + $product = $this->product->findOneByField('id', $product->parent_id); + $data['product_id'] = $product->parent_id; + } + if($checked->isEmpty()) { if($this->wishlist->create($data)) { session()->flash('success', trans('customer::app.wishlist.success'));