diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index df492aecc..b4aa5cbcc 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -2,16 +2,28 @@ 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.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"); + private static final String USER_EMAIL = "test@aa.bb"; + private static final String USER_PASSWORD = "1234"; + 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 +39,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 +57,43 @@ 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); + try { + authenticationService.register(USER_EMAIL, USER_PASSWORD); + } catch (RegistrationException e) { + throw new RuntimeException("Can't register new user with email: " + USER_EMAIL, e); + } + + User user = null; + try { + user = authenticationService.login(USER_EMAIL, USER_PASSWORD); + } catch (AuthenticationException e) { + throw new RuntimeException("Can't login with email: " + USER_EMAIL, e); + } + + ShoppingCartService shoppingCartService + = (ShoppingCartService) INJECTOR.getInstance(ShoppingCartService.class); + + ShoppingCart shoppingCart = shoppingCartService.getByUser(user); + System.out.println(shoppingCart); + + shoppingCartService.addSession(tomorrowMovieSession, user); + shoppingCart = shoppingCartService.getByUser(user); + System.out.println(shoppingCart); + + OrderService orderService = (OrderService) INJECTOR.getInstance(OrderService.class); + orderService.completeOrder(shoppingCart); + + System.out.println(orderService.getOrdersHistory(user)); } } 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..f221fecac --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -0,0 +1,57 @@ +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.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.query.Query; + +@Dao +public class OrderDaoImpl implements OrderDao { + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + + @Override + public Order add(Order order) { + Session session = null; + Transaction transaction = null; + try { + session = sessionFactory.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 List getByUser(User user) { + try (Session session = sessionFactory.openSession()) { + Query query = session.createQuery("from Order o " + + "JOIN FETCH o.user u " + + "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("Can't find orders 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..b5a4d41be --- /dev/null +++ b/src/main/java/mate/academy/model/Order.java @@ -0,0 +1,75 @@ +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 + @JoinColumn(name = "order_id") + private List tickets; + + @Column(name = "order_date") + private LocalDateTime orderDate; + + @ManyToOne(fetch = FetchType.LAZY) + @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/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..1d7ef642a --- /dev/null +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -0,0 +1,35 @@ +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; +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.setTickets(List.copyOf(shoppingCart.getTickets())); + order.setUser(shoppingCart.getUser()); + order.setOrderDate(LocalDateTime.now()); + 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..5929d8afb --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,18 @@ + + + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/ticket_app?serverTimeZone=UTC + root + 12345678 + true + create-drop + + + + + + + + + +