58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\StoreRatingRequest;
|
|
use App\Models\Rating;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RatingController extends Controller
|
|
{
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Fo\StoreRatingRequest $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function store(StoreRatingRequest $request): \Illuminate\Http\JsonResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$validated = $request->validated();
|
|
|
|
/** @var \App\Models\Product|\App\Models\Recipe $relatedModel */
|
|
$relatedModel = app($validated['rating_type'])::findOrFail($validated['rating_id']);
|
|
|
|
// Delete rating.
|
|
if ($validated['rate'] == 0) {
|
|
Rating::query()->where([
|
|
['customer_id', $validated['customer_id']],
|
|
['rating_id', $validated['rating_id']],
|
|
['rating_type', $validated['rating_type']],
|
|
])->first()->delete();
|
|
|
|
return response()->json([
|
|
'message' => trans('Votre note a bien été supprimée !'),
|
|
'data' => Rating::resetCache($relatedModel),
|
|
]);
|
|
}
|
|
|
|
// Save or update rating.
|
|
Rating::updateOrCreate(
|
|
[
|
|
'customer_id' => $validated['customer_id'],
|
|
'rating_id' => $validated['rating_id'],
|
|
'rating_type' => $validated['rating_type'],
|
|
],
|
|
[
|
|
'rate' => $validated['rate'],
|
|
]
|
|
);
|
|
|
|
return response()->json([
|
|
'message' => trans('Votre note a bien été enregistrée !'),
|
|
'data' => Rating::resetCache($relatedModel),
|
|
]);
|
|
});
|
|
}
|
|
}
|