From a03e4c54588d1911cdd501d2dd96a4b646f03f3e Mon Sep 17 00:00:00 2001 From: Slava_Moiseev Date: Sat, 18 May 2024 14:59:04 +0300 Subject: [PATCH 1/4] commit for chat ask --- src/main/java/mate/academy/Main.java | 43 +++++++++++++- src/main/java/mate/academy/dao/OrderDao.java | 12 ++++ .../mate/academy/dao/impl/OrderDaoImpl.java | 48 ++++++++++++++++ src/main/java/mate/academy/model/Order.java | 57 +++++++++++++++++++ .../mate/academy/service/OrderService.java | 13 +++++ .../service/impl/OrderServiceImpl.java | 36 ++++++++++++ src/main/resources/hibernate.cfg.xml | 19 +++++++ 7 files changed, 225 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..50a7ff038 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -2,16 +2,29 @@ import java.time.LocalDate; import java.time.LocalDateTime; + +import mate.academy.exception.AuthenticationException; +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.Ticket; +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; + 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 +40,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 +58,35 @@ 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); + ShoppingCartService shoppingCartService = (ShoppingCartService) injector + .getInstance(ShoppingCartService.class); + User user = new User(); + try { + user = authenticationService.register("visitor@.com", "qwerty"); + } catch (RegistrationException e) { + throw new RuntimeException("Can`t register new user", e); + } + + Ticket ticket = new Ticket(); + ticket.setMovieSession(tomorrowMovieSession); + ShoppingCart shoppingCart = shoppingCartService.getByUser(user); + + OrderService orderService = (OrderService) injector + .getInstance(OrderService.class); + + orderService.completeOrder(shoppingCart); + + Order order = orderService.completeOrder(shoppingCart); } } 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..c206c2167 --- /dev/null +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -0,0 +1,12 @@ +package mate.academy.dao; + +import mate.academy.model.Order; +import mate.academy.model.User; + +import java.util.List; + +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..fb341f982 --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -0,0 +1,48 @@ +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; +import java.util.List; + +@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 create new order: " + order, e); + } finally { + if (session != null) { + session.close(); + + } + } + } + + @Override + public List getByUser(User user) { + try (Session session = HibernateUtil.getSessionFactory().openSession()) { + Query orderQuery = session.createQuery( + "FROM Order o JOIN FETCH o.user WHERE o.user =:user", Order.class) + .setParameter("user", user); + return orderQuery.getResultList(); + } + } +} 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..05565960d --- /dev/null +++ b/src/main/java/mate/academy/model/Order.java @@ -0,0 +1,57 @@ +package mate.academy.model; + +import jakarta.persistence.Entity; +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 + private List ticketList; + private LocalDateTime orderDate; + @ManyToOne + private User user; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getTicketList() { + return ticketList; + } + + public void setTicketList(List ticketList) { + this.ticketList = ticketList; + } + + 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; + } +} 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..3086e2940 --- /dev/null +++ b/src/main/java/mate/academy/service/OrderService.java @@ -0,0 +1,13 @@ +package mate.academy.service; + +import mate.academy.model.Order; +import mate.academy.model.ShoppingCart; +import mate.academy.model.User; + +import java.util.List; + +public interface OrderService { + Order completeOrder(ShoppingCart shoppingCart); + + List getOrdersHistory(User user); +} \ No newline at end of file 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..607da7f86 --- /dev/null +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -0,0 +1,36 @@ +package mate.academy.service.impl; + +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.Ticket; +import mate.academy.model.User; +import mate.academy.service.OrderService; +import mate.academy.service.ShoppingCartService; + +import java.util.ArrayList; +import java.util.List; + +@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.setUser(shoppingCart.getUser()); + order.setTicketList(new ArrayList<>(shoppingCart.getTickets())); + shoppingCartService.clearShoppingCart(shoppingCart); + 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..cbfb7c774 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,19 @@ + + + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost:3306/test + com.mysql.cj.jdbc.Driver + root + rfrfirf + true + create-drop + + + + + + + + + + \ No newline at end of file From 1723820a0aba0bc9f233b8b375c8badc11072412 Mon Sep 17 00:00:00 2001 From: Slava_Moiseev Date: Sat, 18 May 2024 15:18:05 +0300 Subject: [PATCH 2/4] Finished. Tests - build, but run main - error. why? screen in chat --- src/main/java/mate/academy/Main.java | 5 ++--- src/main/java/mate/academy/dao/OrderDao.java | 3 +-- src/main/java/mate/academy/dao/impl/OrderDaoImpl.java | 2 +- src/main/java/mate/academy/model/Order.java | 1 - src/main/java/mate/academy/service/OrderService.java | 5 ++--- .../java/mate/academy/service/impl/OrderServiceImpl.java | 6 ++---- 6 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 50a7ff038..e24358189 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -2,8 +2,6 @@ import java.time.LocalDate; import java.time.LocalDateTime; - -import mate.academy.exception.AuthenticationException; import mate.academy.exception.RegistrationException; import mate.academy.lib.Injector; import mate.academy.model.CinemaHall; @@ -22,6 +20,7 @@ public class Main { private static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { MovieService movieService = (MovieService) injector .getInstance(MovieService.class); @@ -86,7 +85,7 @@ public static void main(String[] args) { .getInstance(OrderService.class); orderService.completeOrder(shoppingCart); - Order order = orderService.completeOrder(shoppingCart); + System.out.println(order); } } diff --git a/src/main/java/mate/academy/dao/OrderDao.java b/src/main/java/mate/academy/dao/OrderDao.java index c206c2167..85d775eb0 100644 --- a/src/main/java/mate/academy/dao/OrderDao.java +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -1,10 +1,9 @@ package mate.academy.dao; +import java.util.List; import mate.academy.model.Order; import mate.academy.model.User; -import java.util.List; - public interface OrderDao { Order add(Order order); diff --git a/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java index fb341f982..da0791e0c 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; @@ -9,7 +10,6 @@ import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.Query; -import java.util.List; @Dao public class OrderDaoImpl implements OrderDao { diff --git a/src/main/java/mate/academy/model/Order.java b/src/main/java/mate/academy/model/Order.java index 05565960d..43666983b 100644 --- a/src/main/java/mate/academy/model/Order.java +++ b/src/main/java/mate/academy/model/Order.java @@ -7,7 +7,6 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; - import java.time.LocalDateTime; import java.util.List; diff --git a/src/main/java/mate/academy/service/OrderService.java b/src/main/java/mate/academy/service/OrderService.java index 3086e2940..6c66484ec 100644 --- a/src/main/java/mate/academy/service/OrderService.java +++ b/src/main/java/mate/academy/service/OrderService.java @@ -1,13 +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; -import java.util.List; - public interface OrderService { Order completeOrder(ShoppingCart shoppingCart); List getOrdersHistory(User user); -} \ No newline at end of file +} diff --git a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java index 607da7f86..dfd72398c 100644 --- a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -1,18 +1,16 @@ package mate.academy.service.impl; +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.Ticket; import mate.academy.model.User; import mate.academy.service.OrderService; import mate.academy.service.ShoppingCartService; -import java.util.ArrayList; -import java.util.List; - @Service public class OrderServiceImpl implements OrderService { @Inject From f2beb836b43e9495812ef072e459c58b850ce753 Mon Sep 17 00:00:00 2001 From: Slava_Moiseev Date: Sun, 19 May 2024 11:10:47 +0300 Subject: [PATCH 3/4] Refactor code --- src/main/java/mate/academy/Main.java | 2 +- src/main/java/mate/academy/model/Order.java | 3 ++- src/main/java/mate/academy/service/impl/OrderServiceImpl.java | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index e24358189..ce87639bd 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -70,7 +70,7 @@ public static void main(String[] args) { .getInstance(AuthenticationService.class); ShoppingCartService shoppingCartService = (ShoppingCartService) injector .getInstance(ShoppingCartService.class); - User user = new User(); + User user; try { user = authenticationService.register("visitor@.com", "qwerty"); } catch (RegistrationException e) { diff --git a/src/main/java/mate/academy/model/Order.java b/src/main/java/mate/academy/model/Order.java index 43666983b..929c14931 100644 --- a/src/main/java/mate/academy/model/Order.java +++ b/src/main/java/mate/academy/model/Order.java @@ -1,6 +1,7 @@ package mate.academy.model; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -19,7 +20,7 @@ public class Order { @OneToMany private List ticketList; private LocalDateTime orderDate; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) private User user; public Long getId() { diff --git a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java index dfd72398c..699eefc52 100644 --- a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -1,5 +1,6 @@ package mate.academy.service.impl; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import mate.academy.dao.OrderDao; @@ -22,6 +23,7 @@ public class OrderServiceImpl implements OrderService { public Order completeOrder(ShoppingCart shoppingCart) { Order order = new Order(); order.setUser(shoppingCart.getUser()); + order.setOrderDate(LocalDateTime.now()); order.setTicketList(new ArrayList<>(shoppingCart.getTickets())); shoppingCartService.clearShoppingCart(shoppingCart); return orderDao.add(order); From e42e395528993477b3f7ca4084f1589804e9a7ca Mon Sep 17 00:00:00 2001 From: Slava_Moiseev Date: Wed, 12 Jun 2024 21:43:32 +0300 Subject: [PATCH 4/4] Refactor code --- src/main/java/mate/academy/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index ce87639bd..ff2b1381c 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -87,5 +87,6 @@ public static void main(String[] args) { orderService.completeOrder(shoppingCart); Order order = orderService.completeOrder(shoppingCart); System.out.println(order); + System.out.println(order); } }