generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
252 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
Transaction transaction = null; | ||
Session session = null; | ||
try { | ||
session = HibernateUtil.getSessionFactory().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<Order> getByUser(User user) { | ||
try (Session session = HibernateUtil.getSessionFactory().openSession()) { | ||
Query<Order> query = session.createQuery("FROM Order o " | ||
+ "LEFT JOIN FETCH o.tickets t " | ||
+ "LEFT JOIN FETCH o.user u " | ||
+ "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 an order by user: " + user, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package mate.academy.model; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
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; | ||
|
||
@Entity | ||
@Table(name = "orders") | ||
public class Order { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@OneToMany | ||
@JoinTable(name = "orders_tickets", | ||
joinColumns = @JoinColumn(name = "order_id"), | ||
inverseJoinColumns = @JoinColumn(name = "ticket_id")) | ||
private List<Ticket> tickets; | ||
private LocalDateTime orderDate; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
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 | ||
+ '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
src/main/java/mate/academy/service/impl/OrderServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.setTickets(shoppingCart.getTickets()); | ||
order.setOrderDate(LocalDateTime.now()); | ||
order.setUser(shoppingCart.getUser()); | ||
shoppingCartService.clearShoppingCart(shoppingCart); | ||
return orderDao.add(order); | ||
} | ||
|
||
@Override | ||
public List<Order> getOrdersHistory(User user) { | ||
return orderDao.getByUser(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE hibernate-configuration PUBLIC | ||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" | ||
"http://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/cinema?serverTimezone=UTC</property> | ||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">mate12345</property> | ||
<property name="show_sql">true</property> | ||
<property name="hbm2ddl.auto">create-drop</property> | ||
|
||
<mapping class="mate.academy.model.CinemaHall"/> | ||
<mapping class="mate.academy.model.Movie"/> | ||
<mapping class="mate.academy.model.MovieSession"/> | ||
<mapping class="mate.academy.model.Order"/> | ||
<mapping class="mate.academy.model.ShoppingCart"/> | ||
<mapping class="mate.academy.model.Ticket"/> | ||
<mapping class="mate.academy.model.User"/> | ||
</session-factory> | ||
</hibernate-configuration> |