sarga/packages/Webkul/API/Http/Controllers/Shop/ReviewController.php

76 lines
2.1 KiB
PHP
Raw Normal View History

2019-05-07 11:36:21 +00:00
<?php
namespace Webkul\API\Http\Controllers\Shop;
use Illuminate\Http\Request;
use Webkul\Product\Repositories\ProductReviewRepository;
use Webkul\API\Http\Resources\Catalog\ProductReview as ProductReviewResource;
/**
* Review controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ReviewController extends Controller
{
/**
* Contains current guard
*
* @var array
*/
protected $guard;
/**
* ProductReviewRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Product\Repositories\ProductReviewRepository
2019-05-07 11:36:21 +00:00
*/
protected $reviewRepository;
/**
* Controller instance
*
2020-03-05 05:34:57 +00:00
* @param Webkul\Product\Repositories\ProductReviewRepository $reviewRepository
2019-05-07 11:36:21 +00:00
*/
public function __construct(ProductReviewRepository $reviewRepository)
{
$this->guard = request()->has('token') ? 'api' : 'customer';
auth()->setDefaultDriver($this->guard);
$this->reviewRepository = $reviewRepository;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
2020-03-05 05:34:57 +00:00
* @param int $id
2019-05-07 11:36:21 +00:00
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $id)
{
$customer = auth($this->guard)->user();
$this->validate(request(), [
'comment' => 'required',
'rating' => 'required|numeric|min:1|max:5',
'title' => 'required',
]);
$data = array_merge(request()->all(), [
'customer_id' => $customer ? $customer->id : null,
2020-02-19 11:24:04 +00:00
'name' => $customer ? $customer->name : request()->input('name'),
'status' => 'pending',
2020-03-04 06:32:53 +00:00
'product_id' => $id,
2019-05-07 11:36:21 +00:00
]);
$productReview = $this->reviewRepository->create($data);
return response()->json([
2020-02-27 08:03:03 +00:00
'message' => 'Your review submitted successfully.',
2020-03-04 06:32:53 +00:00
'data' => new ProductReviewResource($this->reviewRepository->find($productReview->id)),
2020-02-27 08:03:03 +00:00
]);
2019-05-07 11:36:21 +00:00
}
}