From 0da1aa4ff6c2ba7e6cd60fac9939f1829594bf4b Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Tue, 24 Feb 2026 08:44:23 +0100 Subject: [PATCH] Country stats and searching by exact model name. --- src/Controller/CountryController.php | 22 +++++++++++++++++----- src/Controller/ProductController.php | 26 ++++++++++++++++++++++++++ templates/product.html.twig | 2 +- templates/productList.html.twig | 6 ++++-- templates/template.html.twig | 2 +- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/Controller/CountryController.php b/src/Controller/CountryController.php index 9098994..218e154 100644 --- a/src/Controller/CountryController.php +++ b/src/Controller/CountryController.php @@ -10,19 +10,31 @@ use Symfony\Component\Routing\Attribute\Route; final class CountryController extends BaseController { - #[Route('/country/{country?}', name: 'app_country')] - public function __invoke(?string $country): Response + #[Route('/country/{countryName?}', name: 'app_country')] + public function __invoke(?string $countryName): Response { /** @var Product[] $products */ $products = Product::with(['price', 'lowestPrice', 'country']) ->join('countries', 'countries.id', '=', 'products.country_id') - ->where('countryName', $country) + ->where('countryName', $countryName) ->orderByDesc('starred') ->orderByDesc('created_by') ->get(); - $country = Country::where('countryName', $country)->first(); + $country = Country::where('countryName', $countryName)->first(); - return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'country', 'country' => $country]); + 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/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 e385a03..e46e208 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -21,6 +21,8 @@ {% if listType == 'country' and country is not null%}

{{ country.countryName }}

+
Currency: {{ country.currency }}
+ {{ dump(countryStats) }} {% endif %} @@ -64,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 }}