Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hibernate order hw implementation #769

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
12 changes: 7 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<hibernate-core.version>6.4.4.Final</hibernate-core.version>
<mysql-connector-j.version>8.3.0</mysql-connector-j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
<version>${hibernate-core.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql-connector-j.version}</version>
</dependency>
</dependencies>
<build>
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

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");
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 =
(MovieService) INJECTOR.getInstance(MovieService.class);

Movie fastAndFurious = new Movie("Fast and Furious");
fastAndFurious.setDescription("An action film about street racing, heists, and spies.");
Expand All @@ -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);

Expand All @@ -44,12 +58,29 @@ 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);
User user = authenticationService.register(USER_EMAIL, USER_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<Order> ordersHistory = orderService.getOrdersHistory(user);
System.out.println(ordersHistory);
}
}
11 changes: 11 additions & 0 deletions src/main/java/mate/academy/dao/OrderDao.java
Original file line number Diff line number Diff line change
@@ -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<Order> getByUser(User user);
}
9 changes: 6 additions & 3 deletions src/main/java/mate/academy/dao/impl/CinemaHallDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -37,7 +40,7 @@ public CinemaHall add(CinemaHall cinemaHall) {

@Override
public Optional<CinemaHall> 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);
Expand All @@ -46,7 +49,7 @@ public Optional<CinemaHall> get(Long id) {

@Override
public List<CinemaHall> getAll() {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
try (Session session = sessionFactory.openSession()) {
CriteriaQuery<CinemaHall> criteriaQuery = session.getCriteriaBuilder()
.createQuery(CinemaHall.class);
criteriaQuery.from(CinemaHall.class);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/mate/academy/dao/impl/MovieDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -37,7 +40,7 @@ public Movie add(Movie movie) {

@Override
public Optional<Movie> 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);
Expand All @@ -46,7 +49,7 @@ public Optional<Movie> get(Long id) {

@Override
public List<Movie> getAll() {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
try (Session session = sessionFactory.openSession()) {
CriteriaQuery<Movie> criteriaQuery = session.getCriteriaBuilder()
.createQuery(Movie.class);
criteriaQuery.from(Movie.class);
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/mate/academy/dao/impl/MovieSessionDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -45,7 +47,7 @@ public MovieSession add(MovieSession movieSession) {

@Override
public List<MovieSession> findAvailableSessions(Long movieId, LocalDate date) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
try (Session session = sessionFactory.openSession()) {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<MovieSession> criteriaQuery =
criteriaBuilder.createQuery(MovieSession.class);
Expand All @@ -66,7 +68,7 @@ public List<MovieSession> findAvailableSessions(Long movieId, LocalDate date) {

@Override
public Optional<MovieSession> get(Long id) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
try (Session session = sessionFactory.openSession()) {
Query<MovieSession> query = session.createQuery("FROM MovieSession ms "
+ "JOIN FETCH ms.movie m "
+ "JOIN FETCH ms.cinemaHall ch "
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/mate/academy/dao/impl/OrderDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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;

@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();
} 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<Order> 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
);
}
}
}
9 changes: 6 additions & 3 deletions src/main/java/mate/academy/dao/impl/ShoppingCartDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<ShoppingCart> query = session.createQuery("FROM ShoppingCart sc "
+ "LEFT JOIN FETCH sc.tickets t "
+ "LEFT JOIN FETCH t.movieSession ms "
Expand All @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/mate/academy/dao/impl/TicketDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/mate/academy/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -36,7 +39,7 @@ public User add(User user) {

@Override
public Optional<User> findByEmail(String email) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
try (Session session = sessionFactory.openSession()) {
Query<User> query = session.createQuery("FROM User u "
+ "WHERE u.email = :email", User.class);
query.setParameter("email", email);
Expand Down
Loading
Loading