Add new items listing and counting #14

Merged
krzysiej merged 3 commits from feature/list-new-items into master 2025-01-05 18:53:14 +01:00
3 changed files with 28 additions and 0 deletions
Showing only changes of commit 14104f61a4 - Show all commits

View File

@@ -0,0 +1,22 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Illuminate\Database\Eloquent\Builder;
use Krzysiej\RyobiCrawler\Models\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class NewController extends BaseController
{
#[Route('/promos', name: 'app_new')]
public function __invoke(): Response
{
$products = Product::whereHas('created_at', now()->subDays(30)->format('Y-m-d'))
->orderByDesc('starred')
->orderByDesc('created_by')
->with(['currentPrice'])
->get();
return $this->render('productList.html.twig', ['products' => $products]);
}
}

View File

@@ -32,6 +32,11 @@ class AppExtension extends AbstractExtension
return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count();
}
public function newCount(): int
{
return Product::whereHas('created_at', now()->subDays(30)->format('Y-m-d'))->count();
}
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null
{
return $items->first(fn($item) => str_starts_with($item->created_at, $date));

View File

@@ -20,6 +20,7 @@
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{ path('app_promos') }}">Promos <span class="badge text-bg-secondary">{{ promosCount() }}</span></a>
<a class="nav-link active" aria-current="page" href="{{ path('app_new') }}">New in last 30 days<span class="badge text-bg-secondary">{{ newCount() }}</span></a>
</li>
</ul>
<form class="d-flex col-lg-6 col-sm-8" role="search" action="{{ path('app_search') }}">