LabWork05 neverWORK

This commit is contained in:
Артём Алейкин 2023-06-13 14:00:11 +04:00
parent 61d964259f
commit d5644b1a99
3 changed files with 66 additions and 52 deletions

View File

@ -5,12 +5,15 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; 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.CategoryService;
import ru.ulstu.is.sbapp.HardwareShop.services.ManufacturerService; import ru.ulstu.is.sbapp.HardwareShop.services.ManufacturerService;
import ru.ulstu.is.sbapp.HardwareShop.services.ProductService; import ru.ulstu.is.sbapp.HardwareShop.services.ProductService;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List;
@Controller @Controller
@RequestMapping("/product") @RequestMapping("/product")
@ -27,14 +30,39 @@ public class ProductMvcController {
} }
@GetMapping @GetMapping
public String getProducts(Model model) { public String getProducts(Model model, @RequestParam(required = false)List<ManufacturerDTO> manufacturerDTOS) {
model.addAttribute("products", List<ProductDTO> productDTOS;
productService.findAllProducts().stream() if (manufacturerDTOS == null) {
productDTOS = productService.findAllProducts().stream()
.map(ProductDTO::new) .map(ProductDTO::new)
.toList()); .toList();
}
else {
productDTOS = productService.getProductsFilter(manufacturerDTOS)
.stream()
.map(ProductDTO::new)
.toList();
}
model.addAttribute("products", productDTOS);
return "products"; 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}"}) @GetMapping(value = {"/update", "/update/{id}"})
public String updateProduct(@PathVariable(required = false) Long id, public String updateProduct(@PathVariable(required = false) Long id,
Model model) { Model model) {

View File

@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; 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.controller.ProductDTO;
import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer;
import ru.ulstu.is.sbapp.HardwareShop.models.Product; 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.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -126,4 +128,30 @@ public class ProductService {
public List<Manufacturer> getProductManufacturers(Long id){ public List<Manufacturer> getProductManufacturers(Long id){
return productRepository.findById(id).get().getManufacturerList(); 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;
}
} }

View File

@ -16,9 +16,9 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="selmanufacturer, iterator: ${selmanufacturers}"> <tr th:each="manufacturer, iterator: ${selected}">
<td th:text="${selmanufacturer.name}" style="width: 35%"/> <td th:text="${manufacturer.name}" style="width: 35%"/>
<td th:text="${selmanufacturer.address}" style="width: 35%"/> <td th:text="${manufacturer.address}" style="width: 35%"/>
<td style="width: 10%"> <td style="width: 10%">
<div class="btn-group" role="group" aria-label="Basic example"> <div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-danger button-fixed button-sm" <button type="button" class="btn btn-danger button-fixed button-sm"
@ -26,7 +26,7 @@
<i class="fa fa-trash" aria-hidden="true"></i> Удалить <i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button> </button>
</div> </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 th:id="'remove-' + ${manufacturer.id}" type="submit" style="display: none">
Удалить Удалить
</button> </button>
@ -36,7 +36,7 @@
</tbody> </tbody>
</table> </table>
</div> </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"> <div class="input-group mb-3">
<select class="form-select" th:name="manufacturerid"> <select class="form-select" th:name="manufacturerid">
<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>
@ -44,48 +44,6 @@
<button class="btn btn-outline-secondary" type="submit">Добавить</button> <button class="btn btn-outline-secondary" type="submit">Добавить</button>
</div> </div>
</form> </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> </div>
</body> </body>
</html> </html>