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

71 lines
2.0 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;
class ReviewController extends Controller
{
/**
2020-10-05 12:57:30 +00:00
* Contains current guard.
2019-05-07 11:36:21 +00:00
*
* @var array
*/
protected $guard;
/**
2020-10-05 12:57:30 +00:00
* ProductReviewRepository $reviewRepository
2019-05-07 11:36:21 +00:00
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Product\Repositories\ProductReviewRepository
2019-05-07 11:36:21 +00:00
*/
protected $reviewRepository;
/**
2020-10-05 12:57:30 +00:00
* Controller instance.
2019-05-07 11:36:21 +00:00
*
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 13:37:08 +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();
2020-10-06 06:33:40 +00:00
$this->validate($request, [
2019-05-07 11:36:21 +00:00
'comment' => 'required',
'rating' => 'required|numeric|min:1|max:5',
'title' => 'required',
]);
2020-10-05 12:57:30 +00:00
$productReview = $this->reviewRepository->create([
2019-05-07 11:36:21 +00:00
'customer_id' => $customer ? $customer->id : null,
2020-10-05 12:57:30 +00:00
'name' => $customer ? $customer->name : $request->get('name'),
2020-02-19 11:24:04 +00:00
'status' => 'pending',
2020-03-04 06:32:53 +00:00
'product_id' => $id,
2020-10-05 12:57:30 +00:00
'comment' => $request->comment,
'rating' => $request->rating,
'title' => $request->title
2019-05-07 11:36:21 +00:00
]);
return response()->json([
2020-02-27 08:03:03 +00:00
'message' => 'Your review submitted successfully.',
2020-10-05 13:02:03 +00:00
'data' => new ProductReviewResource($productReview),
2020-02-27 08:03:03 +00:00
]);
2019-05-07 11:36:21 +00:00
}
}