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

Add database setup and Order management implementation #831

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea/*
*.iml
target/*
target/*
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
<version>6.5.2.Final</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>9.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
<executions>
<execution>
<phase>compile</phase>
Expand All @@ -56,10 +56,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.13.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<release>${jdk.version}</release>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
Expand Down
51 changes: 39 additions & 12 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package mate.academy;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
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.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 TEST_EMAIL = "test@email.com";
private static final String TEST_PASSWORD = "password";

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.");
movieService.add(fastAndFurious);
System.out.println(movieService.get(fastAndFurious.getId()));
movieService.getAll().forEach(System.out::println);

CinemaHall firstCinemaHall = new CinemaHall();
firstCinemaHall.setCapacity(100);
Expand All @@ -27,13 +36,11 @@ 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);

System.out.println(cinemaHallService.getAll());
System.out.println(cinemaHallService.get(firstCinemaHall.getId()));

MovieSession tomorrowMovieSession = new MovieSession();
tomorrowMovieSession.setCinemaHall(firstCinemaHall);
tomorrowMovieSession.setMovie(fastAndFurious);
Expand All @@ -44,12 +51,32 @@ 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()));
try {
AuthenticationService authenticationService = (AuthenticationService)
INJECTOR.getInstance(AuthenticationService.class);
authenticationService.register(TEST_EMAIL, TEST_PASSWORD);
User user = authenticationService.login(TEST_EMAIL, TEST_PASSWORD);

ShoppingCartService shoppingCartService = (ShoppingCartService)
INJECTOR.getInstance(ShoppingCartService.class);
shoppingCartService.addSession(tomorrowMovieSession, user);
shoppingCartService.addSession(yesterdayMovieSession, user);
shoppingCartService.addSession(yesterdayMovieSession, user);

ShoppingCart userShoppingCart = shoppingCartService.getByUser(user);

OrderService orderService = (OrderService) INJECTOR.getInstance(OrderService.class);
orderService.completeOrder(userShoppingCart);
List<Order> userOrderList = orderService.getOrdersHistory(user);
userOrderList.forEach(System.out::println);

} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
}
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> getAllByUser(User user);
}
55 changes: 55 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,55 @@
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("Can't add order: " + order, e);
} finally {
if (session != null) {
session.close();
}
}
return order;
}

@Override
public List<Order> getAllByUser(User user) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
Query<Order> orderQuery = session.createQuery("FROM Order or "
+ "LEFT JOIN FETCH or.tickets tc "
+ "LEFT JOIN FETCH tc.movieSession ms "
+ "LEFT JOIN FETCH ms.movie "
+ "LEFT JOIN FETCH ms.cinemaHall "
+ "LEFT JOIN FETCH or.user u "
+ "WHERE or.user =: user",
Order.class);
orderQuery.setParameter("user", user);
return orderQuery.getResultList();
} catch (Exception e) {
throw new DataProcessingException("Can't get all orders by user: " + user, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void update(ShoppingCart shoppingCart) {
try {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.update(shoppingCart);
session.merge(shoppingCart);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/mate/academy/model/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package mate.academy.model;

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.JoinTable;
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
@JoinTable(name = "orders_tickets",
joinColumns = @JoinColumn(name = "orders_id"),
inverseJoinColumns = @JoinColumn(name = "ticket_id"))
private List<Ticket> tickets;
private LocalDateTime orderDate;
@ManyToOne(fetch = FetchType.LAZY)
private User user;

public Order() {
}

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);
}
37 changes: 37 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,37 @@
package mate.academy.service.impl;

import java.time.LocalDateTime;
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.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.setUser(shoppingCart.getUser());
order.setTickets(new ArrayList<>(shoppingCart.getTickets()));
order.setOrderDate(LocalDateTime.now());
orderDao.add(order);
shoppingCartService.clearShoppingCart(shoppingCart);
return order;
}

@Override
public List<Order> getOrdersHistory(User user) {
return orderDao.getAllByUser(user);
}
}
22 changes: 22 additions & 0 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ticket_app</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping class="mate.academy.model.Movie"/>
<mapping class="mate.academy.model.CinemaHall"/>
<mapping class="mate.academy.model.User"/>
<mapping class="mate.academy.model.MovieSession"/>
<mapping class="mate.academy.model.Ticket"/>
<mapping class="mate.academy.model.ShoppingCart"/>
<mapping class="mate.academy.model.Order"/>
</session-factory>
</hibernate-configuration>
Loading