diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 96880d5..5e5dc7a 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -6,6 +6,7 @@ namespace Krzysiej\RyobiCrawler\Command; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Schema\Blueprint; +use Krzysiej\RyobiCrawler\Models\Country; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -93,26 +94,30 @@ class Migrate extends Command $table->timestamps(); }); } - $id = Capsule::table('countries')->insertGetId( - [ - 'countryName' => 'Poland', - 'productsUrl' => 'https://pl.ryobitools.eu/api/product-listing/get-products', - 'cultureCode' => 'pl-PL', - 'currency' => 'PLN', - 'locale' => 'pl', - 'created_at' => now(), - 'updated_at' => now(), - ]); - Capsule::table('countries')->insert([ - 'countryName' => 'UK', - 'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products', - 'cultureCode' => 'en-GB', - 'currency' => 'GBP', - 'locale' => 'en', - 'created_at' => now(), - 'updated_at' => now(), - ] - ); + if (Capsule::schema()->hasTable('countries') && !Country::where('countryName', 'Poland')->exists()) { + $id = Capsule::table('countries')->insertGetId( + [ + 'countryName' => 'Poland', + 'productsUrl' => 'https://pl.ryobitools.eu/api/product-listing/get-products', + 'cultureCode' => 'pl-PL', + 'currency' => 'PLN', + 'locale' => 'pl', + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + if (Capsule::schema()->hasTable('countries') && !Country::where('countryName', 'UK')->exists()) { + Capsule::table('countries')->insert([ + 'countryName' => 'UK', + 'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products', + 'cultureCode' => 'en-GB', + 'currency' => 'GBP', + 'locale' => 'en', + 'created_at' => now(), + 'updated_at' => now(), + ] + ); + } if (!Capsule::schema()->hasColumn('products', 'country_id')) { Capsule::schema()->table('products', function (Blueprint $table) use ($id) { diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 9993707..f68342e 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -113,7 +113,7 @@ class ScrapeWebsite extends Command $productModel->skuID = $product->skuID; $productModel->name = $product->name; $productModel->availableQuantity = $product->availableQuantity; - $productModel->categories = $product->categories; + $productModel->categories = str_replace(['/', ' '], '', $product->categories); $productModel->image = $product->image; $productModel->subTitle = $product->subTitle; $productModel->variantCode = $product->variantCode; diff --git a/src/Controller/NewController.php b/src/Controller/NewController.php index cf1f3ee..16d875b 100644 --- a/src/Controller/NewController.php +++ b/src/Controller/NewController.php @@ -16,11 +16,11 @@ final class NewController extends BaseController if($this->cache->getItem('list_new')->isHit()) { return $this->render('productList.html.twig', ['listType' => 'new']); } - - $products = Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d')) + $date = now()->modify('-30 days')->format('Y-m-d'); + $products = Product::where('created_at', '>', $date) + ->with(['country', 'stock']) ->orderByDesc('starred') ->orderByDesc('created_by') - ->with(['currentPrice', 'lowestPrice']) ->get(); return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'new']); } diff --git a/src/Models/Product.php b/src/Models/Product.php index 139ddf9..eadb99c 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -31,7 +31,7 @@ use function Symfony\Component\Clock\now; class Product extends Model { public $timestamps = true; - public $fillable = ['skuID']; + public $fillable = ['skuID', 'country_id']; public function country(): BelongsTo { diff --git a/templates/product.html.twig b/templates/product.html.twig index a6ce4dc..bbf3147 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -10,7 +10,7 @@ {{ product.name }} {{ product.name }} - {{ product.subTitle }} + {{ product.subTitle }}