From 9b6bcd22be4bf272ab3ff26131ba078e2dc94229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Mon, 27 Jan 2025 12:10:15 +0100 Subject: [PATCH 1/2] Start working on sorting products by different columns. --- src/Controller/BaseController.php | 17 +++++++++++++++++ src/Controller/IndexController.php | 9 ++++----- src/Controller/NewController.php | 1 + src/Controller/SearchController.php | 1 - templates/productList.html.twig | 5 +++-- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index d9e3322..8736aef 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -2,7 +2,9 @@ namespace Krzysiej\RyobiCrawler\Controller; +use Illuminate\Database\Eloquent\Builder; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Twig\Environment; use Illuminate\Database\Capsule\Manager as Capsule; @@ -17,4 +19,19 @@ class BaseController extends AbstractController $capsule->setAsGlobal(); $capsule->bootEloquent(); } + + protected function handleOrderProducts(Builder $builder, Request $request): Builder + { + $builder->orderByDesc('starred')->orderByDesc('created_by'); + if ($request->query->get('order')) { + $orderField = $request->query->get('order'); + $direction = 'desc'; + if (str_starts_with($orderField, '-')) { + $direction = 'asc'; + } + $builder->orderBy($request->query->get('order'), $direction); + } + + return $builder; + } } diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index 0203711..b2a68dd 100644 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -3,18 +3,17 @@ namespace Krzysiej\RyobiCrawler\Controller; use Krzysiej\RyobiCrawler\Models\Product; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; final class IndexController extends BaseController { #[Route('/', name: 'app_home')] - public function __invoke(): Response + public function __invoke(Request $request): Response { - $products = Product::with(['currentStock', 'price']) - ->orderByDesc('starred') - ->orderByDesc('created_by') - ->get(); + $products = Product::with(['currentStock', 'price', 'currentPrice']); + $products = $this->handleOrderProducts($products, $request)->get(); return $this->render('productList.html.twig', ['products' => $products]); } } diff --git a/src/Controller/NewController.php b/src/Controller/NewController.php index 3a54105..6d17cd2 100644 --- a/src/Controller/NewController.php +++ b/src/Controller/NewController.php @@ -15,6 +15,7 @@ final class NewController extends BaseController $date = (new \DateTime())->modify('-30 days')->format('Y-m-d'); $products = Product::where('created_at', '>', $date) ->orderByDesc('starred') + ->orderByDesc('name') ->orderByDesc('created_by') ->with(['currentPrice']) ->get(); diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 0a42dbd..4821c6e 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -13,7 +13,6 @@ final class SearchController extends BaseController public function __invoke(Request $request): Response { $search = $request->query->get('search'); - //dd(); $products = Product::with('price') ->orWhere([['name', 'like', "%$search%"]]) ->orWhere([['subTitle', 'like', "%$search%"]])->get(); diff --git a/templates/productList.html.twig b/templates/productList.html.twig index af16217..d14bff5 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -1,15 +1,16 @@ {% extends "template.html.twig" %} {% block content %} + {{ app.request.get('order') }} - + - + -- 2.52.0 From c947e470affa6c6181e8ec88130c73c96fe09140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Tue, 4 Feb 2025 20:41:58 +0100 Subject: [PATCH 2/2] Increase php memory limit --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index d52e533..adce4f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM php:8.3-cli WORKDIR /usr/src/app +ENV PHP_MEMORY_LIMIT=512M RUN apt-get update && apt-get install -y \ git \ -- 2.52.0
NameName Categories PricePrice