Product API for Storefront Completed

This commit is contained in:
prashant-webkul 2018-11-25 20:51:21 +05:30
parent 4e6c462317
commit dbdf702a28
6 changed files with 241 additions and 22 deletions

View File

@ -6,32 +6,70 @@ use Webkul\API\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Product\Repositories\ProductRepository as Product;
use Auth;
/**
* Product controller for the APIs of Getting Products
* Product controller
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ProductController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* ProductRepository object
*
* @var array
*/
protected $product;
public function __construct(Product $product)
/**
* Create a new controller instance.
*
* @param Webkul\Product\Repositories\ProductRepository $product
* @return void
*/
public function __construct( Product $product)
{
$this->product = $product;
$this->_config = request('_config');
}
public function getAllProducts() {
/**
* Display a listing of the resource.
*
* @param string $slug
* @return \Illuminate\Http\Response
*/
public function getBySlug($slug)
{
$product = $this->product->findBySlugOrFail($slug);
return response()->json(['message' => 'success', 'product' => $product]);
}
public function getAll() {
$products = $this->product->all();
return response()->json($products, 200);
}
public function getNewProducts() {
$newProducts = $this->product->getNewProduct();
public function getNew() {
$newProducts = $this->product->getNewProducts();
return response()->json($newProducts, 200);
}
}
public function getFeatured() {
$featuredProducts = $this->product->getFeaturedProducts();
return response()->json($featuredProducts, 200);
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace Webkul\API\Http\Controllers\Product;
use Webkul\API\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Product\Repositories\ProductRepository as Product;
use Webkul\Product\Repositories\ProductReviewRepository as ProductReview;
use Validator;
/**
* Review controller
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ReviewController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* ProductRepository object
*
* @var array
*/
protected $product;
/**
* ProductReviewRepository object
*
* @var array
*/
protected $productReview;
/**
* Create a new controller instance.
*
* @param Webkul\Product\Repositories\ProductRepository $product
* @param Webkul\Product\Repositories\ProductReviewRepository $productReview
* @return void
*/
public function __construct(Product $product, ProductReview $productReview)
{
// $this->middleware('customer')->only(['create', 'store', 'destroy']);
$this->product = $product;
$this->productReview = $productReview;
$this->_config = request('_config');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request , $id)
{
$this->validate(request(), [
'comment' => 'required',
'rating' => 'required',
'title' => 'required',
]);
$data = request()->all();
$customer_id = auth()->guard('customer')->user()->id;
$data['status'] = 'pending';
$data['product_id'] = $id;
$data['customer_id'] = $customer_id;
$result = $this->productReview->create($data);
if($result) {
return response()->json(['message' => 'success', 'status' => $result]);
} else {
return response()->json(['message' => 'failed', 'status' => $result]);
}
}
/**
* Display reviews accroding to product.
*
* @param string $slug
* @return \Illuminate\Http\Response
*/
public function show($slug)
{
$product = $this->product->findBySlugOrFail($slug);
return view($this->_config['view'],compact('product'));
}
/**
* Delete the review of the current product
*
* @return response
*/
public function destroy($id)
{
$this->productReview->delete($id);
session()->flash('success', 'Product Review Successfully Deleted');
return redirect()->back();
}
}

View File

@ -8,7 +8,7 @@ use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Checkout\Repositories\CartItemRepository as CartItem;
use Webkul\API\Http\Controllers\Shop\OnePagePresenter as PresenterOne;
use Webkul\API\Http\Controllers\Shop\Presenter as Presenter;
use Auth;
use Cart;
@ -88,8 +88,8 @@ class CartController extends Controller
return response()->json(['message' => 'empty', 'items' => null]);
}
$presenter = new PresenterOne();
$summary = $presenter->presenter($cart);
$presenter = new Presenter();
$summary = $presenter->onePagePresenter($cart);
return response()->json(['message' => 'success', 'items' => $cart->items, 'summary' => $summary]);
}

View File

@ -0,0 +1,49 @@
<?php
namespace Webkul\API\Http\Controllers\Shop;
use Webkul\API\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Collection;
use Webkul\Category\Repositories\CategoryRepository as Category;
/**
* Category controller for getting the categories
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CategoryController extends Controller
{
/**
* The category implementation.
*
* for shop bundle's navigation
* menu
*/
protected $category;
/**
* Category Repository DI.
*
* @param $category Category Instance
* @return void
*/
public function __construct(Category $category)
{
$this->category = $category;
}
public function get() {
$categories = [];
foreach ($this->category->getVisibleCategoryTree() as $category) {
array_push($categories, collect($category));
}
return $categories;
}
}

View File

@ -5,12 +5,12 @@ namespace Webkul\API\Http\Controllers\Shop;
/**
* Presenter Class for cart and mini cart(if implemented)
*/
class OnePagePresenter {
class Presenter {
/**
* presenter method will handle the cart and collect all the necessary data to be displayed on the onepage cart
*/
public function presenter($cart) {
public function onePagePresenter($cart) {
$cartSummary['grand_total'] = $cart->grand_total;
$cartSummary['tax_total'] = $cart->tax_total;
$cartSummary['total_items'] = $cart->items_count;

View File

@ -1,13 +1,11 @@
<?php
Route::group(['middleware' => 'api','namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => 'api/customer'], function ($router) {
Route::post('/register', 'RegistrationController@create');
});
Route::group(['namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => 'api/customer'], function ($router) {
//customer registration API
Route::post('/register', 'RegistrationController@create');
//auth route for customer
Route::post('login', 'AuthController@create');
//auth route for customer
Route::post('logout', 'AuthController@destroy');
@ -32,13 +30,14 @@ Route::group(['namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' =>
Route::put('make/default/address/{id}', 'AddressController@makeDefault');
Route::delete('delete/address/{id}', 'AddressController@delete');
//cart
//cart -> not to be used
//active + inactive instances of cart for the current logged in user
Route::get('get/all', 'CustomerController@getAllCart');
//active instances of cart for the current logged in user
Route::get('get/active', 'CustomerController@getActiveCart');
});
//all the cart related API for customer and guests
Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'api/cart'], function ($router) {
//cart
Route::get('get', 'CartController@get');
@ -52,12 +51,29 @@ Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'ap
Route::put('/update/onepage', 'CartController@updateOnePage');
});
//all the product related API to be used for store front
Route::group(['namespace' => 'Webkul\API\Http\Controllers\Product', 'prefix' => 'api/product'], function ($router) {
//product
//to fetch the new product
Route::get('get/all', 'ProductController@getAllProducts');
//to get all products
Route::get('get/all', 'ProductController@getAll');
//to fetch the new products
Route::get('get/new', 'ProductController@getNew');
//to fetch the featured product
Route::get('get/featured', 'ProductController@getFeatured');
//to get the product by its slug
Route::get('get/{slug}', 'ProductController@getBySlug');
//to get the reviews of the product
Route::get('review/{slug}', 'ReviewController@show');
//to store the review for a product
Route::post('review/{slug}', 'ReviewController@create');
});
Route::group(['namespace' => 'Webkul\API\Http\Controllers\Admin', 'prefix' => 'api/admin'], function ($router) {
//all the shop related API to be used for store front
Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'api/shop'], function ($router) {
Route::get('get/category', 'CategoryController@get');
});