Изменения

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;
import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import java.util.ArrayList;
import java.util.List;
@ -17,7 +15,7 @@ public class Customer {
private String login;
@Column
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.REMOVE)
private List<Order> orders;
public Customer() {

View File

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

View File

@ -48,6 +48,11 @@ public class Session {
this.orders.add(orderSession);
}
public void removeOrder(OrderSession orderSession){
if (orders.contains(orderSession))
this.orders.remove(orderSession);
}
@Override
public boolean equals(Object o) {
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.Order;
import com.labwork1.app.student.model.OrderSession;
import com.labwork1.app.student.model.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -56,40 +57,22 @@ public class CustomerService {
@Transactional
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);
int size = currentCustomer.getOrders().size();
for (int i = 0; i < size; i++) {
Order temp = currentCustomer.getOrders().get(0);
temp.getSessions().remove(temp);
temp.getCustomer().removeOrder(temp);
em.remove(temp);
final List<Order> currentCustomerOrders = currentCustomer.getOrders();
for (int i = 0; i < currentCustomerOrders.size(); i++) {
final Order currentOrder = em.find(Order.class, currentCustomerOrders.get(i).getId());
List<OrderSession> orderSessionList = em
.createQuery("select p from OrderSession p where p.id.orderId = " + id, OrderSession.class)
.getResultList();
for (int j = 0; j < orderSessionList.size(); j++) {
final Session currentSession = orderSessionList.get(j).getSession();
currentOrder.getSessions().remove(orderSessionList.get(j));
currentSession.getOrders().remove(orderSessionList.get(j));
em.remove(orderSessionList.get(j));
em.merge(currentSession);
}
int ordSize = currentCustomer.getOrders().size();
for (int i = 0; i < ordSize; i++){
OrderCustomers temp = currentCustomer.getOrders().get(0);
temp.getCustomer().removeOrder(temp);
temp.getOrder().removeCustomers(temp);
em.remove(temp);
em.merge(currentOrder);
}
em.remove(currentCustomer);
return currentCustomer;

View File

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

View File

@ -1,6 +1,7 @@
package com.labwork1.app.student.service;
import com.labwork1.app.student.model.Cinema;
import com.labwork1.app.student.model.OrderSession;
import com.labwork1.app.student.model.Session;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
@ -56,6 +57,13 @@ public class SessionService {
@Transactional
public Session deleteSession(Long 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);
return currentSession;
}

View File

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