-
Notifications
You must be signed in to change notification settings - Fork 796
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
First solution #834
base: master
Are you sure you want to change the base?
First solution #834
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
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; | ||
|
||
@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()) { | ||
return session.createQuery("FROM Order o " | ||
+ "LEFT JOIN FETCH o.tickets t " | ||
+ "LEFT JOIN FETCH o.user " | ||
+ "LEFT JOIN FETCH t.movieSession ms " | ||
+ "LEFT JOIN FETCH t.user " | ||
+ "LEFT JOIN FETCH ms.movie " | ||
+ "LEFT JOIN FETCH ms.cinemaHall " | ||
+ "WHERE o.user =:user", Order.class) | ||
.setParameter("user", user) | ||
.getResultList(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't find a shopping cart by user: " + user, e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception message is incorrect. It should say 'Can't find orders by user' instead of 'Can't find a shopping cart by user'. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package mate.academy.model; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
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(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) | ||
private List<Ticket> tickets; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The relationship between Order and Ticket entities is not explicitly mapped. The 'mappedBy' attribute should be used in @onetomany annotation to specify the field that owns the relationship. |
||
private LocalDateTime orderDate; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private User user; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The relationship between Order and User entities is not clearly defined. Consider adding 'cascade' attribute in @manytoone annotation and specify CascadeType as needed. |
||
|
||
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{" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including associated entities in the toString() method may lead to LazyInitializationException. Consider excluding 'tickets' and 'user' from toString(). |
||
+ "id=" + id | ||
+ ", tickets=" + tickets | ||
+ ", orderDate=" + orderDate | ||
+ ", user=" + user | ||
+ '}'; | ||
} | ||
} |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mate.academy.service.impl; | ||
|
||
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())); | ||
shoppingCartService.clearShoppingCart(shoppingCart); | ||
return orderDao.add(order); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order is not saved in the database, so it can be lost after the method completeOrder is finished. You should add saving the order in the database. |
||
} | ||
|
||
@Override | ||
public List<Order> getOrdersHistory(User user) { | ||
return orderDao.getByUser(user); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<hibernate-configuration> | ||
<session-factory> | ||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ticket_app?user=root</property> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">password</property> | ||
<property name="dialect">org.hibernate.dialect.MySQLDialect</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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also call getOrdersHistory before completing order.