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

finished task #785

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 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,25 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import mate.academy.lib.Injector;
import mate.academy.model.CinemaHall;
import mate.academy.model.Movie;
import mate.academy.model.MovieSession;
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.ShoppingCartService;
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.");
Expand All @@ -27,7 +36,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 +54,42 @@ 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
.get(yesterdayMovieSession.getId()));
System.out.println(movieSessionService.findAvailableSessions(
fastAndFurious.getId(), LocalDate.now()));

User user1 = new User();
user1.setEmail("user1@gmail.com");
user1.setPassword("user1pass");
User user2 = new User();
user2.setEmail("user2@gmail.com");
user2.setPassword("user2pass");

UserService userService = (UserService) injector
.getInstance(UserService.class);
userService.add(user1);
userService.add(user2);

ShoppingCartService shoppingCartService =
(ShoppingCartService) injector.getInstance(ShoppingCartService.class);
shoppingCartService.registerNewShoppingCart(user1);
shoppingCartService.registerNewShoppingCart(user2);
shoppingCartService.addSession(tomorrowMovieSession, user1);
shoppingCartService.addSession(tomorrowMovieSession, user2);

OrderService orderService =
(OrderService) injector.getInstance(OrderService.class);
System.out.println(orderService
.completeOrder(shoppingCartService.getByUser(user1)));
System.out.println(orderService
.completeOrder(shoppingCartService.getByUser(user2)));

System.out.println(orderService.getOrdersHistory(user1));
}
}
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);
}
54 changes: 54 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,54 @@
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.Transaction;
import org.hibernate.query.Query;

@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();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Cannot add order: " + order, e);
} finally {
if (session != null) {
session.close();
}
}
return order;
}

@Override
public List<Order> getByUser(User user) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
Query<Order> createOrderQuery = session.createQuery("from Order o "
+ "JOIN FETCH o.tickets t "
+ "JOIN FETCH t.movieSession ms "
+ "JOIN FETCH ms.cinemaHall "
+ "JOIN FETCH ms.movie "
+ "WHERE o.user = :user", Order.class);
createOrderQuery.setParameter("user", user);
return createOrderQuery.getResultList();
} catch (Exception e) {
throw new DataProcessingException("Cannot get orders by user: " + user, e);
}
}
}
66 changes: 66 additions & 0 deletions src/main/java/mate/academy/model/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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<Ticket> tickets;
private LocalDateTime orderDate;
@ManyToOne
private User user;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List<Ticket> getTickets() {
return tickets;
}

public void setTickets(List<Ticket> 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
+ "}";
}
}
12 changes: 12 additions & 0 deletions src/main/java/mate/academy/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -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<Order> getOrdersHistory(User user);
}
35 changes: 35 additions & 0 deletions src/main/java/mate/academy/service/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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.setOrderDate(LocalDateTime.now());
order.setTickets(shoppingCart.getTickets());
order.setUser(shoppingCart.getUser());
shoppingCartService.clearShoppingCart(shoppingCart);
return orderDao.add(order);
}

@Override
public List<Order> getOrdersHistory(User user) {
return orderDao.getByUser(user);
}
}
19 changes: 19 additions & 0 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/order?serverTimezone=UTC</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">bookPass</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>

<mapping class="mate.academy.model.User"/>
<mapping class="mate.academy.model.MovieSession"/>
<mapping class="mate.academy.model.Movie"/>
<mapping class="mate.academy.model.CinemaHall"/>
<mapping class="mate.academy.model.Ticket"/>
<mapping class="mate.academy.model.ShoppingCart"/>
<mapping class="mate.academy.model.Order"/>
</session-factory>
</hibernate-configuration>
Loading