From d7c0a41087d1119517cec723826dfdb9a918f171 Mon Sep 17 00:00:00 2001 From: Nikita Khilobok Date: Sun, 13 Oct 2024 17:02:20 +0300 Subject: [PATCH] hw is done, please check --- src/main/java/mate/academy/Main.java | 35 ++++++++- src/main/java/mate/academy/dao/OrderDao.java | 11 +++ .../mate/academy/dao/impl/OrderDaoImpl.java | 53 +++++++++++++ .../academy/dao/impl/ShoppingCartDaoImpl.java | 2 +- src/main/java/mate/academy/model/Order.java | 74 +++++++++++++++++++ .../mate/academy/service/OrderService.java | 12 +++ .../service/impl/OrderServiceImpl.java | 37 ++++++++++ src/main/resources/hibernate.cfg.xml | 19 +++++ 8 files changed, 239 insertions(+), 4 deletions(-) create mode 100644 src/main/java/mate/academy/dao/OrderDao.java create mode 100644 src/main/java/mate/academy/dao/impl/OrderDaoImpl.java create mode 100644 src/main/java/mate/academy/model/Order.java create mode 100644 src/main/java/mate/academy/service/OrderService.java create mode 100644 src/main/java/mate/academy/service/impl/OrderServiceImpl.java create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index df492aecc..a5c270246 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -2,16 +2,25 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.List; +import mate.academy.exception.RegistrationException; +import mate.academy.lib.Injector; import mate.academy.model.CinemaHall; import mate.academy.model.Movie; import mate.academy.model.MovieSession; +import mate.academy.model.Order; +import mate.academy.model.User; +import mate.academy.security.AuthenticationService; import mate.academy.service.CinemaHallService; import mate.academy.service.MovieService; import mate.academy.service.MovieSessionService; +import mate.academy.service.OrderService; +import mate.academy.service.ShoppingCartService; public class Main { public static void main(String[] args) { - MovieService movieService = null; + Injector injector = Injector.getInstance("mate.academy"); + MovieService movieService = (MovieService) injector.getInstance(MovieService.class); Movie fastAndFurious = new Movie("Fast and Furious"); fastAndFurious.setDescription("An action film about street racing, heists, and spies."); @@ -27,7 +36,8 @@ public static void main(String[] args) { secondCinemaHall.setCapacity(200); secondCinemaHall.setDescription("second hall with capacity 200"); - CinemaHallService cinemaHallService = null; + CinemaHallService cinemaHallService = (CinemaHallService) injector + .getInstance(CinemaHallService.class); cinemaHallService.add(firstCinemaHall); cinemaHallService.add(secondCinemaHall); @@ -44,12 +54,31 @@ public static void main(String[] args) { yesterdayMovieSession.setMovie(fastAndFurious); yesterdayMovieSession.setShowTime(LocalDateTime.now().minusDays(1L)); - MovieSessionService movieSessionService = null; + MovieSessionService movieSessionService = (MovieSessionService) injector + .getInstance(MovieSessionService.class); movieSessionService.add(tomorrowMovieSession); movieSessionService.add(yesterdayMovieSession); System.out.println(movieSessionService.get(yesterdayMovieSession.getId())); System.out.println(movieSessionService.findAvailableSessions( fastAndFurious.getId(), LocalDate.now())); + + AuthenticationService authenticationService = (AuthenticationService) injector + .getInstance(AuthenticationService.class); + + User user = null; + try { + user = authenticationService.register("user@gmail.com", "qwerty"); + } catch (RegistrationException e) { + throw new RuntimeException("Incorrect email or password!", e); + } + + ShoppingCartService shoppingCartService = (ShoppingCartService) injector + .getInstance(ShoppingCartService.class); + shoppingCartService.addSession(tomorrowMovieSession, user); + + OrderService orderService = (OrderService) injector.getInstance(OrderService.class); + List orders = orderService.getOrdersHistory(user); + orders.forEach(System.out::println); } } diff --git a/src/main/java/mate/academy/dao/OrderDao.java b/src/main/java/mate/academy/dao/OrderDao.java new file mode 100644 index 000000000..a406dd5a8 --- /dev/null +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -0,0 +1,11 @@ +package mate.academy.dao; + +import java.util.List; +import mate.academy.model.Order; +import mate.academy.model.User; + +public interface OrderDao { + Order add(Order order); + + List getByUser(User user); +} diff --git a/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java new file mode 100644 index 000000000..51051770f --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -0,0 +1,53 @@ +package mate.academy.dao.impl; + +import java.util.List; +import mate.academy.dao.OrderDao; +import mate.academy.exception.DataProcessingException; +import mate.academy.lib.Dao; +import mate.academy.model.Order; +import mate.academy.model.User; +import mate.academy.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.query.Query; + +@Dao +public class OrderDaoImpl implements OrderDao { + @Override + public Order add(Order order) { + Session session = null; + Transaction transaction = null; + try { + session = HibernateUtil.getSessionFactory().openSession(); + transaction = session.beginTransaction(); + session.persist(order); + transaction.commit(); + return order; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't insert a order: " + order, e); + } finally { + if (session != null) { + session.close(); + } + } + } + + @Override + public List getByUser(User user) { + try (Session session = HibernateUtil.getSessionFactory().openSession()) { + Query queryOfOrders = session.createQuery("SELECT DISTINCT o FROM Order o " + + "LEFT JOIN FETCH o.tickets t " + + "LEFT JOIN FETCH t.movieSession ms " + + "LEFT JOIN FETCH ms.movie m " + + "LEFT JOIN FETCH ms.cinemaHall ch " + + "Where o.user = :user", Order.class); + queryOfOrders.setParameter("user", user); + return queryOfOrders.getResultList(); + } catch (Exception e) { + throw new DataProcessingException("Can't find order with user " + user, e); + } + } +} diff --git a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java index fa1380642..a0b1a062e 100644 --- a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java @@ -59,7 +59,7 @@ public void update(ShoppingCart shoppingCart) { try { session = HibernateUtil.getSessionFactory().openSession(); transaction = session.beginTransaction(); - session.update(shoppingCart); + session.merge(shoppingCart); transaction.commit(); } catch (Exception e) { if (transaction != null) { diff --git a/src/main/java/mate/academy/model/Order.java b/src/main/java/mate/academy/model/Order.java new file mode 100644 index 000000000..41728356d --- /dev/null +++ b/src/main/java/mate/academy/model/Order.java @@ -0,0 +1,74 @@ +package mate.academy.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import java.time.LocalDateTime; +import java.util.List; + +@Entity +@Table(name = "orders") +public class Order { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @OneToMany + private List tickets; + @Column(name = "orderDate") + private LocalDateTime orderDate; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; + + public Order() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getTickets() { + return tickets; + } + + public void setTickets(List tickets) { + this.tickets = tickets; + } + + public LocalDateTime getOrderDate() { + return orderDate; + } + + public void setOrderDate(LocalDateTime orderDate) { + this.orderDate = orderDate; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + @Override + public String toString() { + return "Order{" + + "id=" + id + + ", tickets=" + tickets + + ", orderDate=" + orderDate + + ", user=" + user + + '}'; + } +} diff --git a/src/main/java/mate/academy/service/OrderService.java b/src/main/java/mate/academy/service/OrderService.java new file mode 100644 index 000000000..511364933 --- /dev/null +++ b/src/main/java/mate/academy/service/OrderService.java @@ -0,0 +1,12 @@ +package mate.academy.service; + +import java.util.List; +import mate.academy.model.Order; +import mate.academy.model.ShoppingCart; +import mate.academy.model.User; + +public interface OrderService { + Order completeOrder(ShoppingCart shoppingCart); + + List getOrdersHistory(User user); +} diff --git a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java new file mode 100644 index 000000000..626070851 --- /dev/null +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -0,0 +1,37 @@ +package mate.academy.service.impl; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import mate.academy.dao.OrderDao; +import mate.academy.lib.Inject; +import mate.academy.lib.Service; +import mate.academy.model.Order; +import mate.academy.model.ShoppingCart; +import mate.academy.model.User; +import mate.academy.service.OrderService; +import mate.academy.service.ShoppingCartService; + +@Service +public class OrderServiceImpl implements OrderService { + @Inject + private OrderDao orderDao; + @Inject + private ShoppingCartService shoppingCartService; + + @Override + public Order completeOrder(ShoppingCart shoppingCart) { + Order order = new Order(); + order.setOrderDate(LocalDateTime.now()); + order.setUser(shoppingCart.getUser()); + order.setTickets(new ArrayList<>(shoppingCart.getTickets())); + Order completeOrder = orderDao.add(order); + shoppingCartService.clearShoppingCart(shoppingCart); + return completeOrder; + } + + @Override + public List getOrdersHistory(User user) { + return orderDao.getByUser(user); + } +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..bfa1f935d --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,19 @@ + + + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost/cinema?serverTimezone=UTC + com.mysql.cj.jdbc.Driver + root + Nk102030! + true + update + + + + + + + + + + \ No newline at end of file