feature/handle-promotions #59

Merged
krzysiej merged 5 commits from feature/handle-promotions into master 2026-02-04 08:35:16 +01:00
4 changed files with 17 additions and 8 deletions
Showing only changes of commit e012d3d2cd - Show all commits

View File

@@ -12,15 +12,16 @@ final class PromosController extends BaseController
public function __invoke(?string $promo): Response public function __invoke(?string $promo): Response
{ {
if($this->cache->getItem('list_promos')->isHit()) { 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('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->with(['currentPrice', 'lowestPrice']) ->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(); ->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\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
use function Symfony\Component\Clock\now; use function Symfony\Component\Clock\now;
/** /**
@@ -59,6 +60,7 @@ class Product extends Model
{ {
return $this->hasOne(Price::class)->ofMany('price', 'MIN'); return $this->hasOne(Price::class)->ofMany('price', 'MIN');
} }
public function newestPrice(): HasOne public function newestPrice(): HasOne
{ {
return $this->hasOne(Price::class)->latest(); return $this->hasOne(Price::class)->latest();
@@ -71,7 +73,7 @@ class Product extends Model
public function currentStock(): HasOne 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 public function toggleStarred(): self
@@ -88,11 +90,16 @@ class Product extends Model
set: fn(array $value) => json_encode($value), set: fn(array $value) => json_encode($value),
); );
} }
public function promotions(): Attribute public function promotions(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn(?string $value) => json_decode($value ?? '{"hasPromotion": false}', 1), 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'); return $this->lastSeen < now()->format('Y-m-d');
} }
public function isNew(): bool public function isNew(): bool
{ {
return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d'); return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d');

View File

@@ -11,7 +11,7 @@
<td> <td>
<a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a> <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> <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>
<td> <td>
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';"> <nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">

View File

@@ -49,7 +49,7 @@
<span class="badge text-bg-light"><a <span class="badge text-bg-light"><a
href="{{ path('app_search', {'search': product.subTitle}) }}" href="{{ path('app_search', {'search': product.subTitle}) }}"
class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span> 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>
<td class="align-middle"> <td class="align-middle">
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';"> <nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">