diff --git a/src/Controller/CountryController.php b/src/Controller/CountryController.php new file mode 100644 index 0000000..218e154 --- /dev/null +++ b/src/Controller/CountryController.php @@ -0,0 +1,40 @@ +join('countries', 'countries.id', '=', 'products.country_id') + ->where('countryName', $countryName) + ->orderByDesc('starred') + ->orderByDesc('created_by') + ->get(); + + $country = Country::where('countryName', $countryName)->first(); + + return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'country', 'country' => $country, 'countryStats' => $this->countryStats($countryName)]); + } + + private function countryStats(string $country): array + { + $countryProducts = Product::join('countries', 'countries.id', '=', 'products.country_id') + ->where('countryName', $country); + + $stats = []; + $stats['items'] = $countryProducts->get()->count(); +// $stats['items2'] = $countryProducts->dd(); + + return $stats; + } +} diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php index 408b0c7..4b5faf2 100644 --- a/src/Controller/ProductController.php +++ b/src/Controller/ProductController.php @@ -32,6 +32,32 @@ final class ProductController extends BaseController ksort($stockList); ksort($priceList); + return $this->render('product.html.twig', [ + 'product' => $product, + 'price_list' => $this->prepareChartData($priceList), + 'stock_list' => $this->prepareChartData($stockList), + 'price_dates' => implode("','", $this->dateRange(array_key_first($priceList), array_key_last($priceList))), + ]); + } + #[Route('/product/model/{productModel}', name: 'app_product_model')] + public function productModel(string $productModel): Response + { + + $product = Product::with([ + 'price' => fn($query) => $query->orderBy('created_at', 'desc'), + 'stock' => fn($query) => $query->orderBy('created_at', 'desc'), + ])->where('subTitle', $productModel)->first(); + + if (null === $product) { + throw $this->createNotFoundException('Product not found'); + } + $priceList = $product->price()->pluck('price', 'created_at')->mapWithKeys(fn($price, $createdAt) => [explode(' ', $createdAt)[0] => $price])->toArray(); + $stockList = $product->stock()->pluck('stock', 'created_at')->mapWithKeys(fn($stock, $createdAt) => [explode(' ', $createdAt)[0] => $stock])->toArray(); + + ksort($stockList); + ksort($priceList); + + return $this->render('product.html.twig', [ 'product' => $product, 'price_list' => $this->prepareChartData($priceList), diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 22bc991..f165e79 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -3,6 +3,7 @@ namespace Krzysiej\RyobiCrawler\Twig; use Illuminate\Database\Eloquent\Collection; +use Krzysiej\RyobiCrawler\Models\Country; use Krzysiej\RyobiCrawler\Models\Price; use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Stock; @@ -28,6 +29,7 @@ class AppExtension extends AbstractExtension new TwigFunction('discontinuedCount', [$this, 'discontinuedCount']), new TwigFunction('lowestPriceCount', [$this, 'lowestPriceCount']), new TwigFunction('renderCategoryTree', [$this, 'renderCategoryTree']), + new TwigFunction('getCountries', [$this, 'getCountries']), ]; } @@ -66,6 +68,11 @@ class AppExtension extends AbstractExtension ->count(); } + public function getCountries(): array + { + return Country::get()->toArray(); + } + public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null { return $items->first(fn($item) => str_starts_with($item->created_at, $date)); diff --git a/templates/product.html.twig b/templates/product.html.twig index 6444b01..becaf18 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -11,7 +11,7 @@ {{ product.name }} - {{ product.subTitle }} {% if product.promotions is not null and product.promotions.hasPromotion %}PROMO: {{ product.promotions.tag }} diff --git a/templates/productList.html.twig b/templates/productList.html.twig index 0f5f595..e46e208 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -19,6 +19,12 @@ {% endif %} + {% if listType == 'country' and country is not null%} +

{{ country.countryName }}

+
Currency: {{ country.currency }}
+ {{ dump(countryStats) }} + {% endif %} + {% if (listType starts with 'category_' and category == null) or not (listType starts with 'category_') or (listType starts with 'category_' and category is not null) %}
@@ -60,10 +66,10 @@ is new {% endif %} {{ product.subTitle }} {% if product.promotions is not null and product.promotions.hasPromotion %}PROMO: {{ product.promotions.tag }}{% endif %} - {{ product.country.countryName }} + {{ product.country.countryName }}