favors done

This commit is contained in:
VictoriaPresnyakova 2023-05-22 22:07:09 +04:00
parent 0a9da86a9d
commit a013ce0bca
7 changed files with 141 additions and 26 deletions

Binary file not shown.

View File

@ -31,13 +31,13 @@ public class FavorDTO {
}
this.componentsDTOList = favor.getComponents() == null ? null: favor.getComponents()
.stream()
.filter(x -> x.getFavors().contains(favor.getId()))
.map(y -> new ComponentDTO(y))
.filter(x -> x.getFavors().contains(favor))
.map(ComponentDTO::new)
.toList();
this.ordersDTOList = favor.getOrders() == null ? null: favor.getOrders()
.stream()
.filter(x -> x.getFavorsList().contains(favor.getId()))
.map(y -> new OrderDTO(y))
.filter(x -> x.getFavorsList().contains(favor))
.map(OrderDTO::new)
.toList();
}
@ -61,5 +61,11 @@ public class FavorDTO {
}
public List<Long> getcomponentIds() { return this.componentIds; }
public void setFavorName(String modelName) {
this.favorName = modelName;
}
public void setPrice(Integer modelPrice) {
this.price = modelPrice;
}
public void setcomponentIds(List<Long> componentIds) { this.componentIds = componentIds; }
}

View File

@ -33,8 +33,8 @@ public class OrderDTO {
}
this.favorsDTOList = order.getFavorsList() == null ? null: order.getFavorsList()
.stream()
.filter(x -> x.getOrders().contains(order.getId()))
.map(y -> new FavorDTO(y))
.filter(x -> x.getOrders().contains(order))
.map(FavorDTO::new)
.toList();
}
public OrderDTO(){

View File

@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.ulstu.is.sbapp.repair.dtos.ComponentDTO;
import ru.ulstu.is.sbapp.repair.dtos.FavorDTO;
import ru.ulstu.is.sbapp.repair.model.Favor;
import ru.ulstu.is.sbapp.repair.service.ComponentService;
import ru.ulstu.is.sbapp.repair.service.FavorService;
import org.springframework.web.bind.annotation.*;
@ -37,17 +38,17 @@ public class FavorMvcController {
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editFavor(@PathVariable(required = false) Long id,
Model model) {
FavorDTO favorDTO = new FavorDTO();
if (id == null || id <= 0) {
model.addAttribute("favorDto", new FavorDTO());
} else {
model.addAttribute("favorId", id);
model.addAttribute("favorDto", new FavorDTO(favorService.findFavor(id)));
favorDTO = new FavorDTO(favorService.findFavor(id));
model.addAttribute("favorDto", favorDTO);
model.addAttribute("componentsList", favorDTO.getComponentsDTOList());
}
// model.addAttribute("componentsList",
// componentService.findFilteredComponents(null, null, null, null, id).stream()
// .map(ComponentDTO::new)
// .toList());
model.addAttribute("allComputers",
model.addAttribute("allComponents",
componentService.findAllComponent().stream()
.map(ComponentDTO::new)
.toList());
@ -69,15 +70,15 @@ public class FavorMvcController {
}
return "redirect:/favors";
}
@PostMapping(value = "/{id}/computer/{computerId}")
@PostMapping(value = "/{id}/component/{componentId}")
public String addFavorComponent(@PathVariable(value = "id") Long id,
@PathVariable(value = "computerId") Long componentId) {
@PathVariable(value = "componentId") Long componentId) {
favorService.addComponentToFavor(id, componentId);
return "redirect:/favors";
}
@PostMapping(value = "/{id}/computerDelete/{computerId}")
@PostMapping(value = "/{id}/componentDelete/{componentId}")
public String deleteFavorComponent(@PathVariable(value = "id") Long id,
@PathVariable(value = "computerId") Long componentId) {
@PathVariable(value = "componentId") Long componentId) {
favorService.deleteComponentfromFavor(id, componentId);
return "redirect:/favors";
}

View File

@ -51,15 +51,15 @@ public class FavorService {
}
@Transactional
public Favor updateFavor(Long id, String componentName, Integer price) {
if (!StringUtils.hasText(componentName)) {
public Favor updateFavor(Long id, String favorName, Integer price) {
if (!StringUtils.hasText(favorName)) {
throw new IllegalArgumentException("Favor name is null or empty");
}
if (price == null){
throw new IllegalArgumentException("Price is null or empty");
}
final Favor currentFavor = findFavor(id);
currentFavor.setFavorName(componentName);
currentFavor.setFavorName(favorName);
currentFavor.setPrice(price);
validatorUtil.validate(currentFavor);
return favorRepository.save(currentFavor);

View File

@ -1,10 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/favors/{id}(id=${id})}" th:object="${favorDto}" method="post">
<div class="mb-3">
<label for="favorName" class="form-label">Название</label>
<input type="text" class="form-control" id="favorName" th:field="${favorDto.favorName}" required="true">
</div>
<div class="mb-3">
<label for="price" class="form-label">Цена</label>
<input type="number" class="form-control" id="price" th:field="${favorDto.price}" required="true">
</div>
<div th:if="${id != null}" class="table-responsive">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Название</th>
<th scope="col">Количество</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="component, iterator: ${componentsList}">
<th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${component.componentName}" style="width: 60%"/>
<td th:text="${component.amount}" style="width: 60%"/>
<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"
th:attr="onclick=|confirm('Удалить?') && document.getElementById('remove-${component.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/favors/{id}/componentDelete/{componentId}(id=${id}, componentId=${component.id})}" method="post">
<button th:id="'remove-' + ${component.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span th:if="${id == null}">Добавить</span>
<span th:if="${id != null}">Обновить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/favors}">
Назад
</a>
</div>
</form>
<form th:if="${id != null}" th:action="@{/favors/{id}/component(id=${id})}" id="addComponentForm" method="post">
<div class="mb-3">
<label for="components" class="form-label">Добавить component </label>
<select class="form-select" id="components" required>
<option disabled value="">Выберите component</option>
<option th:each="component, iterator: ${allComponents}" th:text="${component.componentName}" th:value="${component.id}">
</select>
<button class="btn btn-outline-secondary" id="addComponentButton" th:attr="onclick=|document.getElementById('addComponentForm').action = document.getElementById('addComponentForm').action + '/' + document.getElementById('components').value ; document.getElementById('addComponentForm}').submit()|">Добавить</button>
</div>
</form>
</div>
</body>
</html>

View File

@ -1,10 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<meta charset="UTF-8">
<title>Title</title>
<title>Сайт</title>
</head>
<body>
<div layout:fragment="content">
<div>
<a class="btn btn-success button-fixed"
th:href="@{/favors/edit/}">
<i class="fa-solid fa-plus"></i> Добавить
</a>
</div>
<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>
</tr>
</thead>
<tbody>
<tr th:each="favor, iterator: ${favors}">
<th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${favor.favorName}" style="width: 60%"/>
<td th:text="${favor.price}" style="width: 60%"/>
<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="@{/favors/edit/{id}(id=${favor.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-${favor.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/favors/delete/{id}(id=${favor.id})}" method="post">
<button th:id="'remove-' + ${favor.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>