From 10735b0a92c5bad9d8ca95c23238a5e75a69bffd Mon Sep 17 00:00:00 2001 From: Wojtas1996PL Date: Tue, 15 Oct 2024 16:10:24 +0200 Subject: [PATCH 1/2] First solution --- src/main/java/mate/academy/Main.java | 51 +++++++++++++- src/main/java/mate/academy/dao/OrderDao.java | 10 +++ .../mate/academy/dao/impl/OrderDaoImpl.java | 54 +++++++++++++++ src/main/java/mate/academy/model/Order.java | 68 +++++++++++++++++++ .../mate/academy/service/OrderService.java | 12 ++++ .../service/impl/OrderServiceImpl.java | 44 ++++++++++++ src/main/resources/hibernate.cfg.xml | 18 +++++ 7 files changed, 254 insertions(+), 3 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..409d0de49 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -2,16 +2,26 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.List; +import mate.academy.lib.Injector; import mate.academy.model.CinemaHall; import mate.academy.model.Movie; import mate.academy.model.MovieSession; +import mate.academy.model.ShoppingCart; +import mate.academy.model.Ticket; +import mate.academy.model.User; import mate.academy.service.CinemaHallService; import mate.academy.service.MovieService; import mate.academy.service.MovieSessionService; +import mate.academy.service.OrderService; +import mate.academy.service.UserService; public class Main { + private static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { - MovieService movieService = null; + 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 +37,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 +55,46 @@ 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())); + + User bob = new User(); + bob.setPassword("password"); + bob.setEmail("bob.marley@gmail.com"); + User anna = new User(); + anna.setPassword("password"); + anna.setEmail("anna.haithway@gmail.com"); + UserService userService = (UserService) injector + .getInstance(UserService.class); + userService.add(bob); + userService.add(anna); + + Ticket terminator = new Ticket(); + terminator.setUser(bob); + terminator.setMovieSession(tomorrowMovieSession); + + Ticket missionImpossible = new Ticket(); + missionImpossible.setUser(anna); + missionImpossible.setMovieSession(yesterdayMovieSession); + + ShoppingCart shoppingCartBob = new ShoppingCart(); + shoppingCartBob.setUser(bob); + shoppingCartBob.setTickets(List.of(terminator)); + + ShoppingCart shoppingCartAnna = new ShoppingCart(); + shoppingCartAnna.setUser(anna); + shoppingCartAnna.setTickets(List.of(missionImpossible)); + + OrderService orderService = (OrderService) injector.getInstance(OrderService.class); + System.out.println(orderService.completeOrder(shoppingCartBob)); + System.out.println(orderService.completeOrder(shoppingCartAnna)); + System.out.println(orderService.getOrdersHistory(bob)); + System.out.println(orderService.getOrdersHistory(anna)); } } 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..98aa77eb8 --- /dev/null +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -0,0 +1,10 @@ +package mate.academy.dao; + +import mate.academy.model.Order; +import mate.academy.model.User; + +public interface OrderDao { + Order add(Order order); + + Order 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..f8463f771 --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -0,0 +1,54 @@ +package mate.academy.dao.impl; + +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("Can't insert an order: " + order, e); + } finally { + if (session != null) { + session.close(); + } + } + } + + @Override + public Order 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 o.user " + + "LEFT JOIN FETCH t.movieSession ms " + + "LEFT JOIN FETCH t.user " + + "LEFT JOIN FETCH ms.movie " + + "LEFT JOIN FETCH ms.cinemaHall " + + "WHERE o.user =:user", Order.class); + query.setParameter("user", user); + 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..fa748291a --- /dev/null +++ b/src/main/java/mate/academy/model/Order.java @@ -0,0 +1,68 @@ +package mate.academy.model; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +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(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) + private List tickets; + private LocalDateTime orderDate; + @ManyToOne(fetch = FetchType.LAZY) + 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/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..13e654dbb --- /dev/null +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -0,0 +1,44 @@ +package mate.academy.service.impl; + +import java.util.List; +import mate.academy.dao.OrderDao; +import mate.academy.dao.UserDao; +import mate.academy.exception.RegistrationException; +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.security.AuthenticationService; +import mate.academy.service.OrderService; + +@Service +public class OrderServiceImpl implements OrderService { + @Inject + private AuthenticationService authenticationService; + @Inject + private OrderDao orderDao; + @Inject + private UserDao userDao; + + @Override + public Order completeOrder(ShoppingCart shoppingCart) { + if (userDao.findByEmail(shoppingCart.getUser().getEmail()).isEmpty()) { + try { + authenticationService.register(shoppingCart.getUser().getEmail(), + shoppingCart.getUser().getPassword()); + } catch (RegistrationException e) { + throw new RuntimeException(e); + } + } + Order order = new Order(); + order.setUser(shoppingCart.getUser()); + order.setTickets(shoppingCart.getTickets()); + return orderDao.add(order); + } + + @Override + public List getOrdersHistory(User user) { + return List.of(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..cc0e3ea01 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,18 @@ + + + com.mysql.cj.jdbc.Driver + jdbc:mysql://127.0.0.1:3306/ticket_app?user=root + root + password + org.hibernate.dialect.MySQLDialect + true + create-drop + + + + + + + + + \ No newline at end of file From 092b4c09b4b486e422c77e221a6458dc679a6cd9 Mon Sep 17 00:00:00 2001 From: Wojtas1996PL Date: Wed, 16 Oct 2024 14:29:28 +0200 Subject: [PATCH 2/2] Second solution --- src/main/java/mate/academy/Main.java | 4 ++++ src/main/java/mate/academy/dao/OrderDao.java | 3 ++- .../mate/academy/dao/impl/OrderDaoImpl.java | 12 +++++----- .../service/impl/OrderServiceImpl.java | 23 ++++++------------- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 409d0de49..8e4793b9b 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -86,12 +86,16 @@ public static void main(String[] args) { ShoppingCart shoppingCartBob = new ShoppingCart(); shoppingCartBob.setUser(bob); shoppingCartBob.setTickets(List.of(terminator)); + shoppingCartBob.setId(bob.getId()); ShoppingCart shoppingCartAnna = new ShoppingCart(); shoppingCartAnna.setUser(anna); shoppingCartAnna.setTickets(List.of(missionImpossible)); + shoppingCartAnna.setId(anna.getId()); OrderService orderService = (OrderService) injector.getInstance(OrderService.class); + System.out.println(orderService.getOrdersHistory(bob)); + System.out.println(orderService.getOrdersHistory(anna)); System.out.println(orderService.completeOrder(shoppingCartBob)); System.out.println(orderService.completeOrder(shoppingCartAnna)); System.out.println(orderService.getOrdersHistory(bob)); diff --git a/src/main/java/mate/academy/dao/OrderDao.java b/src/main/java/mate/academy/dao/OrderDao.java index 98aa77eb8..85d775eb0 100644 --- a/src/main/java/mate/academy/dao/OrderDao.java +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -1,10 +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); - Order getByUser(User user); + 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 index f8463f771..9661fb042 100644 --- a/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -1,5 +1,6 @@ package mate.academy.dao.impl; +import java.util.List; import mate.academy.dao.OrderDao; import mate.academy.exception.DataProcessingException; import mate.academy.lib.Dao; @@ -8,7 +9,6 @@ import mate.academy.util.HibernateUtil; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.query.Query; @Dao public class OrderDaoImpl implements OrderDao { @@ -35,18 +35,18 @@ public Order add(Order order) { } @Override - public Order getByUser(User user) { + public List getByUser(User user) { try (Session session = HibernateUtil.getSessionFactory().openSession()) { - Query query = session.createQuery("FROM Order o " + return session.createQuery("FROM Order o " + "LEFT JOIN FETCH o.tickets t " + "LEFT JOIN FETCH o.user " + "LEFT JOIN FETCH t.movieSession ms " + "LEFT JOIN FETCH t.user " + "LEFT JOIN FETCH ms.movie " + "LEFT JOIN FETCH ms.cinemaHall " - + "WHERE o.user =:user", Order.class); - query.setParameter("user", user); - return query.uniqueResult(); + + "WHERE o.user =:user", Order.class) + .setParameter("user", user) + .getResultList(); } catch (Exception e) { throw new DataProcessingException("Can't find a shopping cart by user: " + user, e); } diff --git a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java index 13e654dbb..3a5490b01 100644 --- a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -1,44 +1,35 @@ package mate.academy.service.impl; +import java.util.ArrayList; import java.util.List; import mate.academy.dao.OrderDao; -import mate.academy.dao.UserDao; -import mate.academy.exception.RegistrationException; 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.security.AuthenticationService; import mate.academy.service.OrderService; +import mate.academy.service.ShoppingCartService; @Service public class OrderServiceImpl implements OrderService { - @Inject - private AuthenticationService authenticationService; @Inject private OrderDao orderDao; @Inject - private UserDao userDao; + private ShoppingCartService shoppingCartService; @Override public Order completeOrder(ShoppingCart shoppingCart) { - if (userDao.findByEmail(shoppingCart.getUser().getEmail()).isEmpty()) { - try { - authenticationService.register(shoppingCart.getUser().getEmail(), - shoppingCart.getUser().getPassword()); - } catch (RegistrationException e) { - throw new RuntimeException(e); - } - } Order order = new Order(); order.setUser(shoppingCart.getUser()); - order.setTickets(shoppingCart.getTickets()); + order.setTickets(new ArrayList<>(shoppingCart.getTickets())); + shoppingCartService.clearShoppingCart(shoppingCart); return orderDao.add(order); } @Override public List getOrdersHistory(User user) { - return List.of(orderDao.getByUser(user)); + return orderDao.getByUser(user); } + }