From d5644b1a99ae51e74e4b57d82989d36b0a222983 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:00:11 +0400 Subject: [PATCH] LabWork05 neverWORK --- .../controller/ProductMvcController.java | 38 ++++++++++++-- .../HardwareShop/services/ProductService.java | 28 ++++++++++ .../resources/templates/product-filter.html | 52 ++----------------- 3 files changed, 66 insertions(+), 52 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 a91d12e..11b8cad 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 @@ -5,12 +5,15 @@ import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import ru.ulstu.is.sbapp.HardwareShop.models.Product; import ru.ulstu.is.sbapp.HardwareShop.services.CategoryService; import ru.ulstu.is.sbapp.HardwareShop.services.ManufacturerService; import ru.ulstu.is.sbapp.HardwareShop.services.ProductService; import java.io.IOException; +import java.util.ArrayList; import java.util.Base64; +import java.util.List; @Controller @RequestMapping("/product") @@ -27,14 +30,39 @@ public class ProductMvcController { } @GetMapping - public String getProducts(Model model) { - model.addAttribute("products", - productService.findAllProducts().stream() - .map(ProductDTO::new) - .toList()); + public String getProducts(Model model, @RequestParam(required = false)List manufacturerDTOS) { + List productDTOS; + if (manufacturerDTOS == null) { + productDTOS = productService.findAllProducts().stream() + .map(ProductDTO::new) + .toList(); + } + else { + productDTOS = productService.getProductsFilter(manufacturerDTOS) + .stream() + .map(ProductDTO::new) + .toList(); + } + model.addAttribute("products", productDTOS); return "products"; } + @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))); + } + model.addAttribute("selected", selected); + model.addAttribute("manufacturers", manufacturerService.findAllManufacturers() + .stream() + .map(ManufacturerDTO::new) + .toList()); + return "product-filter"; + } + @GetMapping(value = {"/update", "/update/{id}"}) public String updateProduct(@PathVariable(required = false) Long id, Model model) { 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 a44b99c..5a9f0df 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 @@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; +import ru.ulstu.is.sbapp.HardwareShop.controller.ManufacturerDTO; import ru.ulstu.is.sbapp.HardwareShop.controller.ProductDTO; import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; import ru.ulstu.is.sbapp.HardwareShop.models.Product; @@ -18,6 +19,7 @@ import ru.ulstu.is.sbapp.Util.Validation.ValidatorUtil; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -126,4 +128,30 @@ public class ProductService { public List getProductManufacturers(Long id){ return productRepository.findById(id).get().getManufacturerList(); } + + @Transactional + public List getProductsFilter(List manufacturerDTOS) { + List manufacturers = new ArrayList<>(); + for(ManufacturerDTO mnfctrDTO : manufacturerDTOS) { + manufacturers.add(manufacturerService.findManufacturer(mnfctrDTO.getId())); + } + + List filter = findAllProducts(); + + for(Product product : filter) { + boolean flag = false; + for (Manufacturer manufacturer : product.getManufacturerList()) { + if (manufacturers.contains(manufacturer)) { + flag = true; + break; + } + } + if (!flag) + { + filter.remove(product); + } + } + + return filter; + } } diff --git a/src/main/resources/templates/product-filter.html b/src/main/resources/templates/product-filter.html index f8dd6fb..eb6c71e 100644 --- a/src/main/resources/templates/product-filter.html +++ b/src/main/resources/templates/product-filter.html @@ -16,9 +16,9 @@ - - - + + +
-
+ @@ -36,7 +36,7 @@ - +