favors added need fixes

This commit is contained in:
VictoriaPresnyakova 2023-05-22 22:57:26 +04:00
parent a013ce0bca
commit d3d2e13d06
8 changed files with 228 additions and 0 deletions

Binary file not shown.

View File

@ -30,6 +30,11 @@ public class OrderController {
return new OrderDTO(orderService.addFavorToOrder(id, favId));
}
@DeleteMapping("/{id}/favor")
public OrderDTO removeFavorFromOrder(@PathVariable Long id, @RequestParam("favId") Long favId) {
return new OrderDTO(orderService.deleteFavorfromOrder(id, favId));
}
@DeleteMapping("/{id}")
public OrderDTO removeOrder(@PathVariable Long id) {
return new OrderDTO(orderService.deleteOrder(id));

View File

@ -50,4 +50,5 @@ public class OrderDTO {
}
public List<Long> getfavorIds() { return this.favorIds; }
public void setfavorIds(List<Long> favorIds) { this.favorIds = favorIds; }
public void setDate(Date modeldate) { this.date = modeldate; }
}

View File

@ -66,4 +66,6 @@ public class Order {
public int hashCode() {
return Objects.hash(getId(), getDate());
}
public void deleteFavor(Favor favor) {favorsList.remove(favor);}
}

View File

@ -0,0 +1,93 @@
package ru.ulstu.is.sbapp.repair.mvcController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.repair.dtos.ComponentDTO;
import ru.ulstu.is.sbapp.repair.dtos.FavorDTO;
import ru.ulstu.is.sbapp.repair.dtos.OrderDTO;
import ru.ulstu.is.sbapp.repair.service.ComponentService;
import ru.ulstu.is.sbapp.repair.service.FavorService;
import ru.ulstu.is.sbapp.repair.service.OrderService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@Controller
@RequestMapping("/orders")
public class OrderMvcController {
private final OrderService orderService;
private final FavorService favorService;
public OrderMvcController(OrderService orderService,
FavorService favorService) {
this.orderService = orderService;
this.favorService = favorService;
}
@GetMapping
public String getOrders(Model model) {
model.addAttribute("orders",
orderService.findAllOrders().stream()
.map(OrderDTO::new)
.toList());
return "orders";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editOrder(@PathVariable(required = false) Long id,
Model model) {
OrderDTO orderDTO = new OrderDTO();
if (id == null || id <= 0) {
model.addAttribute("orderDto", new OrderDTO());
} else {
model.addAttribute("orderId", id);
orderDTO = new OrderDTO(orderService.findOrder(id));
model.addAttribute("orderDto", orderDTO);
model.addAttribute("favorsList", orderDTO.getFavorsDTOList());
}
model.addAttribute("allFavors",
favorService.findAllFavor().stream()
.map(FavorDTO::new)
.toList());
return "order-edit";
}
@PostMapping(value = {"", "/{id}"})
public String saveOrder(@PathVariable(required = false) Long id,
@ModelAttribute @Valid OrderDTO orderDTO,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "order-edit";
}
if (id == null || id <= 0) {
orderService.addOrder();
}
return "redirect:/orders";
}
@PostMapping(value = "/{id}/favor/{favorId}")
public String addOrderFavor(@PathVariable(value = "id") Long id,
@PathVariable(value = "favorId") Long favorId) {
orderService.addFavorToOrder(id, favorId);
return "redirect:/orders";
}
@PostMapping(value = "/{id}/favorDelete/{favorId}")
public String deleteOrderFavor(@PathVariable(value = "id") Long id,
@PathVariable(value = "favorId") Long favorId) {
orderService.deleteFavorfromOrder(id, favorId);
return "redirect:/orders";
}
@PostMapping("/delete/{id}")
public String deleteOrder(@PathVariable Long id) {
orderService.deleteOrder(id);
return "redirect:/orders";
}
}

View File

@ -4,6 +4,7 @@ package ru.ulstu.is.sbapp.repair.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.is.sbapp.repair.exception.OrderNotFoundException;
import ru.ulstu.is.sbapp.repair.model.Component;
import ru.ulstu.is.sbapp.repair.model.Favor;
import ru.ulstu.is.sbapp.repair.model.Order;
import ru.ulstu.is.sbapp.repair.repository.OrderRepository;
@ -84,4 +85,12 @@ public class OrderService {
public void deleteAllOrder() {
orderRepository.deleteAll();
}
@Transactional
public Order deleteFavorfromOrder(Long id, Long favId){
final Order currentOrder = findOrder(id);
final Favor currentFavor = favorService.findFavor(favId);
currentOrder.deleteFavor(currentFavor);
return currentOrder;
}
}

View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/orders/{id}(id=${id})}" th:object="${orderDto}" method="post">
<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="favor, iterator: ${favorsList}">
<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">
<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="@{/orders/{id}/favorDelete/{favorId}(id=${id}, favorId=${favor.id})}" method="post">
<button th:id="'remove-' + ${favor.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="@{/orders}">
Назад
</a>
</div>
</form>
<form th:if="${id != null}" th:action="@{/orders/{id}/favor(id=${id})}" id="addFavorForm" method="post">
<div class="mb-3">
<label for="favors" class="form-label">Добавить favor </label>
<select class="form-select" id="favors" required>
<option disabled value="">Выберите component</option>
<option th:each="favor, iterator: ${allFavors}" th:text="${favor.favorName}" th:value="${favor.id}">
</select>
<button class="btn btn-outline-secondary" id="addFavorButton" th:attr="onclick=|document.getElementById('addFavorForm').action = document.getElementById('addFavorForm').action + '/' + document.getElementById('favors').value ; document.getElementById('addFavorForm}').submit()|">Добавить</button>
</div>
</form>
</div>
</body>
</html>

View File

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<title>Сайт</title>
</head>
<body>
<div layout:fragment="content">
<div>
<a class="btn btn-success button-fixed"
th:href="@{/orders/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>
</tr>
</thead>
<tbody>
<tr th:each="order, iterator: ${orders}">
<th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${order.date}" 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="@{/orders/edit/{id}(id=${order.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-${order.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/orders/delete/{id}(id=${order.id})}" method="post">
<button th:id="'remove-' + ${order.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>