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
7 changed files with 20 additions and 7 deletions
Showing only changes of commit d6bcffc3e1 - Show all commits

View File

@@ -1,2 +1,2 @@
#!/usr/bin/env bash
docker compose exec php-app bash
bin/cli bash

Binary file not shown.

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 199 KiB

View File

@@ -9,10 +9,11 @@ use Symfony\Component\Routing\Attribute\Route;
final class NewController extends BaseController
{
#[Route('/promos', name: 'app_new')]
#[Route('/new', name: 'app_new')]
public function __invoke(): Response
{
$products = Product::whereHas('created_at', now()->subDays(30)->format('Y-m-d'))
$date = (new \DateTime())->modify('-30 days')->format('Y-m-d');
$products = Product::where('created_at', '>', $date)
->orderByDesc('starred')
->orderByDesc('created_by')
->with(['currentPrice'])

View File

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

View File

@@ -35,7 +35,8 @@ class AppExtension extends AbstractExtension
public function newCount(): int
{
return Product::whereHas('created_at', now()->subDays(30)->format('Y-m-d'))->count();
$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

View File

@@ -25,13 +25,16 @@
{% else %}
<span class="badge text-bg-warning">out of stock</span>
{% endif %}
{% if product.isnew() %}
<span class="badge text-bg-success">is new</span>
{% endif %}
<span class="badge text-bg-light">{{ product.subTitle }}</span>
</td>
<td class="align-middle">
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';" >
<ol class="breadcrumb">
{% for category in product.categories %}
<li class="breadcrumb-item" aria-current="page"><a class="breadcrumb-item text-decoration-none" href="{{ path('app_category', {'category': category}) }}">{{ category }}</a></li></li>
<li class="breadcrumb-item" aria-current="page"><a class="breadcrumb-item text-decoration-none" href="{{ path('app_category', {'category': category}) }}">{{ category }}</a></li>
{% endfor %}
</ol>
</nav>

View File

@@ -20,6 +20,8 @@
<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>
</li>
<li class="nav-item">
<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>