diff --git a/app/Http/Controllers/Fo/ProductController.php b/app/Http/Controllers/Fo/ProductController.php index 8610bfb6..5460fb42 100644 --- a/app/Http/Controllers/Fo/ProductController.php +++ b/app/Http/Controllers/Fo/ProductController.php @@ -117,7 +117,9 @@ class ProductController extends Controller 'recipeModel' => $recipeModel, 'videoModel' => $videoModel, 'staticPageModel' => $staticPageModel, - 'ratingsData' => Rating::getRatings($productModel), + 'ratingsData' => array_merge(Rating::getRatings($productModel), [ + 'customerRating' => Rating::getCustomerRating($productModel), + ]), ]); } diff --git a/app/Http/Controllers/Fo/RatingController.php b/app/Http/Controllers/Fo/RatingController.php index fe4ba177..ab9fcf60 100644 --- a/app/Http/Controllers/Fo/RatingController.php +++ b/app/Http/Controllers/Fo/RatingController.php @@ -23,6 +23,9 @@ class RatingController extends Controller /** @var \App\Models\Product|\App\Models\Recipe $relatedModel */ $relatedModel = app($validated['rating_type'])::findOrFail($validated['rating_id']); + // Update cache scoped on specific model (Product or Recipe). + Rating::resetCache($relatedModel); + // Delete rating. if ($validated['rate'] == 0) { Rating::query()->where([ diff --git a/app/Http/Controllers/Fo/RecipeController.php b/app/Http/Controllers/Fo/RecipeController.php index 34ca39d0..68163618 100644 --- a/app/Http/Controllers/Fo/RecipeController.php +++ b/app/Http/Controllers/Fo/RecipeController.php @@ -92,7 +92,9 @@ class RecipeController extends Controller 'recipeModels' => $this->getRecipesFiltered($recipeModel), 'associatedProductModels' => $recipeModel->products->where('published', true), 'staticPageModel' => $staticPageModel, - 'ratingsData' => Rating::getRatings($recipeModel), + 'ratingsData' => array_merge(Rating::getRatings($recipeModel), [ + 'customerRating' => Rating::getCustomerRating($recipeModel), + ]), ]); } diff --git a/app/Models/Rating.php b/app/Models/Rating.php index c1e43098..899afe3e 100644 --- a/app/Models/Rating.php +++ b/app/Models/Rating.php @@ -70,26 +70,46 @@ class Rating extends Model public static function getRatings(Product|Recipe $model): array { return Cache::remember(sprintf('%s-ratings', $model->slug), 86400, function () use ($model) { - /** @var \App\Models\Customer|null customerModel */ - $customerModel = auth('club')->user(); - return [ - 'total' => $model->ratings()->count(), - 'totalRate' => round($model->ratings()->get()->avg(function (Rating $model) { + 'total' => $model->ratings()->count(), + 'totalRate' => round($model->ratings()->get()->avg(function (Rating $model) { return $model->rate->value(); }), 1), - 'ratings' => $model->ratings()->inRandomOrder() + 'ratings' => $model->ratings()->inRandomOrder() ->take(6)->with('customer')->get()->each(function (Rating $rating) { $rating->customer_username = $rating->customer->username; return $rating; }), - 'ratingType' => get_class($model), - 'customerRating' => $customerModel - ? $model->ratings()->where('customer_id', $customerModel->getkey())->first() - : null, + 'ratingType' => get_class($model), ]; }); } + + /** + * Get customer rating on specific model. + * + * @param \App\Models\Product|\App\Models\Recipe $model + * @return self|null + */ + public static function getCustomerRating(Product|Recipe $model): self|null + { + /** @var \App\Models\Customer|null customerModel */ + $customerModel = auth('club')->user(); + + if (!$customerModel) { + return null; + } + + return Cache::remember( + sprintf('rating-%s-%s', $model->slug, $customerModel->hashcode), + 86400, + function () use ($model, $customerModel) { + return $customerModel + ? $model->ratings()->where('customer_id', $customerModel->getkey())->first() + : null; + } + ); + } /** * Create a SchemaOrg.