From a7c19cd7501b022cdb164b3f1e90613658993a8d Mon Sep 17 00:00:00 2001 From: dasha Date: Thu, 16 Mar 2023 15:12:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B2=D1=81?= =?UTF-8?q?=D1=8F=D0=BA=D0=B8=D1=85=20=D1=88=D1=82=D1=83=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/labwork1/app/student/model/Order.java | 6 +++-- .../app/student/service/OrderService.java | 19 +++++++++++++--- .../com/labwork1/app/JpaCustomerTests.java | 22 +++++++++---------- 3 files changed, 30 insertions(+), 17 deletions(-) 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 2967209..7ce8faf 100644 --- a/src/main/java/com/labwork1/app/student/model/Order.java +++ b/src/main/java/com/labwork1/app/student/model/Order.java @@ -3,6 +3,7 @@ package com.labwork1.app.student.model; import jakarta.persistence.*; import java.sql.Date; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -27,13 +28,14 @@ public class Order { public Order() { } - public Order(Customer customer, Date dateOfPurchase, List tickets) { + public Order(Customer customer, Date dateOfPurchase) { this.customer = customer; this.dateOfPurchase = dateOfPurchase; - this.tickets = tickets; } public void addTicket(Ticket ticket) { + if (tickets == null) + tickets = new ArrayList<>(); tickets.add(ticket); if (ticket.getOrders() == null) { ticket.setOrder(this); 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 88c4ac5..ab89613 100644 --- a/src/main/java/com/labwork1/app/student/service/OrderService.java +++ b/src/main/java/com/labwork1/app/student/service/OrderService.java @@ -18,12 +18,12 @@ public class OrderService { private EntityManager em; @Transactional - public Order addOrder(Customer customer, List tickets) { - if (customer == null || tickets.size() == 0) { + public Order addOrder(Customer customer) { + if (customer == null) { throw new IllegalArgumentException("addOrder empty fields"); } final Order order = new Order(customer, - new Date(System.currentTimeMillis()), tickets); + new Date(System.currentTimeMillis())); em.persist(order); return order; } @@ -73,6 +73,19 @@ public class OrderService { return currentOrder; } + @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); + } + @Transactional public void deleteAllOrders() { em.createQuery("delete from Order").executeUpdate(); diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index 3bfa9f7..53af5c9 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.List; @SpringBootTest @@ -36,16 +35,12 @@ public class JpaCustomerTests { final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis())); final Ticket ticket2 = ticketService.addTicket("Аватар", 200.0, new Timestamp(System.currentTimeMillis())); - List tickets = new ArrayList<>(); - tickets.add(ticket1); - tickets.add(ticket2); final Customer customer1 = customerService.addCustomer("Родион", "Иванов"); - final Order order = orderService - .addOrder(customerService.findCustomer(customer1.getId()), tickets); - final Order order1 = orderService.findOrder(order.getId()); - Assertions.assertEquals(order, order1); + 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); @@ -56,16 +51,17 @@ public class JpaCustomerTests { final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов"); final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis())); - tickets.clear(); - tickets.add(ticket3); + final Order order2 = orderService - .addOrder(customerService.findCustomer(customer2.getId()), tickets); + .addOrder(customerService.findCustomer(customer2.getId())); orderService.addTicket(order2.getId(), ticket1); orderService.addTicket(order2.getId(), ticket2); + orderService.deleteTicketInOrder(order2.getId(), ticket2.getId()); + Assertions.assertEquals(orderService - .findOrder(order2.getId()).getTickets().size(), 3); + .findOrder(order2.getId()).getTickets().size(), 2); customerService.deleteCustomer(customer2.getId()); Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId())); @@ -133,5 +129,7 @@ public class JpaCustomerTests { final List tickets2 = ticketService.findAllTickets(); log.info(tickets2.toString() + "testTicketReadAllEmpty"); Assertions.assertEquals(tickets2.size(), 0); + + } }