diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 2ece48c..cea7a04 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -51,7 +51,6 @@ class Migrate extends Command $table->string('name'); $table->integer('skuID')->unique(); $table->integer('availableQuantity'); - $table->integer('stock'); $table->json('categories'); $table->string('image'); $table->string('subTitle'); diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index b931a42..11bfbdd 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -69,7 +69,7 @@ class ScrapeWebsite extends Command $products = array_merge($products, $responseObject->products); $page++; $canLoadMore = $responseObject->canLoadMore; - } catch (GuzzleException $e) { + } catch (GuzzleException) { return $products; } } while ($canLoadMore); @@ -81,12 +81,10 @@ class ScrapeWebsite extends Command { /** @var Product $productModel */ $productModel = Product::firstOrNew(['skuID' => $product->skuID]); - //dd($productModel); $productModel->skuID = $product->skuID; $productModel->name = $product->name; $productModel->availableQuantity = $product->availableQuantity; - $productModel->stock = $product->stock; $productModel->categories = $product->categories; $productModel->image = $product->image; $productModel->subTitle = $product->subTitle; diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php index 042cd89..20ebb4b 100644 --- a/src/Controller/ProductController.php +++ b/src/Controller/ProductController.php @@ -23,7 +23,6 @@ final class ProductController extends BaseController 'price' => fn($query) => $query->orderBy('created_at', 'desc'), 'stock' => fn($query) => $query->orderBy('created_at', 'desc'), ])->find($productId); - if(null === $product) { throw $this->createNotFoundException('Product not found'); } diff --git a/src/Models/Product.php b/src/Models/Product.php index 8537677..54fd545 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -11,7 +11,6 @@ use Illuminate\Database\Eloquent\Relations\HasOne; * @property integer $skuID * @property string $name * @property integer $availableQuantity - * @property integer $stock * @property string[] $categories * @property string $image * @property string $subTitle diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index fc6216f..0376fbd 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -3,21 +3,37 @@ namespace Krzysiej\RyobiCrawler\Twig; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; +use Krzysiej\RyobiCrawler\Models\Price; use Krzysiej\RyobiCrawler\Models\Product; +use Krzysiej\RyobiCrawler\Models\Stock; use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; use Twig\TwigFunction; class AppExtension extends AbstractExtension { - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('promosCount', [$this, 'promosCount']), ]; } + public function getFilters(): array + { + return [ + new TwigFilter('findByCreatedAtDate', [$this, 'findByCreatedAtDate']), + ]; + } + public function promosCount(): int { return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count(); } + + public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null + { + return $items->first(fn($item) => str_starts_with($item->created_at, $date)); + } } \ No newline at end of file diff --git a/templates/product.html.twig b/templates/product.html.twig index 3bfd197..c52bfd8 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -32,6 +32,7 @@ price lowest product price in 30 days standard price + Stock {% for price in product.price %} @@ -40,6 +41,7 @@ {{ price.lowestProductPrice30Days | format_currency('PLN', {}, 'pl') }} {{ price.productStandardPrice | format_currency('PLN', {}, 'pl') }} {{ price.created_at }} + {{ (product.stock | findByCreatedAtDate(price.created_at | slice(0,10))).stock ?? '' }} {% endfor %}