From e6af154d0a7e5a7e882eb97da1ccb7ce7ce9dc8f Mon Sep 17 00:00:00 2001 From: dasha Date: Thu, 16 Mar 2023 17:32:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labwork1/app/student/model/Customer.java | 10 ++--- .../com/labwork1/app/student/model/Order.java | 6 ++- .../app/student/service/OrderService.java | 39 +++++++------------ .../com/labwork1/app/JpaCustomerTests.java | 25 ++++++------ 4 files changed, 34 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/labwork1/app/student/model/Customer.java b/src/main/java/com/labwork1/app/student/model/Customer.java index 247efea..69d24be 100644 --- a/src/main/java/com/labwork1/app/student/model/Customer.java +++ b/src/main/java/com/labwork1/app/student/model/Customer.java @@ -27,10 +27,6 @@ public class Customer { this.orders = new ArrayList<>(); } - public void addOrder(Order order) { - orders.add(order); - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -77,7 +73,9 @@ public class Customer { return orders; } - public void setOrders(List orders) { - this.orders = orders; + public void setOrder(Order order) { + if (order.getCustomer().equals(this)) { + this.orders.add(order); + } } } diff --git a/src/main/java/com/labwork1/app/student/model/Order.java b/src/main/java/com/labwork1/app/student/model/Order.java index 7ce8faf..911fbf6 100644 --- a/src/main/java/com/labwork1/app/student/model/Order.java +++ b/src/main/java/com/labwork1/app/student/model/Order.java @@ -28,8 +28,7 @@ public class Order { public Order() { } - public Order(Customer customer, Date dateOfPurchase) { - this.customer = customer; + public Order(Date dateOfPurchase) { this.dateOfPurchase = dateOfPurchase; } @@ -81,6 +80,9 @@ public class Order { public void setCustomer(Customer customer) { this.customer = customer; + if (!customer.getOrders().contains(this)) { + customer.setOrder(this); + } } public List getTickets() { diff --git a/src/main/java/com/labwork1/app/student/service/OrderService.java b/src/main/java/com/labwork1/app/student/service/OrderService.java index ab89613..083957c 100644 --- a/src/main/java/com/labwork1/app/student/service/OrderService.java +++ b/src/main/java/com/labwork1/app/student/service/OrderService.java @@ -19,12 +19,13 @@ public class OrderService { @Transactional public Order addOrder(Customer customer) { - if (customer == null) { + if (customer == null || em.find(Customer.class, customer.getId()) == null) { throw new IllegalArgumentException("addOrder empty fields"); } - final Order order = new Order(customer, - new Date(System.currentTimeMillis())); + final Order order = new Order(new Date(System.currentTimeMillis())); + order.setCustomer(customer); em.persist(order); + em.merge(customer); return order; } @@ -35,6 +36,8 @@ public class OrderService { } final Order currentOrder = findOrder(id); currentOrder.addTicket(ticket); + ticket.setOrder(currentOrder); + em.merge(ticket); return em.merge(currentOrder); } @@ -53,19 +56,6 @@ public class OrderService { .getResultList(); } - @Transactional - public Order updateOrder(Long id, Date dateOfPurchase, List tickets) { - if (dateOfPurchase == null || tickets == null) { - throw new IllegalArgumentException("updateOrder empty fields"); - } - if (tickets.size() == 0) - return deleteOrder(id); - final Order currentOrder = findOrder(id); - currentOrder.setDateOfPurchase(dateOfPurchase); - currentOrder.setTickets(tickets); - return em.merge(currentOrder); - } - @Transactional public Order deleteOrder(Long id) { final Order currentOrder = findOrder(id); @@ -74,16 +64,13 @@ public class OrderService { } @Transactional - public void deleteTicketInOrder(Long id, Long ticket_id) { -// final Order currentOrder = findOrder(id); -// em.remove(currentOrder); -// return currentOrder; - final Order currentReader = findOrder(id); - final Ticket currentManga = em.find(Ticket.class, ticket_id); - currentReader.getTickets().remove(currentManga); - currentManga.getOrders().remove(currentReader); - em.merge(currentReader); - em.merge(currentManga); + public Order deleteTicketInOrder(Long id, Ticket ticket) { + final Order currentOrder = findOrder(id); + final Ticket currentTicket = em.find(Ticket.class, ticket.getId()); + currentOrder.getTickets().remove(currentTicket); + currentTicket.getOrders().remove(currentOrder); + em.merge(currentTicket); + return em.merge(currentOrder); } @Transactional diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index 53af5c9..53ce456 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -32,37 +32,38 @@ public class JpaCustomerTests { ticketService.deleteAllTickets(); orderService.deleteAllOrders(); customerService.deleteAllCustomers(); - + // 2 билета final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis())); final Ticket ticket2 = ticketService.addTicket("Аватар", 200.0, new Timestamp(System.currentTimeMillis())); - + // 1 покупатель final Customer customer1 = customerService.addCustomer("Родион", "Иванов"); - + // 1 заказ, 1 копия заказа final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId())); final Order order1 = orderService.findOrder(order0.getId()); Assertions.assertEquals(order0, order1); Assertions.assertEquals(customerService .findCustomer(customer1.getId()).getOrders().size(), 1); - + // 0 заказов orderService.deleteAllOrders(); Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L)); - + // 2 покупателя final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов"); - + // 3 билета final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis())); - + // 1 заказ final Order order2 = orderService .addOrder(customerService.findCustomer(customer2.getId())); - + // у заказа 2 билета orderService.addTicket(order2.getId(), ticket1); orderService.addTicket(order2.getId(), ticket2); - - orderService.deleteTicketInOrder(order2.getId(), ticket2.getId()); + // у заказа 1 билет + orderService.deleteTicketInOrder(order2.getId(), ticket2); Assertions.assertEquals(orderService - .findOrder(order2.getId()).getTickets().size(), 2); - + .findOrder(order2.getId()).getTickets().size(), 1); + // 2-ой покупатель удален + // 0 заказов customerService.deleteCustomer(customer2.getId()); Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId())); Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));