Сохранение
This commit is contained in:
parent
a7c19cd750
commit
e6af154d0a
@ -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<Order> orders) {
|
||||
this.orders = orders;
|
||||
public void setOrder(Order order) {
|
||||
if (order.getCustomer().equals(this)) {
|
||||
this.orders.add(order);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Ticket> getTickets() {
|
||||
|
@ -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<Ticket> 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
|
||||
|
@ -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()));
|
||||
|
Loading…
Reference in New Issue
Block a user