diff --git a/bin/bash b/bin/bash index 65f38a4..a17b24a 100755 --- a/bin/bash +++ b/bin/bash @@ -1,2 +1,2 @@ #!/usr/bin/env bash -docker compose exec php-app bash \ No newline at end of file +bin/cli bash \ No newline at end of file diff --git a/screenshot1.png b/screenshot1.png index bada133..d9b677e 100644 Binary files a/screenshot1.png and b/screenshot1.png differ diff --git a/src/Controller/NewController.php b/src/Controller/NewController.php new file mode 100644 index 0000000..3a54105 --- /dev/null +++ b/src/Controller/NewController.php @@ -0,0 +1,23 @@ +modify('-30 days')->format('Y-m-d'); + $products = Product::where('created_at', '>', $date) + ->orderByDesc('starred') + ->orderByDesc('created_by') + ->with(['currentPrice']) + ->get(); + return $this->render('productList.html.twig', ['products' => $products]); + } +} diff --git a/src/Models/Product.php b/src/Models/Product.php index 4ddb836..f2a1ce2 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -31,7 +31,7 @@ class Product extends Model public function isStarred(): bool { - return (bool) $this->starred; + return (bool)$this->starred; } public function currentPrice(): HasOne @@ -65,4 +65,10 @@ class Product extends Model set: fn(array $value) => json_encode($value), ); } + + public function isnew(): bool + { + $newDate = (new \DateTime())->modify('-30 days')->format('Y-m-d'); + return $this->created_at->format('Y-m-d') > $newDate; + } } diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 0376fbd..48a0ce5 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -17,6 +17,7 @@ class AppExtension extends AbstractExtension { return [ new TwigFunction('promosCount', [$this, 'promosCount']), + new TwigFunction('newCount', [$this, 'newCount']), ]; } @@ -32,6 +33,12 @@ class AppExtension extends AbstractExtension return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count(); } + public function newCount(): int + { + $date = (new \DateTime())->modify('-30 days')->format('Y-m-d'); + return Product::where('created_at', '>', $date)->count(); + } + public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null { return $items->first(fn($item) => str_starts_with($item->created_at, $date)); diff --git a/templates/productList.html.twig b/templates/productList.html.twig index e8ce0f3..af16217 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -25,13 +25,16 @@ {% else %} out of stock {% endif %} + {% if product.isnew() %} + is new + {% endif %} {{ product.subTitle }}