Add new items listing and counting #14
2
bin/bash
2
bin/bash
@@ -1,2 +1,2 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
docker compose exec php-app bash
|
bin/cli bash
|
||||||
BIN
screenshot1.png
BIN
screenshot1.png
Binary file not shown.
|
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 199 KiB |
23
src/Controller/NewController.php
Normal file
23
src/Controller/NewController.php
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,4 +65,10 @@ class Product extends Model
|
|||||||
set: fn(array $value) => json_encode($value),
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class AppExtension extends AbstractExtension
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFunction('promosCount', [$this, 'promosCount']),
|
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();
|
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
|
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null
|
||||||
{
|
{
|
||||||
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
||||||
|
|||||||
@@ -25,13 +25,16 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<span class="badge text-bg-warning">out of stock</span>
|
<span class="badge text-bg-warning">out of stock</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if product.isnew() %}
|
||||||
|
<span class="badge text-bg-success">is new</span>
|
||||||
|
{% endif %}
|
||||||
<span class="badge text-bg-light">{{ product.subTitle }}</span>
|
<span class="badge text-bg-light">{{ product.subTitle }}</span>
|
||||||
</td>
|
</td>
|
||||||
<td class="align-middle">
|
<td class="align-middle">
|
||||||
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';" >
|
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';" >
|
||||||
<ol class="breadcrumb">
|
<ol class="breadcrumb">
|
||||||
{% for category in product.categories %}
|
{% 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 %}
|
{% endfor %}
|
||||||
</ol>
|
</ol>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
@@ -21,6 +21,9 @@
|
|||||||
<li class="nav-item">
|
<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_promos') }}">Promos <span class="badge text-bg-secondary">{{ promosCount() }}</span></a>
|
||||||
</li>
|
</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>
|
</ul>
|
||||||
<form class="d-flex col-lg-6 col-sm-8" role="search" action="{{ path('app_search') }}">
|
<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">
|
<input class="form-control me-2" type="search" name="search" placeholder="Search term eg. 36v or RCS18X" value="{{ search|default('') }}" aria-label="Search">
|
||||||
|
|||||||
Reference in New Issue
Block a user