Сохранение
This commit is contained in:
parent
a7c19cd750
commit
e6af154d0a
@ -27,10 +27,6 @@ public class Customer {
|
|||||||
this.orders = new ArrayList<>();
|
this.orders = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addOrder(Order order) {
|
|
||||||
orders.add(order);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
@ -77,7 +73,9 @@ public class Customer {
|
|||||||
return orders;
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOrders(List<Order> orders) {
|
public void setOrder(Order order) {
|
||||||
this.orders = orders;
|
if (order.getCustomer().equals(this)) {
|
||||||
|
this.orders.add(order);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,7 @@ public class Order {
|
|||||||
public Order() {
|
public Order() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Order(Customer customer, Date dateOfPurchase) {
|
public Order(Date dateOfPurchase) {
|
||||||
this.customer = customer;
|
|
||||||
this.dateOfPurchase = dateOfPurchase;
|
this.dateOfPurchase = dateOfPurchase;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +80,9 @@ public class Order {
|
|||||||
|
|
||||||
public void setCustomer(Customer customer) {
|
public void setCustomer(Customer customer) {
|
||||||
this.customer = customer;
|
this.customer = customer;
|
||||||
|
if (!customer.getOrders().contains(this)) {
|
||||||
|
customer.setOrder(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Ticket> getTickets() {
|
public List<Ticket> getTickets() {
|
||||||
|
@ -19,12 +19,13 @@ public class OrderService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Order addOrder(Customer customer) {
|
public Order addOrder(Customer customer) {
|
||||||
if (customer == null) {
|
if (customer == null || em.find(Customer.class, customer.getId()) == null) {
|
||||||
throw new IllegalArgumentException("addOrder empty fields");
|
throw new IllegalArgumentException("addOrder empty fields");
|
||||||
}
|
}
|
||||||
final Order order = new Order(customer,
|
final Order order = new Order(new Date(System.currentTimeMillis()));
|
||||||
new Date(System.currentTimeMillis()));
|
order.setCustomer(customer);
|
||||||
em.persist(order);
|
em.persist(order);
|
||||||
|
em.merge(customer);
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,6 +36,8 @@ public class OrderService {
|
|||||||
}
|
}
|
||||||
final Order currentOrder = findOrder(id);
|
final Order currentOrder = findOrder(id);
|
||||||
currentOrder.addTicket(ticket);
|
currentOrder.addTicket(ticket);
|
||||||
|
ticket.setOrder(currentOrder);
|
||||||
|
em.merge(ticket);
|
||||||
return em.merge(currentOrder);
|
return em.merge(currentOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,19 +56,6 @@ public class OrderService {
|
|||||||
.getResultList();
|
.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
|
@Transactional
|
||||||
public Order deleteOrder(Long id) {
|
public Order deleteOrder(Long id) {
|
||||||
final Order currentOrder = findOrder(id);
|
final Order currentOrder = findOrder(id);
|
||||||
@ -74,16 +64,13 @@ public class OrderService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteTicketInOrder(Long id, Long ticket_id) {
|
public Order deleteTicketInOrder(Long id, Ticket ticket) {
|
||||||
// final Order currentOrder = findOrder(id);
|
final Order currentOrder = findOrder(id);
|
||||||
// em.remove(currentOrder);
|
final Ticket currentTicket = em.find(Ticket.class, ticket.getId());
|
||||||
// return currentOrder;
|
currentOrder.getTickets().remove(currentTicket);
|
||||||
final Order currentReader = findOrder(id);
|
currentTicket.getOrders().remove(currentOrder);
|
||||||
final Ticket currentManga = em.find(Ticket.class, ticket_id);
|
em.merge(currentTicket);
|
||||||
currentReader.getTickets().remove(currentManga);
|
return em.merge(currentOrder);
|
||||||
currentManga.getOrders().remove(currentReader);
|
|
||||||
em.merge(currentReader);
|
|
||||||
em.merge(currentManga);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -32,37 +32,38 @@ public class JpaCustomerTests {
|
|||||||
ticketService.deleteAllTickets();
|
ticketService.deleteAllTickets();
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
customerService.deleteAllCustomers();
|
customerService.deleteAllCustomers();
|
||||||
|
// 2 билета
|
||||||
final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis()));
|
final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis()));
|
||||||
final Ticket ticket2 = ticketService.addTicket("Аватар", 200.0, new Timestamp(System.currentTimeMillis()));
|
final Ticket ticket2 = ticketService.addTicket("Аватар", 200.0, new Timestamp(System.currentTimeMillis()));
|
||||||
|
// 1 покупатель
|
||||||
final Customer customer1 = customerService.addCustomer("Родион", "Иванов");
|
final Customer customer1 = customerService.addCustomer("Родион", "Иванов");
|
||||||
|
// 1 заказ, 1 копия заказа
|
||||||
final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId()));
|
final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId()));
|
||||||
final Order order1 = orderService.findOrder(order0.getId());
|
final Order order1 = orderService.findOrder(order0.getId());
|
||||||
Assertions.assertEquals(order0, order1);
|
Assertions.assertEquals(order0, order1);
|
||||||
|
|
||||||
Assertions.assertEquals(customerService
|
Assertions.assertEquals(customerService
|
||||||
.findCustomer(customer1.getId()).getOrders().size(), 1);
|
.findCustomer(customer1.getId()).getOrders().size(), 1);
|
||||||
|
// 0 заказов
|
||||||
orderService.deleteAllOrders();
|
orderService.deleteAllOrders();
|
||||||
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
|
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
|
||||||
|
// 2 покупателя
|
||||||
final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
|
final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
|
||||||
|
// 3 билета
|
||||||
final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis()));
|
final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis()));
|
||||||
|
// 1 заказ
|
||||||
final Order order2 = orderService
|
final Order order2 = orderService
|
||||||
.addOrder(customerService.findCustomer(customer2.getId()));
|
.addOrder(customerService.findCustomer(customer2.getId()));
|
||||||
|
// у заказа 2 билета
|
||||||
orderService.addTicket(order2.getId(), ticket1);
|
orderService.addTicket(order2.getId(), ticket1);
|
||||||
orderService.addTicket(order2.getId(), ticket2);
|
orderService.addTicket(order2.getId(), ticket2);
|
||||||
|
// у заказа 1 билет
|
||||||
orderService.deleteTicketInOrder(order2.getId(), ticket2.getId());
|
orderService.deleteTicketInOrder(order2.getId(), ticket2);
|
||||||
|
|
||||||
Assertions.assertEquals(orderService
|
Assertions.assertEquals(orderService
|
||||||
.findOrder(order2.getId()).getTickets().size(), 2);
|
.findOrder(order2.getId()).getTickets().size(), 1);
|
||||||
|
// 2-ой покупатель удален
|
||||||
|
// 0 заказов
|
||||||
customerService.deleteCustomer(customer2.getId());
|
customerService.deleteCustomer(customer2.getId());
|
||||||
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
|
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
|
||||||
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));
|
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));
|
||||||
|
Loading…
Reference in New Issue
Block a user