diff --git a/data.mv.db b/data.mv.db index 04a1b7b..14eedeb 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/controller/OrderController.java b/src/main/java/ru/ulstu/is/sbapp/repair/controller/OrderController.java index a335fb1..cf9d5bc 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/controller/OrderController.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/controller/OrderController.java @@ -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)); diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/dtos/OrderDTO.java b/src/main/java/ru/ulstu/is/sbapp/repair/dtos/OrderDTO.java index 1051e0c..7bef5cd 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/dtos/OrderDTO.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/dtos/OrderDTO.java @@ -50,4 +50,5 @@ public class OrderDTO { } public List getfavorIds() { return this.favorIds; } public void setfavorIds(List favorIds) { this.favorIds = favorIds; } + public void setDate(Date modeldate) { this.date = modeldate; } } diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java b/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java index b2146d6..c17eefb 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/model/Order.java @@ -66,4 +66,6 @@ public class Order { public int hashCode() { return Objects.hash(getId(), getDate()); } + + public void deleteFavor(Favor favor) {favorsList.remove(favor);} } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/mvcController/OrderMvcController.java b/src/main/java/ru/ulstu/is/sbapp/repair/mvcController/OrderMvcController.java new file mode 100644 index 0000000..e1bd877 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/repair/mvcController/OrderMvcController.java @@ -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"; + } +} diff --git a/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java b/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java index e92f9f9..f358037 100644 --- a/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java +++ b/src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java @@ -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; + } } diff --git a/src/main/resources/templates/order-edit.html b/src/main/resources/templates/order-edit.html new file mode 100644 index 0000000..1307b6f --- /dev/null +++ b/src/main/resources/templates/order-edit.html @@ -0,0 +1,66 @@ + + + + + +
+
+
+
+ + + + + + + + + + + + + + +
#Название Цена
+ + + +
+ +
+ + + +
+
+
+ + + Назад + +
+ + +
+
+ + + +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/orders.html b/src/main/resources/templates/orders.html new file mode 100644 index 0000000..4bf122a --- /dev/null +++ b/src/main/resources/templates/orders.html @@ -0,0 +1,52 @@ + + + + Сайт + + +
+
+ + Добавить + +
+
+ + + + + + + + + + + + + +
#Дата
+ + +
+ + Изменить + + +
+
+ +
+
+
+
+ + \ No newline at end of file