5 Commits

Author SHA1 Message Date
dc287aadc6 Merge pull request 'feature/lowest-price' (#27) from feature/lowest-price into master
Reviewed-on: #27
2025-05-14 12:46:12 +02:00
708a5eeae0 Merge pull request 'Implement a caching mechanism' (#28) from feature/cache into master
Reviewed-on: #28
2025-05-14 12:45:57 +02:00
Krzysztof Płaczek
7352eea270 Add the lowest price column to every product list view 2025-05-14 10:28:58 +02:00
Krzysztof Płaczek
e97d976705 Implement a caching mechanism 2025-05-14 09:03:25 +02:00
beb717f9b9 Add lowest price first draft. 2025-05-13 23:34:51 +02:00
10 changed files with 21 additions and 16 deletions

View File

@@ -4,7 +4,7 @@
1. Clone repository using `git clone https://git.techtube.pl/krzysiej/ryobi-crawler.git` 1. Clone repository using `git clone https://git.techtube.pl/krzysiej/ryobi-crawler.git`
2. Cd into project directory `cd ryobi-crawler` 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. 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. 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. 6. Access web interface using `localhost:9001` address in web browser.

View File

@@ -42,8 +42,8 @@ class CacheWarmCommand extends Command
$progress = new ProgressBar($output); $progress = new ProgressBar($output);
$progress->start(); $progress->start();
$products = Product::with([ $products = Product::with([
'price' => fn($query) => $query->orderBy('created_at', 'desc'), 'price' => fn($query) => $query->orderByDesc('created_at'),
'stock' => fn($query) => $query->orderBy('created_at', 'desc'), 'stock' => fn($query) => $query->orderByDesc('created_at'),
])->get(); ])->get();
$progress->setMaxSteps(count($products)); $progress->setMaxSteps(count($products));

View File

@@ -15,7 +15,7 @@ final class CategoryController extends BaseController
return $this->render('productList.html.twig', ['listType' => 'category_'.$category]); return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
} }
$products = Product::with('price') $products = Product::with(['price', 'lowestPrice'])
->selectRaw('products.*') ->selectRaw('products.*')
->distinct('products.id') ->distinct('products.id')
->fromRaw('products, json_each(products.categories)') ->fromRaw('products, json_each(products.categories)')

View File

@@ -20,7 +20,7 @@ final class DiscontinuedController extends BaseController
$products = Product::where('updated_at', '<', now()->format('Y-m-d')) $products = Product::where('updated_at', '<', now()->format('Y-m-d'))
->orderByDesc('starred') ->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->with(['currentPrice']) ->with(['currentPrice', 'lowestPrice'])
->get(); ->get();
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'discontinued']); return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'discontinued']);
} }

View File

@@ -11,10 +11,10 @@ final class IndexController extends BaseController
#[Route('/', name: 'app_home')] #[Route('/', name: 'app_home')]
public function __invoke(): Response 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']); return $this->render('productList.html.twig', ['listType' => 'all']);
} }
$products = Product::with(['currentStock', 'price']) $products = Product::with(['currentStock', 'price', 'lowestPrice'])
->orderByDesc('starred') ->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->get(); ->get();

View File

@@ -21,7 +21,7 @@ final class NewController extends BaseController
$products = Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d')) $products = Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d'))
->orderByDesc('starred') ->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->with(['currentPrice']) ->with(['currentPrice', 'lowestPrice'])
->get(); ->get();
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'new']); return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'new']);
} }

View File

@@ -19,7 +19,7 @@ final class PromosController extends BaseController
$products = Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice')) $products = Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))
->orderByDesc('starred') ->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->with(['currentPrice']) ->with(['currentPrice', 'lowestPrice'])
->get(); ->get();
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos']); return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos']);
} }

View File

@@ -34,14 +34,12 @@ class Kernel extends BaseKernel
'secret' => 'S0ME_SECRET' 'secret' => 'S0ME_SECRET'
]); ]);
$services = $container->services()->defaults()->autowire()->autoconfigure(); $services = $container->services()->defaults()->autowire()->autoconfigure();
$services->load('Krzysiej\\RyobiCrawler\\Controller\\', __DIR__ . '/Controller/*'); $services->load('Krzysiej\\RyobiCrawler\\', __DIR__ )
$services->load('Krzysiej\\RyobiCrawler\\Command\\', __DIR__ . '/Command/*')->tag('console.command'); ->exclude('../src/{Models,Twig,Kernel.php}');
$services->set('twig.extension.cache', AppExtension::class)->tag('twig.extension'); $services->set('twig.extension.cache', AppExtension::class)->tag('twig.extension');
$services->set(CacheExtension::class)->tag('twig.extension'); $services->set(CacheExtension::class)->tag('twig.extension');
$services->set(FilesystemAdapter::class)->args([ $services->set(FilesystemAdapter::class)->args([
'', // namespace '$directory' => __DIR__ . '/../var/cache/twig_blocks'
0, // default lifetime
__DIR__ . '/../var/cache/twig_blocks' // custom path
]); ]);
$services->set('twig.runtime.cache', CacheRuntime::class)->args([new Reference(FilesystemAdapter::class)])->tag('twig.runtime'); $services->set('twig.runtime.cache', CacheRuntime::class)->args([new Reference(FilesystemAdapter::class)])->tag('twig.runtime');
} }

View File

@@ -41,6 +41,11 @@ class Product extends Model
return $this->hasOne(Price::class)->latestOfMany('created_at'); return $this->hasOne(Price::class)->latestOfMany('created_at');
} }
public function lowestPrice(): HasOne
{
return $this->hasOne(Price::class)->ofMany('price', 'MIN');
}
public function stock(): HasMany public function stock(): HasMany
{ {
return $this->hasMany(Stock::class); return $this->hasMany(Stock::class);

View File

@@ -11,7 +11,8 @@
<th>Name</th> <th>Name</th>
<th>Categories</th> <th>Categories</th>
<th></th> <th></th>
<th>Price</th> <th class="text-end">Lowest Price</th>
<th class="text-end">Current Price</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@@ -45,7 +46,8 @@
</nav> </nav>
</td> </td>
<td class="align-middle"><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></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.lowestPrice.price != product.price.last.price %}{{ product.lowestPrice.price | format_currency('PLN', {}, 'pl') }}{% endif %}</td>
<td class="align-middle text-end">{{ product.price.last.price | format_currency('PLN', {}, 'pl') }}</td>
<td class="align-middle"> <td class="align-middle">
<div class="d-flex flex-row"> <div class="d-flex flex-row">
{% if product.price.last.price != product.price.last.productStandardPrice %}<span {% if product.price.last.price != product.price.last.productStandardPrice %}<span