From 548f71b39e02c7047ea0a54d1ee6e414387ccfe0 Mon Sep 17 00:00:00 2001 From: Jakub Date: Wed, 14 Aug 2024 19:08:22 +0100 Subject: [PATCH 1/2] first attempt --- src/main/java/mate/academy/Main.java | 49 +++++++++++- src/main/java/mate/academy/dao/OrderDao.java | 11 +++ .../mate/academy/dao/impl/OrderDaoImpl.java | 54 ++++++++++++++ .../academy/dao/impl/ShoppingCartDaoImpl.java | 6 +- src/main/java/mate/academy/model/Order.java | 74 +++++++++++++++++++ .../java/mate/academy/model/ShoppingCart.java | 5 +- .../mate/academy/service/OrderService.java | 12 +++ .../service/impl/OrderServiceImpl.java | 32 ++++++++ src/main/resources/hibernate.cfg.xml | 19 +++++ 9 files changed, 255 insertions(+), 7 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..ecef4fbf2 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -2,16 +2,30 @@ 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.ShoppingCart; +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 { + + private static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { - MovieService movieService = null; + + final 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 +41,8 @@ public static void main(String[] args) { secondCinemaHall.setCapacity(200); secondCinemaHall.setDescription("second hall with capacity 200"); - CinemaHallService cinemaHallService = null; + final CinemaHallService cinemaHallService = + (CinemaHallService) injector.getInstance(CinemaHallService.class); cinemaHallService.add(firstCinemaHall); cinemaHallService.add(secondCinemaHall); @@ -44,12 +59,40 @@ public static void main(String[] args) { yesterdayMovieSession.setMovie(fastAndFurious); yesterdayMovieSession.setShowTime(LocalDateTime.now().minusDays(1L)); - MovieSessionService movieSessionService = null; + final 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())); + + final AuthenticationService authenticationService = + (AuthenticationService) injector.getInstance(AuthenticationService.class); + + User mateUser = null; + try { + mateUser = authenticationService.register("mate.academy@gmail.com", "mate"); + } catch (RegistrationException e) { + throw new RuntimeException("Registration failed", e); + } + System.out.println(mateUser); + + final ShoppingCartService shoppingCartService = + (ShoppingCartService) injector.getInstance(ShoppingCartService.class); + + shoppingCartService.addSession(tomorrowMovieSession, mateUser); + ShoppingCart usersShoppingCart = shoppingCartService.getByUser(mateUser); + System.out.println(usersShoppingCart); + + final OrderService orderService = + (OrderService) injector.getInstance(OrderService.class); + + Order order = orderService.completeOrder(usersShoppingCart); + System.out.println(order); + + List ordersHistory = orderService.getOrdersHistory(mateUser); + System.out.println(ordersHistory); } } 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..85d775eb0 --- /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..3428fbd00 --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -0,0 +1,54 @@ +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) { + Transaction transaction = null; + Session session = 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("Error adding order: " + order, e); + } finally { + if (session != null) { + session.close(); + } + } + } + + @Override + public List getByUser(User user) { + try (Session session = HibernateUtil.getSessionFactory().openSession()) { + Query query = session.createQuery("FROM Order o " + + "LEFT JOIN FETCH o.tickets t " + + "LEFT JOIN FETCH t.movieSession ms " + + "LEFT JOIN FETCH ms.movie " + + "LEFT JOIN FETCH ms.cinemaHall " + + "WHERE o.user =:user", Order.class); + query.setParameter("user", user); + return query.getResultList(); + } catch (Exception e) { + throw new DataProcessingException("Error retrieving orders from" + + "database for 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..0dd0888cd 100644 --- a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java @@ -44,8 +44,10 @@ public ShoppingCart getByUser(User user) { + "LEFT JOIN FETCH t.movieSession ms " + "LEFT JOIN FETCH ms.movie " + "LEFT JOIN FETCH ms.cinemaHall " - + "WHERE sc.user =:user", ShoppingCart.class); - query.setParameter("user", user); + + " LEFT JOIN FETCH t.user " + + "LEFT JOIN FETCH sc.user u " + + "WHERE u.id =:userID", ShoppingCart.class); + query.setParameter("userID", user.getId()); return query.uniqueResult(); } catch (Exception e) { throw new DataProcessingException("Can't find a shopping cart by user: " + user, e); 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..03ececaa5 --- /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.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +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 + @JoinTable(name = "order_tickets", + joinColumns = @JoinColumn(name = "order_id"), + inverseJoinColumns = @JoinColumn(name = "ticket_id")) + private List tickets; + @Column (name = "order_date") + private LocalDateTime orderDate; + @ManyToOne + @JoinColumn(name = "user_id") + private User user; + + 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/model/ShoppingCart.java b/src/main/java/mate/academy/model/ShoppingCart.java index 0c7a73d48..e29c58e4e 100644 --- a/src/main/java/mate/academy/model/ShoppingCart.java +++ b/src/main/java/mate/academy/model/ShoppingCart.java @@ -1,6 +1,7 @@ package mate.academy.model; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; @@ -20,9 +21,9 @@ public class ShoppingCart { joinColumns = @JoinColumn(name = "shopping_cart_id"), inverseJoinColumns = @JoinColumn(name = "ticket_id")) private List tickets; - @OneToOne + @OneToOne (fetch = FetchType.LAZY) @MapsId - @JoinColumn(name = "id") + @JoinColumn(name = "user_id") private User user; public Long getId() { 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..6c66484ec --- /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..2c5cfd0d0 --- /dev/null +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -0,0 +1,32 @@ +package mate.academy.service.impl; + +import java.time.LocalDateTime; +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; + +@Service +public class OrderServiceImpl implements OrderService { + @Inject + private OrderDao orderDao; + + @Override + public Order completeOrder(ShoppingCart shoppingCart) { + Order order = new Order(); + order.setUser(shoppingCart.getUser()); + order.setTickets(shoppingCart.getTickets()); + order.setOrderDate(LocalDateTime.now()); + return orderDao.add(order); + } + + @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..ac4b7dbe9 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,19 @@ + + + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost:3306/cinema?serverTimezone=UTC + com.mysql.cj.jdbc.Driver + root + Styczen10! + true + create-drop + + + + + + + + + + \ No newline at end of file From e5342ef7726826d7d878e703c85af7ea5e31611e Mon Sep 17 00:00:00 2001 From: Jakub Date: Thu, 15 Aug 2024 20:21:38 +0100 Subject: [PATCH 2/2] - createOrder method created for OrderServiceImpl - removed empty lines and extra spaces in main and ShoppingCartDaoImpl --- src/main/java/mate/academy/Main.java | 1 - .../mate/academy/dao/impl/ShoppingCartDaoImpl.java | 2 +- .../mate/academy/service/impl/OrderServiceImpl.java | 12 ++++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index ecef4fbf2..563325e22 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -19,7 +19,6 @@ import mate.academy.service.ShoppingCartService; public class Main { - private static final Injector injector = Injector.getInstance("mate.academy"); public static void main(String[] args) { diff --git a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java index 0dd0888cd..0ad8c0435 100644 --- a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java @@ -44,7 +44,7 @@ public ShoppingCart getByUser(User user) { + "LEFT JOIN FETCH t.movieSession ms " + "LEFT JOIN FETCH ms.movie " + "LEFT JOIN FETCH ms.cinemaHall " - + " LEFT JOIN FETCH t.user " + + "LEFT JOIN FETCH t.user " + "LEFT JOIN FETCH sc.user u " + "WHERE u.id =:userID", ShoppingCart.class); query.setParameter("userID", user.getId()); diff --git a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java index 2c5cfd0d0..fd589ee0a 100644 --- a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -17,10 +17,7 @@ public class OrderServiceImpl implements OrderService { @Override public Order completeOrder(ShoppingCart shoppingCart) { - Order order = new Order(); - order.setUser(shoppingCart.getUser()); - order.setTickets(shoppingCart.getTickets()); - order.setOrderDate(LocalDateTime.now()); + Order order = createOrder(shoppingCart); return orderDao.add(order); } @@ -29,4 +26,11 @@ public List getOrdersHistory(User user) { return orderDao.getByUser(user); } + private Order createOrder(ShoppingCart shoppingCart) { + Order order = new Order(); + order.setUser(shoppingCart.getUser()); + order.setTickets(shoppingCart.getTickets()); + order.setOrderDate(LocalDateTime.now()); + return order; + } }