fix: 🐛 update ratings cache scoped on specific model #312
This commit is contained in:
parent
b94d6f711c
commit
9dc3168dcd
4 changed files with 39 additions and 12 deletions
|
|
@ -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),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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([
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue