LabWork05 neverWORK
This commit is contained in:
parent
61d964259f
commit
d5644b1a99
@ -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()
|
||||
public String getProducts(Model model, @RequestParam(required = false)List<ManufacturerDTO> manufacturerDTOS) {
|
||||
List<ProductDTO> productDTOS;
|
||||
if (manufacturerDTOS == null) {
|
||||
productDTOS = productService.findAllProducts().stream()
|
||||
.map(ProductDTO::new)
|
||||
.toList());
|
||||
.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<ManufacturerDTO> 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) {
|
||||
|
@ -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<Manufacturer> getProductManufacturers(Long id){
|
||||
return productRepository.findById(id).get().getManufacturerList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Product> getProductsFilter(List<ManufacturerDTO> manufacturerDTOS) {
|
||||
List<Manufacturer> manufacturers = new ArrayList<>();
|
||||
for(ManufacturerDTO mnfctrDTO : manufacturerDTOS) {
|
||||
manufacturers.add(manufacturerService.findManufacturer(mnfctrDTO.getId()));
|
||||
}
|
||||
|
||||
List<Product> 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;
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,9 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="selmanufacturer, iterator: ${selmanufacturers}">
|
||||
<td th:text="${selmanufacturer.name}" style="width: 35%"/>
|
||||
<td th:text="${selmanufacturer.address}" style="width: 35%"/>
|
||||
<tr th:each="manufacturer, iterator: ${selected}">
|
||||
<td th:text="${manufacturer.name}" style="width: 35%"/>
|
||||
<td th:text="${manufacturer.address}" style="width: 35%"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<button type="button" class="btn btn-danger button-fixed button-sm"
|
||||
@ -26,7 +26,7 @@
|
||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||
</button>
|
||||
</div>
|
||||
<form th:action="@{/product/filter/delete/{id}(id=${selmanufacturer.id})}" method="post">
|
||||
<form th:action="@{/manufacturer/delete/{id}(id=${manufacturer.id})}" method="post">
|
||||
<button th:id="'remove-' + ${manufacturer.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
@ -36,7 +36,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<form th:action="@{/product/filter}" th:object="${selmanufacturers}" method="post">
|
||||
<form th:action="@{/product/filter(selected=${selected})}" enctype="manufacturerid/form-data" method="get">
|
||||
<div class="input-group mb-3">
|
||||
<select class="form-select" th:name="manufacturerid">
|
||||
<option th:each="manufacturer, iterator: ${manufacturers}" th:value="${manufacturer.id}" th:text="${manufacturer.name}"></option>
|
||||
@ -44,48 +44,6 @@
|
||||
<button class="btn btn-outline-secondary" type="submit">Добавить</button>
|
||||
</div>
|
||||
</form>
|
||||
<div class="table-responsive">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Название</th>
|
||||
<th scope="col">Цена</th>
|
||||
<th scope="col">Фото</th>
|
||||
<th scope="col">Категория</th>
|
||||
<th scope="col">Редактировать запись</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="product, iterator: ${products}">
|
||||
<td th:text="${product.name}" style="width: 25%"/>
|
||||
<td th:text="${product.price}" style="width: 25%"/>
|
||||
<td><img th:src="${product.photo}" class="img-thumbnail mw-50 mh-50"/></td>
|
||||
<td th:text="${product.categoryName}" style="width: 25%"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<a class="btn btn-warning button-fixed button-sm"
|
||||
th:href="@{/product/update/{id}(id=${product.id})}">
|
||||
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
|
||||
</a>
|
||||
<button type="button" class="btn btn-danger button-fixed button-sm"
|
||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${product.id}').click()|">
|
||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||
</button>
|
||||
<a class="btn btn-primary button-fixed button-sm"
|
||||
th:href="@{/product/{id}/manufacturers(id=${product.id})}">
|
||||
<i class="fa fa-pencil" aria-hidden="true"></i> Производители
|
||||
</a>
|
||||
</div>
|
||||
<form th:action="@{/product/delete/{id}(id=${product.id})}" method="post">
|
||||
<button th:id="'remove-' + ${product.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user