LabWork05 vrode work

This commit is contained in:
Артём Алейкин 2023-06-13 14:58:48 +04:00
parent d5644b1a99
commit a84e05540a
3 changed files with 27 additions and 16 deletions

View File

@ -48,19 +48,29 @@ public class ProductMvcController {
} }
@GetMapping("filter") @GetMapping("filter")
public String FilterMethod(@RequestParam(required = false) Long manufacturerid, Model model, @RequestParam(name = "selected", required = false)List<ManufacturerDTO> selected) { public String FilterMethod(Model model, @RequestParam(name = "selected", required = false)List<Long> selected) {
if (manufacturerid != null && manufacturerid != 0) { if(selected == null) {
if (selected == null) { model.addAttribute("manufacturers", manufacturerService.findAllManufacturers()
selected = new ArrayList<>(); .stream()
} .map(ManufacturerDTO::new)
selected.add(new ManufacturerDTO(manufacturerService.findManufacturer(manufacturerid))); .toList());
model.addAttribute("selected", selected);
return "product-filter";
} }
model.addAttribute("selected", selected);
model.addAttribute("manufacturers", manufacturerService.findAllManufacturers() List<ProductDTO> productDTOS;
List<ManufacturerDTO> 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() .stream()
.map(ManufacturerDTO::new) .map(ProductDTO::new)
.toList()); .toList();
return "product-filter";
model.addAttribute("products", productDTOS);
return "products";
} }
@GetMapping(value = {"/update", "/update/{id}"}) @GetMapping(value = {"/update", "/update/{id}"})

View File

@ -137,6 +137,7 @@ public class ProductService {
} }
List<Product> filter = findAllProducts(); List<Product> filter = findAllProducts();
List<Product> result = new ArrayList<>();
for(Product product : filter) { for(Product product : filter) {
boolean flag = false; boolean flag = false;
@ -146,12 +147,12 @@ public class ProductService {
break; break;
} }
} }
if (!flag) if (flag)
{ {
filter.remove(product); result.add(product);
} }
} }
return filter; return result;
} }
} }

View File

@ -36,9 +36,9 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<form th:action="@{/product/filter(selected=${selected})}" enctype="manufacturerid/form-data" method="get"> <form th:action="@{/product/filter}" enctype="manufacturerid/form-data" method="get">
<div class="input-group mb-3"> <div class="input-group mb-3">
<select class="form-select" th:name="manufacturerid"> <select name="selected" class="form-select" multiple="multiple">
<option th:each="manufacturer, iterator: ${manufacturers}" th:value="${manufacturer.id}" th:text="${manufacturer.name}"></option> <option th:each="manufacturer, iterator: ${manufacturers}" th:value="${manufacturer.id}" th:text="${manufacturer.name}"></option>
</select> </select>
<button class="btn btn-outline-secondary" type="submit">Добавить</button> <button class="btn btn-outline-secondary" type="submit">Добавить</button>