Add twig extra bundle to use currency formatter filter. Add custom twig method to display number of active promos. Remove old index.php file.

This commit is contained in:
Krzysztof Płaczek
2024-11-17 12:33:40 +01:00
parent f1c0ec6b3a
commit 48ee68f71a
9 changed files with 264 additions and 60 deletions

View File

@@ -2,7 +2,6 @@
namespace Krzysiej\RyobiCrawler\Controller;
use Illuminate\Support\ItemNotFoundException;
use Krzysiej\RyobiCrawler\Models\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
@@ -21,7 +20,8 @@ final class ProductController extends BaseController
public function __invoke(int $productId): Response
{
$product = Product::with([
'price' => fn($query) => $query->orderBy('created_at', 'desc')
'price' => fn($query) => $query->orderBy('created_at', 'desc'),
'stock' => fn($query) => $query->orderBy('created_at', 'desc'),
])->find($productId);
if(null === $product) {

23
src/Twig/AppExtension.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace Krzysiej\RyobiCrawler\Twig;
use Illuminate\Database\Eloquent\Builder;
use Krzysiej\RyobiCrawler\Models\Product;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('promosCount', [$this, 'promosCount']),
];
}
public function promosCount(): int
{
return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count();
}
}