Изменения

This commit is contained in:
dasha 2023-03-27 22:57:52 +04:00
parent af19636b32
commit 302a163749
7 changed files with 56 additions and 49 deletions

View File

@ -1,8 +1,6 @@
package com.labwork1.app.student.model; package com.labwork1.app.student.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -17,7 +15,7 @@ public class Customer {
private String login; private String login;
@Column @Column
private String password; private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer") @OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.REMOVE)
private List<Order> orders; private List<Order> orders;
public Customer() { public Customer() {

View File

@ -1,8 +1,6 @@
package com.labwork1.app.student.model; package com.labwork1.app.student.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import java.sql.Date; import java.sql.Date;
import java.util.ArrayList; import java.util.ArrayList;
@ -39,6 +37,11 @@ public class Order {
this.sessions.add(orderSession); this.sessions.add(orderSession);
} }
public void removeSession(OrderSession orderSession){
if (sessions.contains(orderSession))
this.sessions.remove(orderSession);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -48,6 +48,11 @@ public class Session {
this.orders.add(orderSession); this.orders.add(orderSession);
} }
public void removeOrder(OrderSession orderSession){
if (orders.contains(orderSession))
this.orders.remove(orderSession);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -3,6 +3,7 @@ package com.labwork1.app.student.service;
import com.labwork1.app.student.model.Customer; import com.labwork1.app.student.model.Customer;
import com.labwork1.app.student.model.Order; import com.labwork1.app.student.model.Order;
import com.labwork1.app.student.model.OrderSession; import com.labwork1.app.student.model.OrderSession;
import com.labwork1.app.student.model.Session;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -56,40 +57,22 @@ public class CustomerService {
@Transactional @Transactional
public Customer deleteCustomer(Long id) { public Customer deleteCustomer(Long id) {
// final Customer currentCustomer = findCustomer(id);
// final List<Order> currentCustomerOrders = currentCustomer.getOrders();
//
// for (int i = 0; i < currentCustomerOrders.size(); i++) {
// final Order temp = em.find(Order.class, currentCustomerOrders.get(i).getId());
// System.out.println(temp);
//
// List<OrderSession> orderSessionList = em
// .createQuery("select p from OrderSession p where p.id.orderId = " + temp.getId(),
// OrderSession.class)
// .getResultList();
// for (OrderSession x : orderSessionList) {
// System.out.println(x.getOrder());
// em.remove(x);
// }
// temp.getSessions().clear();
// }
// currentCustomer.getOrders().clear();
// em.remove(currentCustomer);
// return currentCustomer;
final Customer currentCustomer = findCustomer(id); final Customer currentCustomer = findCustomer(id);
int size = currentCustomer.getOrders().size(); final List<Order> currentCustomerOrders = currentCustomer.getOrders();
for (int i = 0; i < size; i++) {
Order temp = currentCustomer.getOrders().get(0); for (int i = 0; i < currentCustomerOrders.size(); i++) {
temp.getSessions().remove(temp); final Order currentOrder = em.find(Order.class, currentCustomerOrders.get(i).getId());
temp.getCustomer().removeOrder(temp); List<OrderSession> orderSessionList = em
em.remove(temp); .createQuery("select p from OrderSession p where p.id.orderId = " + id, OrderSession.class)
} .getResultList();
int ordSize = currentCustomer.getOrders().size(); for (int j = 0; j < orderSessionList.size(); j++) {
for (int i = 0; i < ordSize; i++){ final Session currentSession = orderSessionList.get(j).getSession();
OrderCustomers temp = currentCustomer.getOrders().get(0); currentOrder.getSessions().remove(orderSessionList.get(j));
temp.getCustomer().removeOrder(temp); currentSession.getOrders().remove(orderSessionList.get(j));
temp.getOrder().removeCustomers(temp); em.remove(orderSessionList.get(j));
em.remove(temp); em.merge(currentSession);
}
em.merge(currentOrder);
} }
em.remove(currentCustomer); em.remove(currentCustomer);
return currentCustomer; return currentCustomer;

View File

@ -32,7 +32,7 @@ public class OrderService {
@Transactional @Transactional
public Order addSession(Long id, Long session, Integer count) { public Order addSession(Long id, Long session, Integer count) {
if (session <= 0) { if (session <= 0 || count <= 0) {
throw new IllegalArgumentException("addOrder empty fields"); throw new IllegalArgumentException("addOrder empty fields");
} }
final Order currentOrder = findOrder(id); final Order currentOrder = findOrder(id);

View File

@ -1,6 +1,7 @@
package com.labwork1.app.student.service; package com.labwork1.app.student.service;
import com.labwork1.app.student.model.Cinema; import com.labwork1.app.student.model.Cinema;
import com.labwork1.app.student.model.OrderSession;
import com.labwork1.app.student.model.Session; import com.labwork1.app.student.model.Session;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
@ -56,6 +57,13 @@ public class SessionService {
@Transactional @Transactional
public Session deleteSession(Long id) { public Session deleteSession(Long id) {
final Session currentSession = findSession(id); final Session currentSession = findSession(id);
int size = currentSession.getOrders().size();
for (int i = 0; i < size; i++) {
OrderSession temp = currentSession.getOrders().get(0);
temp.getSession().removeOrder(temp);
temp.getOrder().removeSession(temp);
em.remove(temp);
}
em.remove(currentSession); em.remove(currentSession);
return currentSession; return currentSession;
} }

View File

@ -40,12 +40,12 @@ public class JpaCustomerTests {
// 2 кино // 2 кино
final Cinema cinema1 = cinemaService.addCinema("Меню"); final Cinema cinema1 = cinemaService.addCinema("Меню");
final Cinema cinema2 = cinemaService.addCinema("Аватар"); final Cinema cinema2 = cinemaService.addCinema("Аватар");
// 2 билета // 2 сеанса
final Session session1 = sessionService.addSession(300.0, final Session session1 = sessionService.addSession(300.0,
new Timestamp(System.currentTimeMillis()), cinema1.getId()); new Timestamp(System.currentTimeMillis()), cinema1.getId());
final Session session2 = sessionService.addSession( 200.0, final Session session2 = sessionService.addSession( 200.0,
new Timestamp(System.currentTimeMillis()), cinema1.getId()); new Timestamp(System.currentTimeMillis()), cinema1.getId());
// 2 билета у 1 кино // проверка 2 сеанса у 1 кино
Assertions.assertEquals(cinemaService Assertions.assertEquals(cinemaService
.findCinema(cinema1.getId()).getSessions().size(), 2); .findCinema(cinema1.getId()).getSessions().size(), 2);
// 1 покупатель // 1 покупатель
@ -55,6 +55,7 @@ public class JpaCustomerTests {
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 заказов // 0 заказов
@ -62,26 +63,35 @@ public class JpaCustomerTests {
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L)); Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
// 2 покупателя // 2 покупателя
final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов"); final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
// 3 билета // 3 сеанса
final Session session3 = sessionService.addSession(300.0, final Session session3 = sessionService.addSession(300.0,
new Timestamp(System.currentTimeMillis()), cinema2.getId()); new Timestamp(System.currentTimeMillis()), cinema2.getId());
// 1 заказ // 1 заказ
final Order order2 = orderService final Order order2 = orderService
.addOrder(customerService.findCustomer(customer2.getId())); .addOrder(customerService.findCustomer(customer2.getId()));
// у заказа 2 билета // у заказа 2 сеанса
orderService.addSession(order2.getId(), session1.getId(), 2); orderService.addSession(order2.getId(), session1.getId(), 2);
orderService.addSession(order2.getId(), session2.getId(), 3); orderService.addSession(order2.getId(), session2.getId(), 3);
// у заказа 1 билет // у заказа 1 сеанс
orderService.deleteSessionInOrder(order2.getId(), session2.getId()); orderService.deleteSessionInOrder(order2.getId(), session2.getId());
// у заказа 0 сеансов
// осталось 2 сеанса
sessionService.deleteSession(session1.getId());
Assertions.assertEquals(sessionService.findAllSessions().size(), 2);
Assertions.assertEquals(orderService Assertions.assertEquals(orderService
.findOrder(order2.getId()).getSessions().size(), 1); .findOrder(order2.getId()).getSessions().size(), 0);
// 2-ой покупатель удален // 2-ой покупатель удален
// 0 заказов // 0 заказов после его удаления
Assertions.assertEquals(orderService.findAllOrders().size(), 1);
customerService.deleteCustomer(customer2.getId()); customerService.deleteCustomer(customer2.getId());
// Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
// Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId())); Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
// Assertions.assertEquals(orderService.findAllOrders().size(), 0); Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));
Assertions.assertEquals(orderService.findAllOrders().size(), 0);
Assertions.assertEquals(sessionService.findSession(session3.getId()), session3);
cinemaService.deleteCinema(cinema2.getId());
} }
/*@Test() /*@Test()