Filter by promo type

This commit is contained in:
2026-02-03 09:27:37 +01:00
parent a21e366c2c
commit e012d3d2cd
4 changed files with 17 additions and 8 deletions

View File

@@ -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]);
}
}

View File

@@ -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');

View File

@@ -11,7 +11,7 @@
<td>
<a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a>
<span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}" class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.tag}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
</td>
<td>
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">

View File

@@ -49,7 +49,7 @@
<span class="badge text-bg-light"><a
href="{{ path('app_search', {'search': product.subTitle}) }}"
class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.tag}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
</td>
<td class="align-middle">
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">