From a84e05540ae3ec806634aac8e616c62bbbb5f569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Jun 2023 14:58:48 +0400 Subject: [PATCH] LabWork05 vrode work --- .../controller/ProductMvcController.java | 32 ++++++++++++------- .../HardwareShop/services/ProductService.java | 7 ++-- .../resources/templates/product-filter.html | 4 +-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductMvcController.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductMvcController.java index 11b8cad..d329e3a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductMvcController.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductMvcController.java @@ -48,19 +48,29 @@ public class ProductMvcController { } @GetMapping("filter") - public String FilterMethod(@RequestParam(required = false) Long manufacturerid, Model model, @RequestParam(name = "selected", required = false)List selected) { - if (manufacturerid != null && manufacturerid != 0) { - if (selected == null) { - selected = new ArrayList<>(); - } - selected.add(new ManufacturerDTO(manufacturerService.findManufacturer(manufacturerid))); + public String FilterMethod(Model model, @RequestParam(name = "selected", required = false)List selected) { + if(selected == null) { + model.addAttribute("manufacturers", manufacturerService.findAllManufacturers() + .stream() + .map(ManufacturerDTO::new) + .toList()); + model.addAttribute("selected", selected); + return "product-filter"; } - model.addAttribute("selected", selected); - model.addAttribute("manufacturers", manufacturerService.findAllManufacturers() + + List productDTOS; + List manufacturerDTOS = new ArrayList<>(); + + for(int i = 0; i < selected.size(); ++i) { + manufacturerDTOS.add(new ManufacturerDTO(manufacturerService.findManufacturer(selected.get(i)))); + } + productDTOS = productService.getProductsFilter(manufacturerDTOS) .stream() - .map(ManufacturerDTO::new) - .toList()); - return "product-filter"; + .map(ProductDTO::new) + .toList(); + + model.addAttribute("products", productDTOS); + return "products"; } @GetMapping(value = {"/update", "/update/{id}"}) diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java index 5a9f0df..40f6128 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java @@ -137,6 +137,7 @@ public class ProductService { } List filter = findAllProducts(); + List result = new ArrayList<>(); for(Product product : filter) { boolean flag = false; @@ -146,12 +147,12 @@ public class ProductService { break; } } - if (!flag) + if (flag) { - filter.remove(product); + result.add(product); } } - return filter; + return result; } } diff --git a/src/main/resources/templates/product-filter.html b/src/main/resources/templates/product-filter.html index eb6c71e..ecb3838 100644 --- a/src/main/resources/templates/product-filter.html +++ b/src/main/resources/templates/product-filter.html @@ -36,9 +36,9 @@ -
+
-