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.
order entity with dao impl and service impl
- Loading branch information
Showing
7 changed files
with
341 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,125 @@ | ||
package mate.academy; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import mate.academy.exception.AuthenticationException; | ||
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.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; | ||
import mate.academy.service.UserService; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
MovieService movieService = null; | ||
|
||
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); | ||
firstCinemaHall.setDescription("first hall with capacity 100"); | ||
|
||
CinemaHall secondCinemaHall = new CinemaHall(); | ||
secondCinemaHall.setCapacity(200); | ||
secondCinemaHall.setDescription("second hall with capacity 200"); | ||
|
||
CinemaHallService cinemaHallService = null; | ||
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); | ||
tomorrowMovieSession.setShowTime(LocalDateTime.now().plusDays(1L)); | ||
|
||
MovieSession yesterdayMovieSession = new MovieSession(); | ||
yesterdayMovieSession.setCinemaHall(firstCinemaHall); | ||
yesterdayMovieSession.setMovie(fastAndFurious); | ||
yesterdayMovieSession.setShowTime(LocalDateTime.now().minusDays(1L)); | ||
|
||
MovieSessionService movieSessionService = null; | ||
movieSessionService.add(tomorrowMovieSession); | ||
movieSessionService.add(yesterdayMovieSession); | ||
|
||
System.out.println(movieSessionService.get(yesterdayMovieSession.getId())); | ||
System.out.println(movieSessionService.findAvailableSessions( | ||
fastAndFurious.getId(), LocalDate.now())); | ||
Injector injector = Injector.getInstance("mate.academy"); | ||
|
||
final AuthenticationService authenticationService = | ||
(AuthenticationService) injector.getInstance(AuthenticationService.class); | ||
|
||
final CinemaHallService cinemaHallService = | ||
(CinemaHallService) injector.getInstance(CinemaHallService.class); | ||
|
||
final MovieService movieService = | ||
(MovieService) injector.getInstance(MovieService.class); | ||
|
||
final MovieSessionService movieSessionService = | ||
(MovieSessionService) injector.getInstance(MovieSessionService.class); | ||
|
||
final UserService userService = | ||
(UserService) injector.getInstance(UserService.class); | ||
|
||
final ShoppingCartService shoppingCartService = | ||
(ShoppingCartService) injector.getInstance(ShoppingCartService.class); | ||
|
||
final OrderService orderService = | ||
(OrderService) injector.getInstance(OrderService.class); | ||
|
||
try { | ||
String email = "john.doe@example.com"; | ||
String password = "password123"; | ||
User registeredUser = authenticationService.register(email, password); | ||
System.out.println("User registered: " + registeredUser); | ||
|
||
User loggedInUser = authenticationService.login(email, password); | ||
System.out.println("User logged in: " + loggedInUser); | ||
|
||
CinemaHall cinemaHall1 = new CinemaHall(); | ||
cinemaHall1.setCapacity(100); | ||
cinemaHall1.setDescription("Large hall for blockbuster movies"); | ||
cinemaHallService.add(cinemaHall1); | ||
|
||
CinemaHall cinemaHall2 = new CinemaHall(); | ||
cinemaHall2.setCapacity(50); | ||
cinemaHall2.setDescription("Small hall for indie movies"); | ||
cinemaHallService.add(cinemaHall2); | ||
|
||
Movie movie1 = new Movie(); | ||
movie1.setTitle("Avengers: Endgame"); | ||
movie1.setDescription("Superhero movie with epic battles"); | ||
movieService.add(movie1); | ||
|
||
Movie movie2 = new Movie(); | ||
movie2.setTitle("Parasite"); | ||
movie2.setDescription("Award-winning Korean thriller"); | ||
movieService.add(movie2); | ||
|
||
MovieSession session1 = new MovieSession(); | ||
session1.setMovie(movie1); | ||
session1.setCinemaHall(cinemaHall1); | ||
session1.setShowTime(LocalDateTime.now().plusDays(1)); | ||
movieSessionService.add(session1); | ||
|
||
MovieSession session2 = new MovieSession(); | ||
session2.setMovie(movie2); | ||
session2.setCinemaHall(cinemaHall2); | ||
session2.setShowTime(LocalDateTime.now().plusDays(2)); | ||
movieSessionService.add(session2); | ||
|
||
shoppingCartService.addSession(session1, loggedInUser); | ||
shoppingCartService.addSession(session2, loggedInUser); | ||
|
||
ShoppingCart shoppingCart = shoppingCartService.getByUser(loggedInUser); | ||
Order order = orderService.completeOrder(shoppingCart); | ||
System.out.println("\nOrder created:"); | ||
System.out.println(order); | ||
|
||
List<CinemaHall> cinemaHalls = cinemaHallService.getAll(); | ||
System.out.println("\nCinema Halls:"); | ||
cinemaHalls.forEach(System.out::println); | ||
|
||
List<Movie> movies = movieService.getAll(); | ||
System.out.println("\nMovies:"); | ||
movies.forEach(System.out::println); | ||
|
||
List<MovieSession> movieSessions = movieSessionService | ||
.findAvailableSessions(movie1.getId(), | ||
LocalDateTime.now().plusDays(1).toLocalDate()); | ||
System.out.println("\nMovie Sessions:"); | ||
movieSessions.forEach(System.out::println); | ||
|
||
System.out.println("\nUser:"); | ||
System.out.println(loggedInUser); | ||
|
||
shoppingCart = shoppingCartService.getByUser(loggedInUser); | ||
System.out.println("\nShopping Cart:"); | ||
System.out.println(shoppingCart); | ||
|
||
List<Order> ordersHistory = orderService.getOrdersHistory(loggedInUser); | ||
System.out.println("\nOrders History:"); | ||
ordersHistory.forEach(System.out::println); | ||
|
||
} catch (AuthenticationException | RegistrationException e) { | ||
System.err.println("Error: " + e.getMessage()); | ||
} | ||
} | ||
} |
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,47 @@ | ||
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; | ||
try (Session 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 add order: " + order, e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Order> getByUser(User user) { | ||
try (Session session = HibernateUtil.getSessionFactory().openSession()) { | ||
String hql = "FROM Order o " | ||
+ "LEFT JOIN FETCH o.tickets t " | ||
+ "LEFT JOIN FETCH t.movieSession ms " | ||
+ "LEFT JOIN FETCH ms.cinemaHall ch " | ||
+ "LEFT JOIN FETCH ms.movie " | ||
+ "WHERE o.user = :user"; | ||
return session.createQuery(hql, Order.class) | ||
.setParameter("user", user) | ||
.getResultList(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't retrieve orders for 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,87 @@ | ||
package mate.academy.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.JoinTable; | ||
import jakarta.persistence.ManyToMany; | ||
import jakarta.persistence.OneToOne; | ||
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; | ||
@ManyToMany | ||
@JoinTable(name = "orders_tickets", | ||
joinColumns = @JoinColumn(name = "order_id"), | ||
inverseJoinColumns = @JoinColumn(name = "ticket_id")) | ||
private List<Ticket> tickets; | ||
private LocalDateTime orderDate; | ||
@OneToOne | ||
private User user; | ||
|
||
public Order() { | ||
} | ||
|
||
private Order(List<Ticket> tickets, LocalDateTime orderDate, User user) { | ||
this.tickets = tickets; | ||
this.orderDate = orderDate; | ||
this.user = user; | ||
} | ||
|
||
public static Order createOrder(List<Ticket> tickets, LocalDateTime orderDate, User user) { | ||
return new Order(tickets, orderDate, user); | ||
} | ||
|
||
public Long id() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public List<Ticket> tickets() { | ||
return tickets; | ||
} | ||
|
||
public void setTickets(List<Ticket> tickets) { | ||
this.tickets = tickets; | ||
} | ||
|
||
public LocalDateTime orderDate() { | ||
return orderDate; | ||
} | ||
|
||
public void setOrderDate(LocalDateTime orderDate) { | ||
this.orderDate = orderDate; | ||
} | ||
|
||
public User user() { | ||
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); | ||
} |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
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 newOrder = Order.createOrder( | ||
new ArrayList<>(shoppingCart.getTickets()), | ||
LocalDateTime.now(), | ||
shoppingCart.getUser() | ||
); | ||
|
||
Order savedOrder = orderDao.add(newOrder); | ||
if (savedOrder != null) { | ||
shoppingCartService.clearShoppingCart(shoppingCart); | ||
} | ||
return savedOrder; | ||
} | ||
|
||
@Override | ||
public List<Order> getOrdersHistory(User user) { | ||
return orderDao.getByUser(user); | ||
} | ||
} |
Oops, something went wrong.