Compare commits
26 Commits
1.2.0
...
ebe40785fa
| Author | SHA1 | Date | |
|---|---|---|---|
| ebe40785fa | |||
| 4cf1c2f90b | |||
| e40391eb4c | |||
| 4095037b69 | |||
| b7ebf7374a | |||
| 45343c9121 | |||
| 653f94f9c9 | |||
| 632f76aceb | |||
| cbf143c7a0 | |||
| 7920172735 | |||
| 914310dab8 | |||
| 8e8ef8fe04 | |||
| de4915972c | |||
| 9c2405dd3f | |||
| da6a8f86c2 | |||
| 2c40fb0e61 | |||
| 5314a6a70a | |||
|
|
f772532309 | ||
|
|
17159e811f | ||
|
|
76d8b7d9cf | ||
|
|
f30304cbe9 | ||
| dc287aadc6 | |||
| 708a5eeae0 | |||
|
|
7352eea270 | ||
|
|
e97d976705 | ||
| beb717f9b9 |
@@ -4,7 +4,7 @@
|
||||
|
||||
1. Clone repository using `git clone https://git.techtube.pl/krzysiej/ryobi-crawler.git`
|
||||
2. Cd into project directory `cd ryobi-crawler`
|
||||
3. Build and start docker container `docker compose up -d`
|
||||
3. Build and start docker container `docker compose up -d --build --force-recreate`
|
||||
4. Run `docker compose exec php-app php console.php app:migrate` file to create `database.sqlite` and create tables.
|
||||
5. Run `docker compose exec php-app php console.php app:scrape` command to scrape all the products from the ryobi website.
|
||||
6. Access web interface using `localhost:9001` address in web browser.
|
||||
|
||||
2
bin/cachewarmup
Executable file
2
bin/cachewarmup
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
bin/cli php console.php app:cache:warm-twig
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
[ -z "$1" ] && echo "Please specify a Composer command (ex. install)" && exit
|
||||
[ -z "$1" ] && bin/cli composer list && exit
|
||||
bin/cli composer "$@"
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
echo "Updating project"
|
||||
git pull origin master
|
||||
bin/cli rm -rf var/cache
|
||||
bin/cacheclean
|
||||
echo "Project updated"
|
||||
1151
composer.lock
generated
1151
composer.lock
generated
File diff suppressed because it is too large
Load Diff
BIN
screenshot1.png
BIN
screenshot1.png
Binary file not shown.
|
Before Width: | Height: | Size: 199 KiB After Width: | Height: | Size: 282 KiB |
@@ -16,7 +16,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\Environment;
|
||||
|
||||
#[AsCommand(name: 'app:cache:warm-twig', description: '')]
|
||||
#[AsCommand(name: 'app:cache:warm-twig', description: 'Warmup twig cache')]
|
||||
class CacheWarmCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
@@ -42,8 +42,8 @@ class CacheWarmCommand extends Command
|
||||
$progress = new ProgressBar($output);
|
||||
$progress->start();
|
||||
$products = Product::with([
|
||||
'price' => fn($query) => $query->orderBy('created_at', 'desc'),
|
||||
'stock' => fn($query) => $query->orderBy('created_at', 'desc'),
|
||||
'price' => fn($query) => $query->orderByDesc('created_at'),
|
||||
'stock' => fn($query) => $query->orderByDesc('created_at'),
|
||||
])->get();
|
||||
$progress->setMaxSteps(count($products));
|
||||
|
||||
|
||||
@@ -24,11 +24,10 @@ class Migrate extends Command
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (true === $input->hasOption(self::RECREATE_OPTION)) {
|
||||
if ($input->getOption(self::RECREATE_OPTION)) {
|
||||
unlink(__DIR__ . '/../../database.sqlite');
|
||||
//sleep(5);
|
||||
touch(__DIR__ . '/../../database.sqlite');
|
||||
}
|
||||
touch(__DIR__ . '/../../database.sqlite');
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
@@ -39,6 +38,7 @@ class Migrate extends Command
|
||||
$this->createProductsTable();
|
||||
$this->createPricesTable();
|
||||
$this->createStocksTable();
|
||||
$this->addColumns();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
@@ -94,4 +94,37 @@ class Migrate extends Command
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function addColumns(): void
|
||||
{
|
||||
if (!Capsule::schema()->hasColumn('products', 'priceCurrent')) {
|
||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||
$table->float('priceCurrent')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
if (!Capsule::schema()->hasColumn('products', 'priceLowest')) {
|
||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||
$table->float('priceLowest')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
if (!Capsule::schema()->hasColumn('products', 'productStandardPrice')) {
|
||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||
$table->float('productStandardPrice')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
if (!Capsule::schema()->hasColumn('products', 'lowestProductPrice30Days')) {
|
||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||
$table->float('lowestProductPrice30Days')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
if (!Capsule::schema()->hasColumn('products', 'lastSeen')) {
|
||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||
$table->date('lastSeen')->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Krzysiej\RyobiCrawler\Command;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Krzysiej\RyobiCrawler\Models\Price;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
use Krzysiej\RyobiCrawler\Models\Stock;
|
||||
@@ -21,20 +22,19 @@ class ScrapeWebsite extends Command
|
||||
{
|
||||
private Client $client;
|
||||
|
||||
public function __construct(protected Capsule $database)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => __DIR__ . '/../../database.sqlite',
|
||||
]);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$output->writeln('Scrape products');
|
||||
$progress = new ProgressBar($output);
|
||||
$progress->start();
|
||||
$products = $this->getProducts();
|
||||
@@ -44,8 +44,27 @@ class ScrapeWebsite extends Command
|
||||
$progress->advance();
|
||||
}
|
||||
$progress->finish();
|
||||
$output->writeln('Scrape products - DONE');
|
||||
$output->writeln('');
|
||||
$output->writeln('DONE');
|
||||
|
||||
$output->writeln('Update prices');
|
||||
$products = Product::all();
|
||||
$progress->setMaxSteps(count($products));
|
||||
$progress->start();
|
||||
foreach($products as $product) {
|
||||
$newestPrice = $product->newestPrice;
|
||||
$product->priceCurrent = $newestPrice->price;
|
||||
$product->productStandardPrice = $newestPrice->productStandardPrice;
|
||||
$product->lowestProductPrice30Days = $newestPrice->lowestProductPrice30Days;
|
||||
$product->priceLowest = $product->lowestPrice->price;
|
||||
$product->lastSeen = $newestPrice->created_at->format('Y-m-d');
|
||||
$product->save(['timestamps' => false]);
|
||||
$progress->advance();
|
||||
}
|
||||
$progress->finish();
|
||||
$output->writeln('');
|
||||
$output->writeln('Update prices - DONE');
|
||||
$output->writeln('COMMAND - DONE');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
@@ -90,6 +109,7 @@ class ScrapeWebsite extends Command
|
||||
$productModel->variantCode = $product->variantCode;
|
||||
$productModel->modelCode = $product->modelCode;
|
||||
$productModel->url = $product->url;
|
||||
$productModel->lastSeen = date("Y-m-d");
|
||||
$productModel->touch('updated_at');
|
||||
$productModel->save();
|
||||
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
|
||||
@@ -11,11 +11,7 @@ class BaseController extends AbstractController
|
||||
{
|
||||
protected Environment $twig;
|
||||
|
||||
public function __construct(protected FilesystemAdapter $cache)
|
||||
public function __construct(protected FilesystemAdapter $cache, protected Capsule $database)
|
||||
{
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/../../database.sqlite']);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ final class CategoryController extends BaseController
|
||||
return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
|
||||
}
|
||||
|
||||
$products = Product::with('price')
|
||||
$products = Product::with(['price', 'lowestPrice'])
|
||||
->selectRaw('products.*')
|
||||
->distinct('products.id')
|
||||
->fromRaw('products, json_each(products.categories)')
|
||||
|
||||
@@ -17,10 +17,9 @@ final class DiscontinuedController extends BaseController
|
||||
return $this->render('productList.html.twig', ['listType' => 'discontinued']);
|
||||
}
|
||||
|
||||
$products = Product::where('updated_at', '<', now()->format('Y-m-d'))
|
||||
$products = Product::where('lastSeen', '<', now()->format('Y-m-d'))
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->with(['currentPrice'])
|
||||
->get();
|
||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'discontinued']);
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ final class IndexController extends BaseController
|
||||
#[Route('/', name: 'app_home')]
|
||||
public function __invoke(): Response
|
||||
{
|
||||
if($this->cache->getItem('list_all')->isHit()) {
|
||||
if ($this->cache->getItem('list_all')->isHit()) {
|
||||
return $this->render('productList.html.twig', ['listType' => 'all']);
|
||||
}
|
||||
$products = Product::with(['currentStock', 'price'])
|
||||
$products = Product::with(['currentStock'])
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->get();
|
||||
|
||||
29
src/Controller/LowestPriceController.php
Normal file
29
src/Controller/LowestPriceController.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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 LowestPriceController extends BaseController
|
||||
{
|
||||
#[Route('/lowest-price', name: 'app_lowest_price', methods: ['GET'])]
|
||||
public function __invoke(): Response
|
||||
{
|
||||
$listType = 'lowest_price';
|
||||
if($this->cache->getItem('lowest_price')->isHit()) {
|
||||
return $this->render('productList.html.twig', ['listType' => $listType]);
|
||||
}
|
||||
|
||||
$products = Product::whereRaw('priceCurrent = priceLowest')
|
||||
->whereRaw('lastSeen = "'.now()->format('Y-m-d').'"')
|
||||
->whereRaw('priceCurrent < productStandardPrice')
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->with(['currentPrice', 'lowestPrice'])
|
||||
->get();
|
||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => $listType]);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
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;
|
||||
@@ -21,7 +20,7 @@ final class NewController extends BaseController
|
||||
$products = Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d'))
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->with(['currentPrice'])
|
||||
->with(['currentPrice', 'lowestPrice'])
|
||||
->get();
|
||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'new']);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Twig\Error\LoaderError;
|
||||
@@ -20,7 +19,7 @@ final class ProductController extends BaseController
|
||||
#[Route('/product/{productId<\d+>}', name: 'app_product')]
|
||||
public function __invoke(int $productId): Response
|
||||
{
|
||||
if($this->cache->getItem('product'.$productId)->isHit()) {
|
||||
if ($this->cache->getItem('product' . $productId)->isHit()) {
|
||||
return $this->render('product.html.twig', ['product' => ['id' => $productId]]);
|
||||
}
|
||||
|
||||
@@ -31,26 +30,34 @@ final class ProductController extends BaseController
|
||||
if (null === $product) {
|
||||
throw $this->createNotFoundException('Product not found');
|
||||
}
|
||||
$priceList = $product->price()->pluck('price');
|
||||
$stockList = $product->stock()->pluck('stock');
|
||||
$priceDates = $product->price()->pluck('created_at')->map(fn($date) => $date->format('Y-m-d'))->toArray();
|
||||
$stockDates = $product->stock()->pluck('created_at')->map(fn($date) => $date->format('Y-m-d'))->toArray();
|
||||
|
||||
$priceList = $product->price()->pluck('price', 'created_at')->mapWithKeys(fn($price, $createdAt) => [explode(' ', $createdAt)[0] => $price])->toArray();
|
||||
$stockList = $product->stock()->pluck('stock', 'created_at')->mapWithKeys(fn($stock, $createdAt) => [explode(' ', $createdAt)[0] => $stock])->toArray();
|
||||
return $this->render('product.html.twig', [
|
||||
'product' => $product,
|
||||
'price_list' => $this->prepareChartData($priceDates, $priceList),
|
||||
'stock_list' => $this->prepareChartData($stockDates, $stockList),
|
||||
'price_dates' => implode("','", $priceDates),
|
||||
'price_list' => $this->prepareChartData($priceList),
|
||||
'stock_list' => $this->prepareChartData($stockList),
|
||||
'price_dates' => implode("','", $this->dateRange(array_key_first($priceList), array_key_last($priceList))),
|
||||
]);
|
||||
}
|
||||
|
||||
private function prepareChartData($set1, $set2): string
|
||||
private function prepareChartData($set1): string
|
||||
{
|
||||
$data = [];
|
||||
foreach ($set1 as $key => $value) {
|
||||
$data[] = ['x' => $value, 'y' => $set2[$key]];
|
||||
}
|
||||
$dates = $this->dateRange(array_key_first($set1), array_key_last($set1));
|
||||
$data = array_map(fn($date) => ['x' => $date, 'y' => $set1[$date] ?? null], $dates);
|
||||
$stringData = json_encode($data);
|
||||
|
||||
return str_replace(['"x"', '"y"'], ['x', 'y'], $stringData);
|
||||
}
|
||||
|
||||
private function dateRange($dateStart, $dateEnd): array
|
||||
{
|
||||
$from = new \DateTime($dateStart);
|
||||
$to = new \DateTime($dateEnd);
|
||||
$range = [];
|
||||
for ($date = clone $from; $date < $to; $date->modify('+1 day')) {
|
||||
$range[] = $date->format('Y-m-d');
|
||||
}
|
||||
|
||||
return $range;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
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;
|
||||
@@ -16,10 +15,10 @@ final class PromosController extends BaseController
|
||||
return $this->render('productList.html.twig', ['listType' => 'promos']);
|
||||
}
|
||||
|
||||
$products = Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))
|
||||
$products = Product::whereRaw('priceCurrent < productStandardPrice')
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->with(['currentPrice'])
|
||||
->with(['currentPrice', 'lowestPrice'])
|
||||
->get();
|
||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos']);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,6 @@ final class SearchController extends BaseController
|
||||
->orWhere([['name', 'like', "%$search%"]])
|
||||
->orWhere([['subTitle', 'like', "%$search%"]])->get();
|
||||
|
||||
return $this->render('productList.html.twig', ['products' => $products ?? [], 'search' => $search]);
|
||||
return $this->render('productList.html.twig', ['products' => $products ?? [], 'search' => $search, 'listType' => 'search_'.$search]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,18 @@ final class StarController extends BaseController
|
||||
#[Route('/star/{productId<\d+>}', name: 'app_star')]
|
||||
public function __invoke(int $productId, Request $request): Response
|
||||
{
|
||||
$this->cache->deleteItems(['list_all', 'list_promos', 'list_new', 'list_discontinued']);
|
||||
$referer = $request->headers->get('referer');
|
||||
if (str_contains($referer, '/category/')) {
|
||||
preg_match('#/category/(.*)#i', $referer, $matches);
|
||||
$this->cache->deleteItem('list_category_'.urldecode($matches[1]));
|
||||
}
|
||||
if (str_contains($referer, '/search?search=')) {
|
||||
preg_match('#/search\?search=(.*)#i', $referer, $matches);
|
||||
$this->cache->deleteItem('list_search_'.urldecode($matches[1]));
|
||||
}
|
||||
|
||||
Product::find($productId)->toggleStarred()->save();
|
||||
return $this->redirect($request->headers->get('referer'));
|
||||
return $this->redirect($referer);
|
||||
}
|
||||
}
|
||||
|
||||
14
src/DatabaseFactory.php
Normal file
14
src/DatabaseFactory.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
class DatabaseFactory
|
||||
{
|
||||
public static function create(Capsule $capsule): void
|
||||
{
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/../database.sqlite']);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Krzysiej\RyobiCrawler;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager;
|
||||
use Krzysiej\RyobiCrawler\Twig\AppExtension;
|
||||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
|
||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
@@ -34,14 +35,13 @@ class Kernel extends BaseKernel
|
||||
'secret' => 'S0ME_SECRET'
|
||||
]);
|
||||
$services = $container->services()->defaults()->autowire()->autoconfigure();
|
||||
$services->load('Krzysiej\\RyobiCrawler\\Controller\\', __DIR__ . '/Controller/*');
|
||||
$services->load('Krzysiej\\RyobiCrawler\\Command\\', __DIR__ . '/Command/*')->tag('console.command');
|
||||
$services->set(Manager::class)->configurator([DatabaseFactory::class, 'create']);
|
||||
$services->load('Krzysiej\\RyobiCrawler\\', __DIR__ )
|
||||
->exclude('../src/{Models,Twig,DatabaseFactory.php,Kernel.php}');
|
||||
$services->set('twig.extension.cache', AppExtension::class)->tag('twig.extension');
|
||||
$services->set(CacheExtension::class)->tag('twig.extension');
|
||||
$services->set(FilesystemAdapter::class)->args([
|
||||
'', // namespace
|
||||
0, // default lifetime
|
||||
__DIR__ . '/../var/cache/twig_blocks' // custom path
|
||||
'$directory' => __DIR__ . '/../var/cache/twig_blocks'
|
||||
]);
|
||||
$services->set('twig.runtime.cache', CacheRuntime::class)->args([new Reference(FilesystemAdapter::class)])->tag('twig.runtime');
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Models;
|
||||
|
||||
use Carbon\Traits\Date;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
@@ -20,6 +21,11 @@ use function Symfony\Component\Clock\now;
|
||||
* @property string $modelCode
|
||||
* @property string $url
|
||||
* @property int $starred
|
||||
* @property float $priceCurrent
|
||||
* @property float $priceLowest
|
||||
* @property float $productStandardPrice
|
||||
* @property float $lowestProductPrice30Days
|
||||
* @property Date $lastSeen
|
||||
*/
|
||||
class Product extends Model
|
||||
{
|
||||
@@ -41,6 +47,15 @@ class Product extends Model
|
||||
return $this->hasOne(Price::class)->latestOfMany('created_at');
|
||||
}
|
||||
|
||||
public function lowestPrice(): HasOne
|
||||
{
|
||||
return $this->hasOne(Price::class)->ofMany('price', 'MIN');
|
||||
}
|
||||
public function newestPrice(): HasOne
|
||||
{
|
||||
return $this->hasOne(Price::class)->latest();
|
||||
}
|
||||
|
||||
public function stock(): HasMany
|
||||
{
|
||||
return $this->hasMany(Stock::class);
|
||||
@@ -48,9 +63,7 @@ class Product extends Model
|
||||
|
||||
public function currentStock(): HasOne
|
||||
{
|
||||
return $this->stock()->one()->ofMany()->withDefault(function (Stock $stock) {
|
||||
$stock->stock = 0;
|
||||
});
|
||||
return $this->stock()->one()->ofMany()->withDefault(fn (Stock $stock) => $stock->stock = 0);
|
||||
}
|
||||
|
||||
public function toggleStarred(): self
|
||||
@@ -70,7 +83,7 @@ class Product extends Model
|
||||
|
||||
public function isDiscontinued(): bool
|
||||
{
|
||||
return $this->updated_at->format('Y-m-d') < now()->format('Y-m-d');
|
||||
return $this->lastSeen < now()->format('Y-m-d');
|
||||
}
|
||||
public function isNew(): bool
|
||||
{
|
||||
|
||||
@@ -22,6 +22,7 @@ class AppExtension extends AbstractExtension
|
||||
new TwigFunction('allCount', [$this, 'allCount']),
|
||||
new TwigFunction('newCount', [$this, 'newCount']),
|
||||
new TwigFunction('discontinuedCount', [$this, 'discontinuedCount']),
|
||||
new TwigFunction('lowestPriceCount', [$this, 'lowestPriceCount']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -31,6 +32,7 @@ class AppExtension extends AbstractExtension
|
||||
new TwigFilter('findByCreatedAtDate', [$this, 'findByCreatedAtDate']),
|
||||
];
|
||||
}
|
||||
|
||||
public function allCount(): int
|
||||
{
|
||||
return Product::count();
|
||||
@@ -38,7 +40,7 @@ class AppExtension extends AbstractExtension
|
||||
|
||||
public function promosCount(): int
|
||||
{
|
||||
return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->count();
|
||||
return Product::whereRaw('priceCurrent < productStandardPrice')->count();
|
||||
}
|
||||
|
||||
public function newCount(): int
|
||||
@@ -48,7 +50,15 @@ class AppExtension extends AbstractExtension
|
||||
|
||||
public function discontinuedCount(): int
|
||||
{
|
||||
return Product::where('updated_at', '<', now()->format('Y-m-d'))->count();
|
||||
return Product::where('lastSeen', '<>', now()->format('Y-m-d'))->count();
|
||||
}
|
||||
|
||||
public function lowestPriceCount(): int
|
||||
{
|
||||
return Product::whereRaw('priceCurrent = priceLowest')
|
||||
->whereRaw('lastSeen = "'.now()->format('Y-m-d').'"')
|
||||
->whereRaw('priceCurrent < productStandardPrice')
|
||||
->count();
|
||||
}
|
||||
|
||||
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null
|
||||
|
||||
6
templates/css/bootstrap.min.css
vendored
6
templates/css/bootstrap.min.css
vendored
File diff suppressed because one or more lines are too long
1
templates/css/bootstrap.min.css.map
Normal file
1
templates/css/bootstrap.min.css.map
Normal file
File diff suppressed because one or more lines are too long
7
templates/js/bootstrap.bundle.min.js
vendored
Normal file
7
templates/js/bootstrap.bundle.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
templates/js/bootstrap.bundle.min.js.map
Normal file
1
templates/js/bootstrap.bundle.min.js.map
Normal file
File diff suppressed because one or more lines are too long
@@ -83,6 +83,7 @@
|
||||
]
|
||||
},
|
||||
options: {
|
||||
spanGaps: false,
|
||||
responsive: true,
|
||||
animation: false,
|
||||
scales: {
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
<th>Name</th>
|
||||
<th>Categories</th>
|
||||
<th></th>
|
||||
<th>Price</th>
|
||||
<th class="text-end">Lowest Price</th>
|
||||
<th class="text-end">Current Price</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -28,7 +29,7 @@
|
||||
<span class="badge text-bg-warning">out of stock</span>
|
||||
{% endif %}
|
||||
{% if product.isDiscontinued() %}
|
||||
<span class="badge text-bg-secondary" data-bs-toggle="tooltip" data-bs-title="Last update: {{ product.updated_at }}">is discontinued</span>
|
||||
<span class="badge text-bg-secondary" data-bs-toggle="tooltip" data-bs-title="Last update: {{ product.lastSeen }}">is discontinued</span>
|
||||
{% endif %}
|
||||
{% if product.isNew() %}
|
||||
<span class="badge text-bg-success">is new</span>
|
||||
@@ -37,7 +38,7 @@
|
||||
</td>
|
||||
<td class="align-middle">
|
||||
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';" >
|
||||
<ol class="breadcrumb">
|
||||
<ol class="breadcrumb mb-0">
|
||||
{% 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>
|
||||
{% endfor %}
|
||||
@@ -45,12 +46,19 @@
|
||||
</nav>
|
||||
</td>
|
||||
<td class="align-middle"><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></td>
|
||||
<td class="align-middle">{{ product.price.last.price | format_currency('PLN', {}, 'pl') }}</td>
|
||||
<td class="align-middle text-end">
|
||||
{% if product.isDiscontinued() or product.priceCurrent == product.productStandardPrice %}
|
||||
{{ product.priceLowest | format_currency('PLN', {}, 'pl') }}
|
||||
{% else %}
|
||||
{% if product.priceLowest != product.priceCurrent %}{{ product.priceLowest | format_currency('PLN', {}, 'pl') }}{%else%}<span class="badge text-bg-info">now lowest</span>{% endif %}</td>
|
||||
{% endif %}
|
||||
|
||||
<td class="align-middle text-end">{{ product.priceCurrent | format_currency('PLN', {}, 'pl') }}</td>
|
||||
<td class="align-middle">
|
||||
<div class="d-flex flex-row">
|
||||
{% if product.price.last.price != product.price.last.productStandardPrice %}<span
|
||||
class="badge text-bg-warning text-decoration-line-through flex-fill">{{ product.price.last.productStandardPrice | format_currency('PLN', {}, 'pl') }}</span> <span
|
||||
class="badge text-bg-success flex-fill">{{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%</span>
|
||||
{% if product.priceCurrent != product.productStandardPrice %}<span
|
||||
class="badge text-bg-warning text-decoration-line-through flex-fill">{{ product.productStandardPrice | format_currency('PLN', {}, 'pl') }}</span> <span
|
||||
class="badge text-bg-success flex-fill">{{ ((1 - product.priceCurrent / product.productStandardPrice)*100)|number_format(0) }}%</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
</td>
|
||||
|
||||
@@ -18,13 +18,16 @@
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
{% cache "menu_count" %}
|
||||
{% cache "menu_count" ~ listType|default('') %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {% if app.request.pathinfo == path('app_home') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_home') }}">All products <span class="badge text-bg-secondary">{{ allCount() }}</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {% if app.request.pathinfo == path('app_promos') %}active shadow-sm bg-body rounded{% endif %}" 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 {% if app.request.pathinfo == path('app_lowest_price') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_lowest_price') }}">Lowest price <span class="badge text-bg-secondary">{{ lowestPriceCount() }}</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {% if app.request.pathinfo == path('app_new') %}active shadow-sm bg-body rounded{% endif %}" aria-current="page" href="{{ path('app_new') }}">New in last 30 days <span class="badge text-bg-secondary">{{ newCount() }}</span></a>
|
||||
</li>
|
||||
@@ -44,8 +47,7 @@
|
||||
|
||||
</nav>
|
||||
{% block content %}{% endblock %}
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
||||
|
||||
<script src="/templates/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user