From 46065dae1a8399377e57fa07ed5cfdbb8d6e6024 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Mon, 19 Jan 2026 15:08:53 +0100 Subject: [PATCH] Add stock to product. --- src/Command/Migrate.php | 16 ++++++++++++++-- src/Command/ScrapeWebsite.php | 6 ++---- src/Models/Product.php | 1 + 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 96880d5..ac5dd1a 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -170,13 +170,25 @@ class Migrate extends Command $table->date('lastSeen')->nullable(); }); } + + if (!Capsule::schema()->hasColumn('products', 'stock')) { + Capsule::schema()->table('products', function (Blueprint $table) { + $table->integer('stock')->nullable(); + }); + } } public function index(): void { + if (!count(Capsule::select('SELECT name FROM sqlite_master WHERE type = "index" and name = "products_skuid_country_id_unique"'))) { + Capsule::schema()->table('products', function (Blueprint $table) { + $table->integer('skuID')->unique(false)->change(); + $table->unique(['skuID', 'country_id']); + }); + } + Capsule::schema()->table('products', function (Blueprint $table) { - $table->integer('skuID')->unique(false)->change(); - $table->unique(['skuID', 'country_id']); + $table->foreign('id')->references('product_id')->on('stocks'); }); } } diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 9993707..81c7d48 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -58,11 +58,13 @@ class ScrapeWebsite extends Command $progress->start(); foreach($products as $product) { $newestPrice = $product->newestPrice; + $currentStock = $product->currentStock; $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->stock = $currentStock->stock; $product->save(['timestamps' => false]); $progress->advance(); } @@ -103,10 +105,6 @@ class ScrapeWebsite extends Command private function saveProduct(\stdClass $product, Country $country): void { -// if ($product->skuID == 0) { -// dump([$product->skuID, $product->name]); -// } -// return; /** @var Product $productModel */ $productModel = Product::firstOrNew(['skuID' => $product->skuID, 'country_id' => $country->id]); diff --git a/src/Models/Product.php b/src/Models/Product.php index 139ddf9..5a27bb8 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -27,6 +27,7 @@ use function Symfony\Component\Clock\now; * @property float $productStandardPrice * @property float $lowestProductPrice30Days * @property Date $lastSeen + * @property integer $stock */ class Product extends Model {