diff --git a/browser.php b/browser.php index e429948..7c93009 100644 --- a/browser.php +++ b/browser.php @@ -3,6 +3,8 @@ include_once 'vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as Capsule; +use Krzysiej\RyobiCrawler\Models\Price; +use Krzysiej\RyobiCrawler\Models\Product; $capsule = new Capsule; $capsule->addConnection([ @@ -11,29 +13,34 @@ $capsule->addConnection([ 'prefix' => '', ]); $capsule->setAsGlobal(); +$capsule->bootEloquent(); echo ''; echo ""; if (isset($_GET['product_id'])) { - $products = Capsule::table('product')->leftJoin('price', 'product.id', '=', 'price.product_id')->where('product.id', '=', $_GET['product_id'])->get(); - foreach ($products as $product) { - echo " + $product = Product::with('price')->find($_GET['product_id']); + echo " - - - + "; + /** @var Price $price */ + foreach ($product->price as $price) { + echo " + + + + "; } } else { - $products = Capsule::table('product')->get(); + $products = Product::with('price')->get(); foreach ($products as $product) { echo " - + "; } } diff --git a/composer.json b/composer.json index c34b066..5245765 100644 --- a/composer.json +++ b/composer.json @@ -4,5 +4,10 @@ "symfony/var-dumper": "^7.0", "illuminate/database": "^11.0", "ext-json": "*" + }, + "autoload": { + "psr-4": { + "Krzysiej\\RyobiCrawler\\": "src/" + } } } diff --git a/index.php b/index.php index 027f5a7..c00fee3 100644 --- a/index.php +++ b/index.php @@ -3,6 +3,8 @@ include_once 'vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as Capsule; +use Krzysiej\RyobiCrawler\Models\Price; +use Krzysiej\RyobiCrawler\Models\Product; if (php_sapi_name() !== 'cli') { echo 'Execute this script in cli only'; @@ -14,6 +16,7 @@ $capsule->addConnection([ 'database' => __DIR__ . '/database.sqlite', ]); $capsule->setAsGlobal(); +$capsule->bootEloquent(); $client = new GuzzleHttp\Client(); $page = 0; do { @@ -29,29 +32,27 @@ do { $responseObject = json_decode($res->getBody()->getContents()); $products = $responseObject->products; foreach ($products as $product) { - Capsule::table('product')->updateOrInsert([ - 'skuID' => $product->skuID, - ], [ - 'name' => $product->name, - 'availableQuantity' => $product->availableQuantity, - 'stock' => $product->stock, - 'categories' => json_encode($product->categories), - 'image' => $product->image, - 'subTitle' => $product->subTitle, - 'variantCode' => $product->variantCode, - 'modelCode' => $product->modelCode, - 'url' => $product->url, - 'updated_at' => date('Y-m-d'), - ]); - $databaseProduct = Capsule::table('product')->where('skuID', '=', $product->skuID)->first(); - Capsule::table('price')->updateOrInsert([ - 'product_id' => $databaseProduct->id, - 'created_at' => date('Y-m-d'), - ], [ - 'price' => $product->productPrice, - 'productStandardPrice' => $product->productStandardPrice, - 'lowestProductPrice30Days' => $product->lowestProductPrice30Days, - ]); + /** @var Product $productModel */ + $productModel = Product::firstOrNew(['skuID' => $product->skuID]); + $productModel->skuID = $product->skuID; + $productModel->name = $product->name; + $productModel->availableQuantity = $product->availableQuantity; + $productModel->stock = $product->stock; + $productModel->categories = json_encode($product->categories); + $productModel->image = $product->image; + $productModel->subTitle = $product->subTitle; + $productModel->variantCode = $product->variantCode; + $productModel->modelCode = $product->modelCode; + $productModel->url = $product->url; + $productModel->save(); + $priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); + if (!$priceExists) { + $price = new Price(); + $price->price = $product->productPrice; + $price->productStandardPrice = $product->productStandardPrice; + $price->lowestProductPrice30Days = $product->lowestProductPrice30Days; + $productModel->price()->save($price); + } echo "."; } echo "\n"; diff --git a/migration.php b/migration.php index 5716717..4326cec 100644 --- a/migration.php +++ b/migration.php @@ -17,11 +17,12 @@ $capsule->addConnection([ ]); $capsule->setAsGlobal(); -if (!Capsule::schema()->hasTable('product')) { - Capsule::schema()->create('product', function (Blueprint $table) { +if (!Capsule::schema()->hasTable('products')) { + Capsule::schema()->create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); - $table->string('skuID'); + $table->integer('skuID')->unique(); + $table->integer('agilityID'); $table->integer('availableQuantity'); $table->integer('stock'); $table->json('categories'); @@ -35,8 +36,8 @@ if (!Capsule::schema()->hasTable('product')) { } -if (!Capsule::schema()->hasTable('price')) { - Capsule::schema()->create('price', function (Blueprint $table) { +if (!Capsule::schema()->hasTable('prices')) { + Capsule::schema()->create('prices', function (Blueprint $table) { $table->increments('id'); $table->foreignId('product_id'); $table->float('price'); diff --git a/src/Models/Price.php b/src/Models/Price.php new file mode 100644 index 0000000..a80c94a --- /dev/null +++ b/src/Models/Price.php @@ -0,0 +1,21 @@ +belongsTo(Product::class); + } +} \ No newline at end of file diff --git a/src/Models/Product.php b/src/Models/Product.php new file mode 100644 index 0000000..c477d0b --- /dev/null +++ b/src/Models/Product.php @@ -0,0 +1,29 @@ +hasMany(Price::class); + } +} \ No newline at end of file
$product->name $product->name $product->subTitlelink$product->price$product->created_atlink
$price->price$price->lowestProductPrice30Days$price->productStandardPrice$price->created_at
$product->name $product->name $product->subTitle link$product->price{$product->price->last()->price}