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")
public String FilterMethod(@RequestParam(required = false) Long manufacturerid, Model model, @RequestParam(name = "selected", required = false)List<ManufacturerDTO> 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<Long> 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<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()
.map(ManufacturerDTO::new)
.toList());
return "product-filter";
.map(ProductDTO::new)
.toList();
model.addAttribute("products", productDTOS);
return "products";
}
@GetMapping(value = {"/update", "/update/{id}"})

View File

@ -137,6 +137,7 @@ public class ProductService {
}
List<Product> filter = findAllProducts();
List<Product> 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;
}
}

View File

@ -36,9 +36,9 @@
</tbody>
</table>
</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">
<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>
</select>
<button class="btn btn-outline-secondary" type="submit">Добавить</button>