Customer Wishlist API implemented

This commit is contained in:
prashant-webkul 2018-11-24 15:26:31 +05:30
parent 9ce3e53914
commit 4455613180
7 changed files with 121 additions and 30 deletions

View File

@ -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
*

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -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]);
}
}
}

View File

@ -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);
}
}

View File

@ -1,6 +1,6 @@
<?php
Route::group(['namespace' => '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

View File

@ -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'));