diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 434e768..468aad7 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -246,6 +246,16 @@ class Migrate extends Command $table->json('promotions')->nullable(); }); } + if (!Capsule::schema()->hasColumn('prices', 'conversionRate')) { + Capsule::schema()->table('prices', function (Blueprint $table) { + $table->float('conversionRate')->nullable(); + }); + } + if (!Capsule::schema()->hasColumn('products', 'conversionRate')) { + Capsule::schema()->table('products', function (Blueprint $table) { + $table->float('conversionRate')->nullable(); + }); + } } public function index(): void diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index fa7b719..dffd416 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -23,13 +23,13 @@ class ScrapeWebsite extends Command { const COUNTRY_ID = 'country'; private Client $client; + private array $rates; public function __construct(protected Capsule $database) { parent::__construct(); $this->client = new Client(); - $this->getCurrencyExchange(); - die(); + $this->rates = $this->getCurrencyExchange(); } protected function configure(): void @@ -70,6 +70,7 @@ class ScrapeWebsite extends Command $product->priceLowest = $product->lowestPrice->price; $product->lastSeen = $newestPrice->created_at->format('Y-m-d'); $product->stock = $currentStock->stock; + $product->conversionRate = $newestPrice->conversionRate; $product->save(['timestamps' => false]); $progress->advance(); } @@ -100,7 +101,7 @@ class ScrapeWebsite extends Command $products = array_merge($products, $responseObject->products); $page++; $canLoadMore = $responseObject->canLoadMore; - } catch (GuzzleException $e) { + } catch (GuzzleException) { return $products; } } while ($canLoadMore); @@ -134,6 +135,7 @@ class ScrapeWebsite extends Command $price->price = $product->productPrice; $price->productStandardPrice = $product->productStandardPrice; $price->lowestProductPrice30Days = $product->lowestProductPrice30Days; + $price->conversionRate = $this->getConversionRate($country->currency); $productModel->price()->save($price); } $stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); @@ -150,11 +152,21 @@ class ScrapeWebsite extends Command } } - public function getCurrencyExchange(): void { - + public function getCurrencyExchange(): array + { $result = $this->client->request('GET', 'https://api.nbp.pl/api/exchangerates/tables/A/?format=json'); - dd(json_decode($result->getBody()->getContents())); + $rates = ['PLN' => 1.0]; + foreach(json_decode($result->getBody()->getContents(),true)[0]['rates'] as $rate){ + $rates[$rate['code']] = $rate['mid']; + } + return $rates; + } + private function getConversionRate(string $currency): float + { + $currency = strtoupper($currency); + + return $this->rates[$currency]; } } diff --git a/src/Models/Price.php b/src/Models/Price.php index a80c94a..9c4d75c 100644 --- a/src/Models/Price.php +++ b/src/Models/Price.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property float $price * @property float $productStandardPrice * @property float $lowestProductPrice30Days + * @property float $conversionRate */ class Price extends Model { diff --git a/src/Models/Product.php b/src/Models/Product.php index 3f5d456..64b03a9 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -27,6 +27,7 @@ use function Symfony\Component\Clock\now; * @property float $priceLowest * @property float $productStandardPrice * @property float $lowestProductPrice30Days + * @property float $conversionRate * @property Date $lastSeen * @property integer $stock * @property Object $promotions