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 45 additions and 3 deletions

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

@@ -0,0 +1,23 @@
<?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('/new', name: 'app_new')]
public function __invoke(): Response
{
$date = (new \DateTime())->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]);
}
}

View File

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

View File

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

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

@@ -21,6 +21,9 @@
<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>
<form class="d-flex col-lg-6 col-sm-8" role="search" action="{{ path('app_search') }}">
<input class="form-control me-2" type="search" name="search" placeholder="Search term eg. 36v or RCS18X" value="{{ search|default('') }}" aria-label="Search">