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 @@ - +