diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 11bfbdd..f6be3fe 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -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(); diff --git a/src/Controller/DiscontinuedController.php b/src/Controller/DiscontinuedController.php new file mode 100644 index 0000000..d296ba8 --- /dev/null +++ b/src/Controller/DiscontinuedController.php @@ -0,0 +1,24 @@ +format('Y-m-d')) + ->orderByDesc('starred') + ->orderByDesc('created_by') + ->with(['currentPrice']) + ->get(); + return $this->render('productList.html.twig', ['products' => $products]); + } +} diff --git a/src/Controller/NewController.php b/src/Controller/NewController.php index 3a54105..0d7f542 100644 --- a/src/Controller/NewController.php +++ b/src/Controller/NewController.php @@ -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']) diff --git a/src/Models/Product.php b/src/Models/Product.php index f2a1ce2..cbafa89 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -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'); } } diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 48a0ce5..05e78c9 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -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 diff --git a/templates/productList.html.twig b/templates/productList.html.twig index a697cd1..82cd8e2 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -25,7 +25,10 @@ {% else %} out of stock {% endif %} - {% if product.isnew() %} + {% if product.isDiscontinued() %} + is discontinued + {% endif %} + {% if product.isNew() %} is new {% endif %} {{ product.subTitle }} diff --git a/templates/template.html.twig b/templates/template.html.twig index ef99caa..7223fcb 100644 --- a/templates/template.html.twig +++ b/templates/template.html.twig @@ -24,6 +24,9 @@ +