4 Commits

Author SHA1 Message Date
67019e3933 Add indexes to the database. 2026-02-05 08:40:34 +01:00
a545bfe2ab feature/handle-promotions (#59)
Reviewed-on: #59
Co-authored-by: Krzysiej <krzysiej@gmail.com>
Co-committed-by: Krzysiej <krzysiej@gmail.com>
2026-02-04 08:35:16 +01:00
6556ba0f88 Fix links to products. 2026-01-28 08:33:22 +01:00
6da4f4257e Fix links to products. 2026-01-28 08:31:41 +01:00
7 changed files with 104 additions and 19 deletions

View File

@@ -112,7 +112,7 @@ class Migrate extends Command
'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products', 'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products',
'cultureCode' => 'en-GB', 'cultureCode' => 'en-GB',
'currency' => 'GBP', 'currency' => 'GBP',
'locale' => 'en', 'locale' => 'uk',
'created_at' => now(), 'created_at' => now(),
'updated_at' => now(), 'updated_at' => now(),
] ]
@@ -181,19 +181,72 @@ class Migrate extends Command
$table->integer('stock')->nullable(); $table->integer('stock')->nullable();
}); });
} }
if (!Capsule::schema()->hasColumn('products', 'promotions')) {
Capsule::schema()->table('products', function (Blueprint $table) {
$table->json('promotions')->nullable();
});
}
} }
public function index(): void public function index(): void
{ {
if (!count(Capsule::select('SELECT name FROM sqlite_master WHERE type = "index" and name = "products_skuid_country_id_unique"'))) { if (!$this->hasIndex('products_skuid_country_id_unique')) {
Capsule::schema()->table('products', function (Blueprint $table) { Capsule::schema()->table('products', function (Blueprint $table) {
if ($this->hasIndex('products_skuid_unique')) {
$table->integer('skuID')->unique(false)->change(); $table->integer('skuID')->unique(false)->change();
}
$table->unique(['skuID', 'country_id']); $table->unique(['skuID', 'country_id']);
}); });
} }
if (!$this->hasIndex('prices_product_id_index')) {
Capsule::schema()->table('prices', function (Blueprint $table) {
$table->index('product_id');
});
}
if (!$this->hasIndex('products_id_index')) {
Capsule::schema()->table('products', function (Blueprint $table) {
$table->index('id');
});
}
if (!$this->hasIndex('stocks_product_id_stock_index')) {
Capsule::schema()->table('stocks', function (Blueprint $table) {
$table->index(['product_id', 'stock']);
});
}
if (!$this->isFK('products', 'id', 'stocks', 'product_id')) {
Capsule::schema()->table('products', function (Blueprint $table) { Capsule::schema()->table('products', function (Blueprint $table) {
$table->foreign('id')->references('product_id')->on('stocks'); $table->foreign('id')->references('product_id')->on('stocks');
}); });
} }
if (!$this->isFK('products', 'id', 'prices', 'product_id')) {
Capsule::schema()->table('products', function (Blueprint $table) {
$table->foreign('id')->references('product_id')->on('prices');
});
}
if (!$this->isFK('products', 'country_id', 'countries', 'id')) {
Capsule::schema()->table('products', function (Blueprint $table) {
$table->foreign('country_id')->references('id')->on('countries');
});
}
}
private function isFK(string $table, string $column, string $fTable, string $fColumn): bool
{
$fkColumns = Capsule::schema()->getForeignKeys($table);
return !empty(array_filter($fkColumns, fn($fkColumn) => ($fkColumn['foreign_table'] == $fTable &&
in_array($fColumn, $fkColumn['foreign_columns']) &&
in_array($column, $fkColumn['columns']))
));
}
private function hasIndex(string $indexName): bool
{
return !!count(Capsule::select('SELECT name FROM sqlite_master WHERE type = "index" and name = ?', [$indexName]));
}
} }

View File

@@ -38,7 +38,7 @@ class ScrapeWebsite extends Command
$progress = new ProgressBar($output); $progress = new ProgressBar($output);
$countries = Country::all(); $countries = Country::all();
foreach($countries as $country) { foreach($countries as $country) {
$output->writeln('Country name: ' . $country->countryName."\n"); $output->writeln('Country name: ' . $country->countryName);
$progress->start(); $progress->start();
$products = $this->getProducts($country); $products = $this->getProducts($country);
$progress->setMaxSteps(count($products)); $progress->setMaxSteps(count($products));
@@ -120,6 +120,7 @@ class ScrapeWebsite extends Command
$productModel->lastSeen = date("Y-m-d"); $productModel->lastSeen = date("Y-m-d");
$productModel->touch('updated_at'); $productModel->touch('updated_at');
$productModel->country()->associate($country); $productModel->country()->associate($country);
$productModel->promotions = $product->promotions;
$productModel->save(); $productModel->save();
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); $priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();

View File

@@ -14,11 +14,11 @@ final class IndexController extends BaseController
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']) $products = Product::orderByDesc('starred')
->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->get(); ->get();
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'all']); return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'all']);
} }
} }

View File

@@ -8,18 +8,25 @@ use Symfony\Component\Routing\Attribute\Route;
final class PromosController extends BaseController final class PromosController extends BaseController
{ {
#[Route('/promos', name: 'app_promos')] #[Route('/promos/{promo?}', name: 'app_promos')]
public function __invoke(): Response public function __invoke(?string $promo): Response
{ {
if ($this->cache->getItem('list_promos')->isHit()) { if ($this->cache->getItem('list_promos')->isHit()) {
return $this->render('productList.html.twig', ['listType' => 'promos']); return $this->render('productList.html.twig', ['listType' => 'promos' . $promo]);
} }
$products = Product::whereRaw('priceCurrent < productStandardPrice') $products = Product::when(is_null($promo), fn($q) => $q->whereRaw('priceCurrent < productStandardPrice'))
->orderByDesc('starred') ->orderByDesc('starred')
->orderByDesc('created_by') ->orderByDesc('created_by')
->with(['currentPrice', 'lowestPrice']) ->with(['currentPrice', 'lowestPrice'])
->when(!is_null($promo), fn($q) => $q->whereRaw("json_extract(promotions, '$.slug') LIKE ?", $promo))
->get(); ->get();
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos']);
$promos = Product::select($this->database->getConnection()->raw("distinct json_extract(promotions, '$.slug') as slug, json_extract(promotions, '$.tag') as tag"))
->whereRaw("json_extract(promotions, '$.tag') is not null")
->get();
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos' . $promo, 'promos' => $promos->toArray()]);
} }
} }

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
use function Symfony\Component\Clock\now; use function Symfony\Component\Clock\now;
/** /**
@@ -28,6 +29,7 @@ use function Symfony\Component\Clock\now;
* @property float $lowestProductPrice30Days * @property float $lowestProductPrice30Days
* @property Date $lastSeen * @property Date $lastSeen
* @property integer $stock * @property integer $stock
* @property Object $promotions
*/ */
class Product extends Model class Product extends Model
{ {
@@ -58,9 +60,10 @@ class Product extends Model
{ {
return $this->hasOne(Price::class)->ofMany('price', 'MIN'); return $this->hasOne(Price::class)->ofMany('price', 'MIN');
} }
public function newestPrice(): HasOne public function newestPrice(): HasOne
{ {
return $this->hasOne(Price::class)->latest(); return $this->hasOne(Price::class)->latest()->take(1);
} }
public function stock(): HasMany public function stock(): HasMany
@@ -70,7 +73,7 @@ class Product extends Model
public function currentStock(): HasOne public function currentStock(): HasOne
{ {
return $this->stock()->one()->ofMany()->withDefault(fn (Stock $stock) => $stock->stock = 0); return $this->stock()->one()->ofMany()->withDefault(fn(Stock $stock) => $stock->stock = 0)->take(1);
} }
public function toggleStarred(): self public function toggleStarred(): self
@@ -88,10 +91,23 @@ class Product extends Model
); );
} }
public function promotions(): Attribute
{
return Attribute::make(
get: fn(?string $value) => json_decode($value ?? '{"hasPromotion": false}', 1),
set: function (\stdClass $value) {
$value->slug = Str::slug($value->tag);
return json_encode($value);
}
);
}
public function isDiscontinued(): bool public function isDiscontinued(): bool
{ {
return $this->lastSeen < now()->format('Y-m-d'); return $this->lastSeen < now()->format('Y-m-d');
} }
public function isNew(): bool public function isNew(): bool
{ {
return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d'); return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d');

View File

@@ -11,6 +11,7 @@
<td> <td>
<a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a> <a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a>
<span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}" class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span> <span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}" class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
</td> </td>
<td> <td>
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';"> <nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">
@@ -22,7 +23,7 @@
</ol> </ol>
</nav> </nav>
</td> </td>
<td><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></td> <td><a href='https://{{ product.country.locale }}.ryobitools.eu{{ product.url }}'>link</a></td>
</tr> </tr>
<tr> <tr>
<td colspan="5"> <td colspan="5">

View File

@@ -7,6 +7,12 @@
{{ renderCategoryTree(categoryTree, category) | raw }} {{ renderCategoryTree(categoryTree, category) | raw }}
{% endif %} {% endif %}
{% if listType starts with 'promos' %}
{% for promo in promos %}
<a href="{{ path('app_promos', {'promo': promo.slug}) }}"><span class="badge bg-info">PROMO: {{ promo.tag }}</span></a>
{% endfor %}
{% endif %}
{% if (listType starts with 'category_' and category == null) or not (listType starts with 'category_') or (listType starts with 'category_' and category is not null) %} {% if (listType starts with 'category_' and category == null) or not (listType starts with 'category_') or (listType starts with 'category_' and category is not null) %}
<div class="table-responsive"> <div class="table-responsive">
@@ -49,6 +55,7 @@
<span class="badge text-bg-light"><a <span class="badge text-bg-light"><a
href="{{ path('app_search', {'search': product.subTitle}) }}" href="{{ path('app_search', {'search': product.subTitle}) }}"
class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span> class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
</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: '>';">
@@ -62,7 +69,7 @@
</ol> </ol>
</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://{{ product.country.locale }}.ryobitools.eu{{ product.url }}'>link</a></td>
<td class="align-middle text-end"> <td class="align-middle text-end">
{% if product.isDiscontinued() or product.priceCurrent == product.productStandardPrice %} {% if product.isDiscontinued() or product.priceCurrent == product.productStandardPrice %}
{{ product.priceLowest | format_currency(product.country.currency, {}, product.country.locale) }} {{ product.priceLowest | format_currency(product.country.currency, {}, product.country.locale) }}