Keep track of discontinued items. That means items that were tracked, but then they were never updated.Added is discontinued badge. #20

Merged
krzysiej merged 1 commits from feature/discontinued into master 2025-02-15 11:33:45 +01:00
7 changed files with 52 additions and 9 deletions

View File

@@ -40,7 +40,6 @@ class ScrapeWebsite extends Command
$products = $this->getProducts();
$progress->setMaxSteps(count($products));
foreach ($products as $product) {
//dd($product);
$this->saveProduct($product);
$progress->advance();
}
@@ -91,6 +90,7 @@ class ScrapeWebsite extends Command
$productModel->variantCode = $product->variantCode;
$productModel->modelCode = $product->modelCode;
$productModel->url = $product->url;
$productModel->touch('updated_at');
$productModel->save();
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();

View File

@@ -0,0 +1,24 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Krzysiej\RyobiCrawler\Models\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function Symfony\Component\Clock\now;
final class DiscontinuedController extends BaseController
{
#[Route('/discontinued', name: 'app_discontinued')]
public function __invoke(): Response
{
$products = Product::where('updated_at', '<', now()->format('Y-m-d'))
->orderByDesc('starred')
->orderByDesc('created_by')
->with(['currentPrice'])
->get();
return $this->render('productList.html.twig', ['products' => $products]);
}
}

View File

@@ -7,13 +7,14 @@ use Krzysiej\RyobiCrawler\Models\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function Symfony\Component\Clock\now;
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)
$products = Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d'))
->orderByDesc('starred')
->orderByDesc('created_by')
->with(['currentPrice'])

View File

@@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use function Symfony\Component\Clock\now;
/**
* @property integer $skuID
* @property string $name
@@ -66,9 +68,12 @@ class Product extends Model
);
}
public function isnew(): bool
public function isDiscontinued(): bool
{
$newDate = (new \DateTime())->modify('-30 days')->format('Y-m-d');
return $this->created_at->format('Y-m-d') > $newDate;
return $this->updated_at->format('Y-m-d') < now()->format('Y-m-d');
}
public function isNew(): bool
{
return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d');
}
}

View File

@@ -11,6 +11,8 @@ use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use function Symfony\Component\Clock\now;
class AppExtension extends AbstractExtension
{
public function getFunctions(): array
@@ -18,6 +20,7 @@ class AppExtension extends AbstractExtension
return [
new TwigFunction('promosCount', [$this, 'promosCount']),
new TwigFunction('newCount', [$this, 'newCount']),
new TwigFunction('discontinuedCount', [$this, 'discontinuedCount']),
];
}
@@ -35,8 +38,12 @@ class AppExtension extends AbstractExtension
public function newCount(): int
{
$date = (new \DateTime())->modify('-30 days')->format('Y-m-d');
return Product::where('created_at', '>', $date)->count();
return Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d'))->count();
}
public function discontinuedCount(): int
{
return Product::where('updated_at', '<', now()->format('Y-m-d'))->count();
}
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null

View File

@@ -25,7 +25,10 @@
{% else %}
<span class="badge text-bg-warning">out of stock</span>
{% endif %}
{% if product.isnew() %}
{% if product.isDiscontinued() %}
<span class="badge text-bg-secondary">is discontinued</span>
{% endif %}
{% if product.isNew() %}
<span class="badge text-bg-success">is new</span>
{% endif %}
<span class="badge text-bg-light">{{ product.subTitle }}</span>

View File

@@ -24,6 +24,9 @@
<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>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{ path('app_discontinued') }}">Discontinued <span class="badge text-bg-secondary">{{ discontinuedCount() }}</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">