From adfabd52733f16a0a6f022f1f1bfa52605238153 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 15:44:52 +0300 Subject: [PATCH 01/10] updated hibernate-core and mysql-connector-java dependencies --- pom.xml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 1d545e7aa..82eb3aa40 100644 --- a/pom.xml +++ b/pom.xml @@ -15,18 +15,20 @@ https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml + 6.4.4.Final + 8.3.0 - org.hibernate + org.hibernate.orm hibernate-core - 6.2.7.Final + ${hibernate-core.version} - mysql - mysql-connector-java - 8.0.33 + com.mysql + mysql-connector-j + ${mysql-connector-j.version} From b2575602d7f0eae2e9f63692de5b29592dc7df0f Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 15:45:17 +0300 Subject: [PATCH 02/10] created hibernate configuration xml file --- src/main/resources/hibernate.cfg.xml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..fd190d1dd --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,24 @@ + + + + + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost:3306/cinema?createDatabaseIfNotExist=true + com.mysql.cj.jdbc.Driver + root + 0000 + true + true + create-drop + + + + + + + + + + From 04335886546be3168d8e7840adaa506571a2d6d1 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 15:45:32 +0300 Subject: [PATCH 03/10] created Order entity class --- src/main/java/mate/academy/model/Order.java | 78 +++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/main/java/mate/academy/model/Order.java 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..3217c7c1c --- /dev/null +++ b/src/main/java/mate/academy/model/Order.java @@ -0,0 +1,78 @@ +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 tickets; + + private LocalDateTime orderDateTime; + + @ManyToOne + private User user; + + public Order() { + } + + public Order(List tickets, LocalDateTime orderDateTime, User user) { + this.tickets = tickets; + this.orderDateTime = orderDateTime; + this.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 getOrderDateTime() { + return orderDateTime; + } + + public void setOrderDateTime(LocalDateTime orderDateTime) { + this.orderDateTime = orderDateTime; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + @Override + public String toString() { + return "Order{" + + "id=" + id + + ", tickets=" + tickets + + ", orderDateTime=" + orderDateTime + + ", user=" + user + + '}'; + } +} From fe9b485313e97b39ec4d057b657a77fc1a9bf4af Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 15:45:46 +0300 Subject: [PATCH 04/10] created OrderDao interface --- src/main/java/mate/academy/dao/OrderDao.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/mate/academy/dao/OrderDao.java 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..7ae7bfe37 --- /dev/null +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -0,0 +1,11 @@ +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); +} From 53f0dc2b4bc9442eab140e8b59fac4238ea37616 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 15:46:16 +0300 Subject: [PATCH 05/10] created OrderDaoImpl class and implemented add and getByUser methods --- .../mate/academy/dao/impl/OrderDaoImpl.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/mate/academy/dao/impl/OrderDaoImpl.java 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..b10183b5e --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -0,0 +1,57 @@ +package mate.academy.dao.impl; + +import mate.academy.dao.OrderDao; +import mate.academy.exception.DataProcessingException; +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 java.util.List; + +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(); + } catch (Exception ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException( + String.format("Can`t add a new order: %s to the DB", order), ex); + } finally { + if (session != null) { + session.close(); + } + } + return order; + } + + @Override + public List getByUser(User user) { + try (Session session = sessionFactory.openSession()) { + return session.createQuery("FROM Order o " + + "JOIN FETCH o.tickets t " + + "JOIN FETCH t.movieSession ms " + + "JOIN FETCH ms.movie " + + "JOIN FETCH ms.cinemaHall " + + "JOIN FETCH o.user u " + + "WHERE u.id = :userId", Order.class) + .setParameter("userId", user.getId()) + .getResultList(); + } catch (Exception ex) { + throw new DataProcessingException( + String.format("Can`t get orders by user: %s", user), ex + ); + } + } +} From 73eee513acd6b6b2c812b5402b56e3e451239be2 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 21:39:06 +0300 Subject: [PATCH 06/10] Hibernate order hw implementation --- src/main/java/mate/academy/Main.java | 40 +++++++++++++++++-- .../academy/dao/impl/CinemaHallDaoImpl.java | 9 +++-- .../mate/academy/dao/impl/MovieDaoImpl.java | 9 +++-- .../academy/dao/impl/MovieSessionDaoImpl.java | 8 ++-- .../mate/academy/dao/impl/OrderDaoImpl.java | 2 + .../academy/dao/impl/ShoppingCartDaoImpl.java | 9 +++-- .../mate/academy/dao/impl/TicketDaoImpl.java | 5 ++- .../mate/academy/dao/impl/UserDaoImpl.java | 7 +++- .../mate/academy/service/OrderService.java | 12 ++++++ .../service/impl/OrderServiceImpl.java | 37 +++++++++++++++++ 10 files changed, 119 insertions(+), 19 deletions(-) create mode 100644 src/main/java/mate/academy/service/OrderService.java create mode 100644 src/main/java/mate/academy/service/impl/OrderServiceImpl.java diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index df492aecc..ffcb1cc86 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 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; + private static final Injector INJECTOR = Injector.getInstance("mate.academy"); + + public static void main(String[] args) throws RegistrationException { + 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,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); + String email = "testemail@email.com"; + String password = "testpassword"; + User user = authenticationService.register(email, password); + + ShoppingCartService shoppingCartService = + (ShoppingCartService) INJECTOR.getInstance(ShoppingCartService.class); + shoppingCartService.addSession(yesterdayMovieSession, user); + shoppingCartService.addSession(tomorrowMovieSession, user); + + OrderService orderService = + (OrderService) INJECTOR.getInstance(OrderService.class); + Order order = orderService.completeOrder(shoppingCartService.getByUser(user)); + System.out.println(order); + List ordersHistory = orderService.getOrdersHistory(user); + System.out.println(ordersHistory); } } diff --git a/src/main/java/mate/academy/dao/impl/CinemaHallDaoImpl.java b/src/main/java/mate/academy/dao/impl/CinemaHallDaoImpl.java index cd1ae9efb..8b4a01b38 100644 --- a/src/main/java/mate/academy/dao/impl/CinemaHallDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/CinemaHallDaoImpl.java @@ -9,16 +9,19 @@ import mate.academy.model.CinemaHall; import mate.academy.util.HibernateUtil; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.Transaction; @Dao public class CinemaHallDaoImpl implements CinemaHallDao { + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + @Override public CinemaHall add(CinemaHall cinemaHall) { Session session = null; Transaction transaction = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.persist(cinemaHall); transaction.commit(); @@ -37,7 +40,7 @@ public CinemaHall add(CinemaHall cinemaHall) { @Override public Optional get(Long id) { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { return Optional.ofNullable(session.get(CinemaHall.class, id)); } catch (Exception e) { throw new DataProcessingException("Can't get a cinema hall by id: " + id, e); @@ -46,7 +49,7 @@ public Optional get(Long id) { @Override public List getAll() { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { CriteriaQuery criteriaQuery = session.getCriteriaBuilder() .createQuery(CinemaHall.class); criteriaQuery.from(CinemaHall.class); diff --git a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java index 47e59a001..5561c65e6 100644 --- a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java @@ -9,16 +9,19 @@ import mate.academy.model.Movie; import mate.academy.util.HibernateUtil; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.Transaction; @Dao public class MovieDaoImpl implements MovieDao { + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + @Override public Movie add(Movie movie) { Transaction transaction = null; Session session = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.persist(movie); transaction.commit(); @@ -37,7 +40,7 @@ public Movie add(Movie movie) { @Override public Optional get(Long id) { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { return Optional.ofNullable(session.get(Movie.class, id)); } catch (Exception e) { throw new DataProcessingException("Can't get a movie by id: " + id, e); @@ -46,7 +49,7 @@ public Optional get(Long id) { @Override public List getAll() { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { CriteriaQuery criteriaQuery = session.getCriteriaBuilder() .createQuery(Movie.class); criteriaQuery.from(Movie.class); diff --git a/src/main/java/mate/academy/dao/impl/MovieSessionDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieSessionDaoImpl.java index a573202d0..27beb751c 100644 --- a/src/main/java/mate/academy/dao/impl/MovieSessionDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/MovieSessionDaoImpl.java @@ -14,19 +14,21 @@ import mate.academy.model.MovieSession; 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 MovieSessionDaoImpl implements MovieSessionDao { private static final LocalTime END_OF_DAY = LocalTime.of(23, 59, 59); + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); @Override public MovieSession add(MovieSession movieSession) { Transaction transaction = null; Session session = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.persist(movieSession); transaction.commit(); @@ -45,7 +47,7 @@ public MovieSession add(MovieSession movieSession) { @Override public List findAvailableSessions(Long movieId, LocalDate date) { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(MovieSession.class); @@ -66,7 +68,7 @@ public List findAvailableSessions(Long movieId, LocalDate date) { @Override public Optional get(Long id) { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { Query query = session.createQuery("FROM MovieSession ms " + "JOIN FETCH ms.movie m " + "JOIN FETCH ms.cinemaHall ch " diff --git a/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java index b10183b5e..37a7f73da 100644 --- a/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/OrderDaoImpl.java @@ -2,6 +2,7 @@ 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; @@ -10,6 +11,7 @@ import org.hibernate.Transaction; import java.util.List; +@Dao public class OrderDaoImpl implements OrderDao { private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); diff --git a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java index fa1380642..b0efc6a5b 100644 --- a/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java @@ -7,17 +7,20 @@ 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 ShoppingCartDaoImpl implements ShoppingCartDao { + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + @Override public ShoppingCart add(ShoppingCart shoppingCart) { Session session = null; Transaction transaction = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); User managedUser = session.merge(shoppingCart.getUser()); shoppingCart.setUser(managedUser); @@ -38,7 +41,7 @@ public ShoppingCart add(ShoppingCart shoppingCart) { @Override public ShoppingCart getByUser(User user) { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { Query query = session.createQuery("FROM ShoppingCart sc " + "LEFT JOIN FETCH sc.tickets t " + "LEFT JOIN FETCH t.movieSession ms " @@ -57,7 +60,7 @@ public void update(ShoppingCart shoppingCart) { Session session = null; Transaction transaction = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.update(shoppingCart); transaction.commit(); diff --git a/src/main/java/mate/academy/dao/impl/TicketDaoImpl.java b/src/main/java/mate/academy/dao/impl/TicketDaoImpl.java index ad7a494a5..88d0967bb 100644 --- a/src/main/java/mate/academy/dao/impl/TicketDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/TicketDaoImpl.java @@ -6,16 +6,19 @@ import mate.academy.model.Ticket; import mate.academy.util.HibernateUtil; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.Transaction; @Dao public class TicketDaoImpl implements TicketDao { + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + @Override public Ticket add(Ticket ticket) { Session session = null; Transaction transaction = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.persist(ticket); transaction.commit(); diff --git a/src/main/java/mate/academy/dao/impl/UserDaoImpl.java b/src/main/java/mate/academy/dao/impl/UserDaoImpl.java index f0de09673..27b46c90f 100644 --- a/src/main/java/mate/academy/dao/impl/UserDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/UserDaoImpl.java @@ -7,17 +7,20 @@ 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 UserDaoImpl implements UserDao { + private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + @Override public User add(User user) { Session session = null; Transaction transaction = null; try { - session = HibernateUtil.getSessionFactory().openSession(); + session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.persist(user); transaction.commit(); @@ -36,7 +39,7 @@ public User add(User user) { @Override public Optional findByEmail(String email) { - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { Query query = session.createQuery("FROM User u " + "WHERE u.email = :email", User.class); query.setParameter("email", email); 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..d0138aeae --- /dev/null +++ b/src/main/java/mate/academy/service/OrderService.java @@ -0,0 +1,12 @@ +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); +} 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..705e142fd --- /dev/null +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -0,0 +1,37 @@ +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.User; +import mate.academy.service.OrderService; +import mate.academy.service.ShoppingCartService; + +import java.time.LocalDateTime; +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.setTickets(List.copyOf(shoppingCart.getTickets())); + order.setOrderDateTime(LocalDateTime.now()); + shoppingCartService.clearShoppingCart(shoppingCart); + return orderDao.add(order); + } + + @Override + public List getOrdersHistory(User user) { + return orderDao.getByUser(user); + } +} From 50d619c83abadbddf79fec53b07ca8fab8c45810 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Thu, 11 Apr 2024 21:44:17 +0300 Subject: [PATCH 07/10] checkstyle code changes --- src/main/java/mate/academy/Main.java | 1 - src/main/java/mate/academy/dao/OrderDao.java | 2 +- .../java/mate/academy/dao/impl/OrderDaoImpl.java | 2 +- src/main/java/mate/academy/model/Order.java | 12 ++++++------ src/main/java/mate/academy/service/OrderService.java | 2 +- .../mate/academy/service/impl/OrderServiceImpl.java | 5 ++--- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index ffcb1cc86..a4fc00dec 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -3,7 +3,6 @@ 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; diff --git a/src/main/java/mate/academy/dao/OrderDao.java b/src/main/java/mate/academy/dao/OrderDao.java index 7ae7bfe37..85d775eb0 100644 --- a/src/main/java/mate/academy/dao/OrderDao.java +++ b/src/main/java/mate/academy/dao/OrderDao.java @@ -1,8 +1,8 @@ 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 37a7f73da..72a8df00d 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.SessionFactory; import org.hibernate.Transaction; -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 3217c7c1c..be690a260 100644 --- a/src/main/java/mate/academy/model/Order.java +++ b/src/main/java/mate/academy/model/Order.java @@ -68,11 +68,11 @@ public void setUser(User user) { @Override public String toString() { - return "Order{" + - "id=" + id + - ", tickets=" + tickets + - ", orderDateTime=" + orderDateTime + - ", user=" + user + - '}'; + return "Order{" + + "id=" + id + + ", tickets=" + tickets + + ", orderDateTime=" + orderDateTime + + ", user=" + user + + '}'; } } diff --git a/src/main/java/mate/academy/service/OrderService.java b/src/main/java/mate/academy/service/OrderService.java index d0138aeae..6c66484ec 100644 --- a/src/main/java/mate/academy/service/OrderService.java +++ b/src/main/java/mate/academy/service/OrderService.java @@ -1,9 +1,9 @@ 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); diff --git a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java index 705e142fd..87fef98f8 100644 --- a/src/main/java/mate/academy/service/impl/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/impl/OrderServiceImpl.java @@ -1,5 +1,7 @@ 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; @@ -9,9 +11,6 @@ import mate.academy.service.OrderService; import mate.academy.service.ShoppingCartService; -import java.time.LocalDateTime; -import java.util.List; - @Service public class OrderServiceImpl implements OrderService { @Inject From f186dc10455bad38400153735cc64dfe50ace968 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Fri, 12 Apr 2024 14:09:12 +0300 Subject: [PATCH 08/10] Changed the code in relation to my colleague's comments --- src/main/java/mate/academy/Main.java | 6 +++--- src/main/resources/hibernate.cfg.xml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index a4fc00dec..e6a967357 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -19,6 +19,8 @@ public class Main { private static final Injector INJECTOR = Injector.getInstance("mate.academy"); + private static final String USER_EMAIL = "testemail@email.com"; + private static final String USER_PASSWORD = "testpassword"; public static void main(String[] args) throws RegistrationException { MovieService movieService = @@ -67,9 +69,7 @@ public static void main(String[] args) throws RegistrationException { AuthenticationService authenticationService = (AuthenticationService) INJECTOR.getInstance(AuthenticationService.class); - String email = "testemail@email.com"; - String password = "testpassword"; - User user = authenticationService.register(email, password); + User user = authenticationService.register(USER_EMAIL, USER_PASSWORD); ShoppingCartService shoppingCartService = (ShoppingCartService) INJECTOR.getInstance(ShoppingCartService.class); diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index fd190d1dd..c0d0d252b 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -7,8 +7,8 @@ org.hibernate.dialect.MySQLDialect jdbc:mysql://localhost:3306/cinema?createDatabaseIfNotExist=true com.mysql.cj.jdbc.Driver - root - 0000 + USE-YOUR-DB-USERNAME + USE-YOUR-DB-PASSWORD true true create-drop From f2638b06452798e16eb22c79db30bebdaa26f5ef Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Fri, 12 Apr 2024 20:09:52 +0300 Subject: [PATCH 09/10] added @JoinColumn annotation to the User field and @CreationTimestamp annotation to the LocalDateTime field --- src/main/java/mate/academy/model/Order.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/mate/academy/model/Order.java b/src/main/java/mate/academy/model/Order.java index be690a260..7f117f90c 100644 --- a/src/main/java/mate/academy/model/Order.java +++ b/src/main/java/mate/academy/model/Order.java @@ -4,9 +4,12 @@ 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 org.hibernate.annotations.CreationTimestamp; + import java.time.LocalDateTime; import java.util.List; @@ -20,9 +23,11 @@ public class Order { @OneToMany private List tickets; + @CreationTimestamp private LocalDateTime orderDateTime; @ManyToOne + @JoinColumn(name = "user_id") private User user; public Order() { From 98fc87808c277d0eeba092cecc720ed23d3f13f5 Mon Sep 17 00:00:00 2001 From: Valentyn Sharshon Date: Fri, 12 Apr 2024 20:13:49 +0300 Subject: [PATCH 10/10] checkstyle code changes --- src/main/java/mate/academy/model/Order.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/mate/academy/model/Order.java b/src/main/java/mate/academy/model/Order.java index 7f117f90c..47ee1a2bc 100644 --- a/src/main/java/mate/academy/model/Order.java +++ b/src/main/java/mate/academy/model/Order.java @@ -8,10 +8,9 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; -import org.hibernate.annotations.CreationTimestamp; - import java.time.LocalDateTime; import java.util.List; +import org.hibernate.annotations.CreationTimestamp; @Entity @Table(name = "orders")