From e012d3d2cdcb2929d6c2d3420411c5e7c37f1da3 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Tue, 3 Feb 2026 09:27:37 +0100 Subject: [PATCH] Filter by promo type --- src/Controller/PromosController.php | 9 +++++---- src/Models/Product.php | 12 ++++++++++-- templates/product.html.twig | 2 +- templates/productList.html.twig | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Controller/PromosController.php b/src/Controller/PromosController.php index 3767f5d..e412566 100644 --- a/src/Controller/PromosController.php +++ b/src/Controller/PromosController.php @@ -12,15 +12,16 @@ final class PromosController extends BaseController public function __invoke(?string $promo): Response { if($this->cache->getItem('list_promos')->isHit()) { - return $this->render('productList.html.twig', ['listType' => 'promos'.urlencode($promo)]); + return $this->render('productList.html.twig', ['listType' => 'promos'.$promo]); } - $products = Product::whereRaw('priceCurrent < productStandardPrice') + $products = Product::when(is_null($promo), fn($q) => $q->whereRaw('priceCurrent < productStandardPrice')) ->orderByDesc('starred') ->orderByDesc('created_by') ->with(['currentPrice', 'lowestPrice']) - ->when(!is_null($promo) , fn ($q) => $q->whereRaw("json_extract(promotions, '$.tag') LIKE ?", $promo)) + ->when(!is_null($promo), fn ($q) => $q->whereRaw("json_extract(promotions, '$.slug') LIKE ?", $promo)) ->get(); - return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos'.urlencode($promo)]); + + return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos'.$promo]); } } diff --git a/src/Models/Product.php b/src/Models/Product.php index ecaf52c..aff74b6 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Support\Str; use function Symfony\Component\Clock\now; /** @@ -59,6 +60,7 @@ class Product extends Model { return $this->hasOne(Price::class)->ofMany('price', 'MIN'); } + public function newestPrice(): HasOne { return $this->hasOne(Price::class)->latest(); @@ -71,7 +73,7 @@ class Product extends Model public function currentStock(): HasOne { - return $this->stock()->one()->ofMany()->withDefault(fn (Stock $stock) => $stock->stock = 0); + return $this->stock()->one()->ofMany()->withDefault(fn(Stock $stock) => $stock->stock = 0); } public function toggleStarred(): self @@ -88,11 +90,16 @@ class Product extends Model set: fn(array $value) => json_encode($value), ); } + public function promotions(): Attribute { return Attribute::make( get: fn(?string $value) => json_decode($value ?? '{"hasPromotion": false}', 1), - set: fn(array $value) => json_encode($value), + set: function (\stdClass $value) { + $value->slug = Str::slug($value->tag); + + return json_encode($value); + } ); } @@ -100,6 +107,7 @@ class Product extends Model { return $this->lastSeen < now()->format('Y-m-d'); } + public function isNew(): bool { return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d'); diff --git a/templates/product.html.twig b/templates/product.html.twig index 3bbc3fe..2cd6b83 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -11,7 +11,7 @@ {{ product.name }} {{ product.subTitle }} - {% if product.promotions is not null and product.promotions.hasPromotion %}PROMO: {{ product.promotions.tag }}{% endif %} + {% if product.promotions is not null and product.promotions.hasPromotion %}PROMO: {{ product.promotions.tag }}{% endif %}