приколы
This commit is contained in:
parent
cfe166c85a
commit
465dcc3fae
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -0,0 +1,26 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CinemaDto {
|
||||
private final long id;
|
||||
private final String name;
|
||||
private final List<SessionDto> sessions;
|
||||
public CinemaDto(Cinema cinema) {
|
||||
this.id = cinema.getId();
|
||||
this.name = cinema.getName();
|
||||
this.sessions = cinema.getSessions().stream()
|
||||
.map(SessionDto::new).toList();
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public List<SessionDto> getSessions() {
|
||||
return sessions;
|
||||
}
|
||||
}
|
@ -43,4 +43,4 @@ public class CustomerController {
|
||||
public CustomerDto deleteCustomer(@PathVariable Long id) {
|
||||
return new CustomerDto(customerService.deleteCustomer(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ public class CustomerDto {
|
||||
this.password = customer.getPassword();
|
||||
this.orders = customer.getOrders();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
import com.labwork1.app.student.model.Order;
|
||||
import com.labwork1.app.student.service.CustomerService;
|
||||
import com.labwork1.app.student.service.OrderService;
|
||||
import com.labwork1.app.student.service.TicketService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@ -35,16 +31,11 @@ public class OrderController {
|
||||
return new OrderDto(orderService.addOrder(customer));
|
||||
}
|
||||
|
||||
/*@PutMapping("/{id}/updcustomer")
|
||||
public OrderDto updateOrderCustomer(@PathVariable Long id,
|
||||
@RequestParam("customer") Long customer) {
|
||||
return new OrderDto(orderService.addCustomer(id, customer));
|
||||
}*/
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public OrderDto updateOrder(@PathVariable Long id,
|
||||
@RequestParam("ticket") Long ticket) {
|
||||
return new OrderDto(orderService.addTicket(id, ticket));
|
||||
@RequestParam("session") Long session,
|
||||
@RequestParam("count") Integer count) {
|
||||
return new OrderDto(orderService.addSession(id, session, count));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
@ -10,14 +10,15 @@ public class OrderDto {
|
||||
private final long id;
|
||||
private final Date dateOfPurchase;
|
||||
private final Customer customer;
|
||||
private final List<TicketDto> tickets;
|
||||
private final List<OrderSessionDto> sessions;
|
||||
public OrderDto(Order order) {
|
||||
this.id = order.getId();
|
||||
this.dateOfPurchase = order.getDateOfPurchase();
|
||||
this.customer = order.getCustomer();
|
||||
this.tickets = order.getTickets()
|
||||
this.sessions = order.getSessions()
|
||||
.stream()
|
||||
.map(x -> new TicketDto(x)).toList();
|
||||
.map(x -> new OrderSessionDto(x.getId().getSessionId(),
|
||||
x.getId().getOrderId(), x.getCount())).toList();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
@ -29,7 +30,7 @@ public class OrderDto {
|
||||
public Customer getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
public List<TicketDto> getTickets() {
|
||||
return tickets;
|
||||
public List<OrderSessionDto> getSessions() {
|
||||
return sessions;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
public class OrderSessionDto {
|
||||
private Long sessionId;
|
||||
private Long orderId;
|
||||
private Integer count;
|
||||
|
||||
public OrderSessionDto() {
|
||||
}
|
||||
public OrderSessionDto(Long sessionId, Long orderId, Integer count) {
|
||||
this.sessionId = sessionId;
|
||||
this.orderId = orderId;
|
||||
this.count = count;
|
||||
}
|
||||
public Long getSessionId() {
|
||||
return sessionId;
|
||||
}
|
||||
public Long getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
import com.labwork1.app.student.model.Session;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
public class SessionDto {
|
||||
private final long id;
|
||||
private Double price;
|
||||
private Timestamp timestamp;
|
||||
private final List<OrderSessionDto> orders;
|
||||
private final CinemaDto cinema;
|
||||
private final int capacity;
|
||||
public SessionDto(Session session) {
|
||||
this.id = session.getId();
|
||||
this.price = session.getPrice();
|
||||
this.timestamp = session.getTimestamp();
|
||||
this.capacity = session.getCapacity();
|
||||
this.cinema = new CinemaDto(session.getCinema());
|
||||
this.orders = session.getOrders()
|
||||
.stream()
|
||||
.map(x -> new OrderSessionDto(x.getId().getSessionId(),
|
||||
x.getId().getOrderId(), x.getCount())).toList();
|
||||
}
|
||||
public int getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
public Timestamp getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
public List<OrderSessionDto> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
public CinemaDto getCinema() {
|
||||
return cinema;
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
import com.labwork1.app.student.service.TicketService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/ticket")
|
||||
public class TicketController {
|
||||
private final TicketService ticketService;
|
||||
|
||||
public TicketController(TicketService ticketService) {
|
||||
this.ticketService = ticketService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public TicketDto getTicket(@PathVariable Long id) {
|
||||
return new TicketDto(ticketService.findTicket(id));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<TicketDto> getTickets() {
|
||||
return ticketService.findAllTickets().stream()
|
||||
.map(TicketDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public TicketDto createTicket(@RequestParam("name") String name,
|
||||
@RequestParam("price") String price,
|
||||
@RequestParam("timestamp") String timestamp) throws ParseException {
|
||||
SimpleDateFormat format = new SimpleDateFormat();
|
||||
format.applyPattern("yyyy-MM-dd-HH:ss");
|
||||
Date docDate = format.parse(timestamp.replace('T', '-'));
|
||||
return new TicketDto(ticketService.addTicket(name, Double.parseDouble(price),
|
||||
new Timestamp(docDate.getTime())));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TicketDto updateTicket(@PathVariable Long id,
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("price") String price) {
|
||||
return new TicketDto(ticketService.updateTicket(id, name, Double.parseDouble(price)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TicketDto deleteTicket(@PathVariable Long id) {
|
||||
return new TicketDto(ticketService.deleteTicket(id));
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
import com.labwork1.app.student.model.Ticket;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class TicketDto {
|
||||
private final long id;
|
||||
private String name;
|
||||
private Double price;
|
||||
private Timestamp timestamp;
|
||||
public TicketDto(Ticket ticket) {
|
||||
this.id = ticket.getId();
|
||||
this.name = ticket.getName();
|
||||
this.price = ticket.getPrice();
|
||||
this.timestamp = ticket.getTimestamp();
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
public Timestamp getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
}
|
52
src/main/java/com/labwork1/app/student/model/Cinema.java
Normal file
52
src/main/java/com/labwork1/app/student/model/Cinema.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.labwork1.app.student.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Cinema {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@NotBlank(message = "name can't be null or empty")
|
||||
@Column
|
||||
private String name;
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "cinema", cascade = CascadeType.REMOVE)
|
||||
private List<Session> sessions;
|
||||
public Cinema() {
|
||||
}
|
||||
|
||||
public Cinema(String name) {
|
||||
this.name = name;
|
||||
this.sessions = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Session> getSessions() {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
public void setSession(Session session) {
|
||||
if (session.getCinema().equals(this)) {
|
||||
this.sessions.add(session);
|
||||
}
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package com.labwork1.app.student.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.sql.Date;
|
||||
@ -18,15 +17,11 @@ public class Order {
|
||||
@NotNull(message = "dateOfPurchase can't be null or empty")
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date dateOfPurchase;
|
||||
@NotNull(message = "customer can't be null or empty")
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "customer_fk")
|
||||
private Customer customer;
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "order_ticket",
|
||||
joinColumns = @JoinColumn(name = "order_fk"),
|
||||
inverseJoinColumns = @JoinColumn(name = "ticket_fk"))
|
||||
private List<Ticket> tickets;
|
||||
@OneToMany(mappedBy = "order", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
private List<OrderSession> sessions;
|
||||
|
||||
public Order() {
|
||||
}
|
||||
@ -35,13 +30,17 @@ public class Order {
|
||||
this.dateOfPurchase = dateOfPurchase;
|
||||
}
|
||||
|
||||
public void addTicket(Ticket ticket) {
|
||||
if (tickets == null)
|
||||
tickets = new ArrayList<>();
|
||||
tickets.add(ticket);
|
||||
if (ticket.getOrders() == null) {
|
||||
ticket.setOrder(this);
|
||||
public void addSession(OrderSession orderSession) {
|
||||
if (sessions == null) {
|
||||
sessions = new ArrayList<>();
|
||||
}
|
||||
if (!sessions.contains(orderSession))
|
||||
this.sessions.add(orderSession);
|
||||
}
|
||||
|
||||
public void removeSession(OrderSession orderSession){
|
||||
if (sessions.contains(orderSession))
|
||||
this.sessions.remove(orderSession);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -88,11 +87,11 @@ public class Order {
|
||||
}
|
||||
}
|
||||
|
||||
public List<Ticket> getTickets() {
|
||||
return tickets;
|
||||
public List<OrderSession> getSessions() {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
public void setTickets(List<Ticket> tickets) {
|
||||
this.tickets = tickets;
|
||||
public void setSessions(List<OrderSession> sessions) {
|
||||
this.sessions = sessions;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,64 @@
|
||||
package com.labwork1.app.student.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "order_session")
|
||||
public class OrderSession {
|
||||
@EmbeddedId
|
||||
private OrderSessionKey id;
|
||||
@ManyToOne
|
||||
@MapsId("sessionId")
|
||||
@JoinColumn(name = "session_id")
|
||||
private Session session;
|
||||
@ManyToOne
|
||||
@MapsId("orderId")
|
||||
@JoinColumn(name = "order_id")
|
||||
private Order order;
|
||||
@Column(name = "count")
|
||||
private Integer count;
|
||||
|
||||
public OrderSession() {
|
||||
}
|
||||
|
||||
public OrderSession(Order order, Session session, Integer count) {
|
||||
this.order = order;
|
||||
this.id = new OrderSessionKey(session.getId(), order.getId());
|
||||
this.id.setOrderId(order.getId());
|
||||
this.id.setSessionId(session.getId());
|
||||
this.session = session;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public OrderSessionKey getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(OrderSessionKey id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Order getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(Order order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public Session getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
public void setSession(Session session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.labwork1.app.student.model;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
@Embeddable
|
||||
public class OrderSessionKey implements Serializable {
|
||||
private Long sessionId;
|
||||
private Long orderId;
|
||||
|
||||
public OrderSessionKey() {
|
||||
}
|
||||
|
||||
public OrderSessionKey(Long sessionId, Long orderId) {
|
||||
this.sessionId = sessionId;
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public Long getSessionId() {
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
public void setSessionId(Long sessionId) {
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
public Long getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(Long orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof OrderSessionKey that)) return false;
|
||||
return Objects.equals(getSessionId(), that.getSessionId()) && Objects.equals(getOrderId(), that.getOrderId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getSessionId(), getOrderId());
|
||||
}
|
||||
}
|
124
src/main/java/com/labwork1/app/student/model/Session.java
Normal file
124
src/main/java/com/labwork1/app/student/model/Session.java
Normal file
@ -0,0 +1,124 @@
|
||||
package com.labwork1.app.student.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Session {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@NotNull(message = "price can't be null or empty")
|
||||
private Double price;
|
||||
@NotNull(message = "timestamp can't be null or empty")
|
||||
@Column
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Timestamp timestamp;
|
||||
@OneToMany(mappedBy = "session", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
private List<OrderSession> orders;
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "cinema_fk")
|
||||
private Cinema cinema;
|
||||
@NotNull(message = "capacity can't be null or empty")
|
||||
@Column
|
||||
private Integer capacity;
|
||||
@NotNull(message = "maxCount can't be null or empty")
|
||||
@Column
|
||||
private Integer maxCount;
|
||||
|
||||
public Session() {
|
||||
}
|
||||
|
||||
public Integer getMaxCount() {
|
||||
return maxCount;
|
||||
}
|
||||
|
||||
public Session(Double price, Timestamp timestamp, Integer maxCount) {
|
||||
this.price = price;
|
||||
this.timestamp = timestamp;
|
||||
this.maxCount = maxCount;
|
||||
this.capacity = maxCount;
|
||||
}
|
||||
|
||||
public Cinema getCinema() {
|
||||
return cinema;
|
||||
}
|
||||
public void setCinema(Cinema cinema) {
|
||||
this.cinema = cinema;
|
||||
if (!cinema.getSessions().contains(this)) {
|
||||
cinema.setSession(this);
|
||||
}
|
||||
}
|
||||
public void addOrder(OrderSession orderSession){
|
||||
if (orders == null){
|
||||
orders = new ArrayList<>();
|
||||
}
|
||||
if (!orders.contains(orderSession)) {
|
||||
this.orders.add(orderSession);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeOrder(OrderSession orderSession){
|
||||
if (orders.contains(orderSession))
|
||||
this.orders.remove(orderSession);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Session session = (Session) o;
|
||||
return Objects.equals(id, session.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Session {" +
|
||||
"id=" + id +
|
||||
", price='" + price + '\'' +
|
||||
", timestamp='" + timestamp.toString() + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Timestamp getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(Timestamp timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public List<OrderSession> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public Integer getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public void setCapacity(Integer capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package com.labwork1.app.student.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.*;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Ticket {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@NotBlank(message = "name can't be null or empty")
|
||||
private String name;
|
||||
@NotNull(message = "price can't be null or empty")
|
||||
private Double price;
|
||||
@NotNull(message = "timestamp can't be null or empty")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Timestamp timestamp;
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "tickets")
|
||||
private List<Order> orders;
|
||||
|
||||
public Ticket() {
|
||||
}
|
||||
|
||||
public Ticket(String name, Double price, Timestamp timestamp) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public void setOrder(Order order) {
|
||||
if (!order.getTickets().contains(this)) {
|
||||
order.getTickets().add(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Ticket ticket = (Ticket) o;
|
||||
return Objects.equals(id, ticket.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Ticket {" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", price='" + price + '\'' +
|
||||
", timestamp='" + timestamp.toString() + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Timestamp getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(Timestamp timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package com.labwork1.app.student.repository;
|
||||
|
||||
import com.labwork1.app.student.model.Ticket;
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface TicketRepository extends JpaRepository<Ticket, Long> {
|
||||
public interface CinemaRepository extends JpaRepository<Cinema, Long> {
|
||||
}
|
@ -3,5 +3,5 @@ package com.labwork1.app.student.repository;
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||
public interface CustomerRepository extends JpaRepository<Customer, Long>, CustomerRepositoryExtension {
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
package com.labwork1.app.student.repository;
|
||||
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CustomerRepositoryExtension {
|
||||
Optional<Customer> deleteCustomerOrders(Long id);
|
||||
void deleteAllCustomers();
|
||||
}
|
@ -3,5 +3,5 @@ package com.labwork1.app.student.repository;
|
||||
import com.labwork1.app.student.model.Order;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||
public interface OrderRepository extends JpaRepository<Order, Long>, OrderRepositoryExtension {
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.labwork1.app.student.repository;
|
||||
|
||||
import com.labwork1.app.student.model.Order;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface OrderRepositoryExtension {
|
||||
Order addOrder(Order order, Long customerId);
|
||||
Optional<Order> addSession(Long id, Long session, Integer count);
|
||||
Optional<Order> deleteSessionInOrder(Long id, Long session, Integer count);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.labwork1.app.student.repository;
|
||||
|
||||
import com.labwork1.app.student.model.Session;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface SessionRepository extends JpaRepository<Session, Long>, SessionRepositoryExtension {
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.labwork1.app.student.repository;
|
||||
|
||||
import com.labwork1.app.student.model.Session;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SessionRepositoryExtension {
|
||||
Session addSession(Session session, Long cinemaId);
|
||||
Session deleteSession(Long id);
|
||||
void deleteAllSessions();
|
||||
List<Session> findAllSessionsWithCapacity();
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.labwork1.app.student.repository.impl;
|
||||
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
import com.labwork1.app.student.model.Order;
|
||||
import com.labwork1.app.student.model.OrderSession;
|
||||
import com.labwork1.app.student.model.Session;
|
||||
import com.labwork1.app.student.repository.CustomerRepository;
|
||||
import com.labwork1.app.student.repository.CustomerRepositoryExtension;
|
||||
import com.labwork1.app.student.repository.OrderRepository;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class CustomerRepositoryImpl implements CustomerRepositoryExtension {
|
||||
@Autowired
|
||||
@Lazy
|
||||
private CustomerRepository customerRepository;
|
||||
@Autowired
|
||||
@Lazy
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public Optional<Customer> deleteCustomerOrders(Long id) {
|
||||
final Optional<Customer> currentCustomer = customerRepository.findById(id);
|
||||
if (currentCustomer.isPresent()) {
|
||||
final List<Order> currentCustomerOrders = currentCustomer.get().getOrders();
|
||||
|
||||
for (int i = 0; i < currentCustomerOrders.size(); i++) {
|
||||
final Optional<Order> tempOrder = orderRepository.findById(currentCustomerOrders.get(i).getId());
|
||||
if (tempOrder.isPresent()) {
|
||||
final Order currentOrder = tempOrder.get();
|
||||
List<OrderSession> orderSessionList = em
|
||||
.createQuery("select p from OrderSession p where p.id.orderId = :id", OrderSession.class)
|
||||
.setParameter("id", id)
|
||||
.getResultList();
|
||||
for (int j = 0; j < orderSessionList.size(); j++) {
|
||||
final Session currentSession = orderSessionList.get(j).getSession();
|
||||
currentOrder.getSessions().remove(orderSessionList.get(j));
|
||||
currentSession.getOrders().remove(orderSessionList.get(j));
|
||||
em.remove(orderSessionList.get(j));
|
||||
em.merge(currentSession);
|
||||
}
|
||||
em.merge(currentOrder);
|
||||
}
|
||||
}
|
||||
em.remove(currentCustomer.get());
|
||||
}
|
||||
return currentCustomer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllCustomers() {
|
||||
List<Customer> customers = em
|
||||
.createQuery("select c from Customer c", Customer.class)
|
||||
.getResultList();
|
||||
for (var customer : customers) {
|
||||
deleteCustomerOrders(customer.getId());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package com.labwork1.app.student.repository.impl;
|
||||
|
||||
import com.labwork1.app.student.model.*;
|
||||
import com.labwork1.app.student.repository.CustomerRepository;
|
||||
import com.labwork1.app.student.repository.OrderRepository;
|
||||
import com.labwork1.app.student.repository.OrderRepositoryExtension;
|
||||
import com.labwork1.app.student.repository.SessionRepository;
|
||||
import com.labwork1.app.student.service.CustomerNotFoundException;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class OrderRepositoryImpl implements OrderRepositoryExtension {
|
||||
@Autowired
|
||||
@Lazy
|
||||
private CustomerRepository customerRepository;
|
||||
@Autowired
|
||||
@Lazy
|
||||
private OrderRepository orderRepository;
|
||||
@Autowired
|
||||
@Lazy
|
||||
private SessionRepository sessionRepository;
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
@Override
|
||||
public Order addOrder(Order order, Long customerId) {
|
||||
final Optional<Customer> customer = customerRepository.findById(customerId);
|
||||
if (customer.isEmpty()) {
|
||||
throw new CustomerNotFoundException(customerId);
|
||||
}
|
||||
order.setCustomer(customer.get());
|
||||
em.persist(order);
|
||||
em.merge(customer.get());
|
||||
return order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Order> addSession(Long id, Long session, Integer count) {
|
||||
if (session <= 0 || count <= 0) {
|
||||
throw new IllegalArgumentException("addOrder empty fields");
|
||||
}
|
||||
Optional<Order> optionalOrder = orderRepository.findById(id);
|
||||
Optional<Session> optionalSession = sessionRepository.findById(session);
|
||||
if (optionalOrder.isEmpty() || optionalSession.isEmpty()) {
|
||||
return optionalOrder;
|
||||
}
|
||||
final Session currentSession = optionalSession.get();
|
||||
final Order currentOrder = optionalOrder.get();
|
||||
OrderSession orderSession = em.find(OrderSession.class,
|
||||
new OrderSessionKey(currentSession.getId(), currentOrder.getId()));
|
||||
|
||||
if (currentSession.getCapacity() < count ||
|
||||
(orderSession != null && orderSession.getCount() + count > currentSession.getCapacity())) {
|
||||
throw new IllegalArgumentException("No more tickets in session. Capacity - "+currentSession.getCapacity()+". Count - "+count);
|
||||
}
|
||||
if (orderSession == null)
|
||||
orderSession = new OrderSession(currentOrder, currentSession, count);
|
||||
else if (orderSession.getCount() + count <= currentSession.getCapacity())
|
||||
orderSession.setCount(orderSession.getCount() + count);
|
||||
|
||||
currentOrder.addSession(orderSession);
|
||||
currentSession.addOrder(orderSession);
|
||||
currentSession.setCapacity(currentSession.getCapacity() - count);
|
||||
em.merge(currentSession);
|
||||
em.merge(currentOrder);
|
||||
return orderRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Order> deleteSessionInOrder(Long id, Long session, Integer count) {
|
||||
final Optional<Order> optionalOrder = orderRepository.findById(id);
|
||||
final Optional<Session> optionalSession = sessionRepository.findById(session);
|
||||
if (optionalOrder.isEmpty() || optionalSession.isEmpty()) {
|
||||
return optionalOrder;
|
||||
}
|
||||
final Order currentOrder = optionalOrder.get();
|
||||
final Session currentSession = optionalSession.get();
|
||||
|
||||
OrderSession orderSession = em.find(OrderSession.class, new OrderSessionKey(session, id));
|
||||
if (orderSession == null)
|
||||
return optionalOrder;
|
||||
|
||||
if (count >= orderSession.getCount()) {
|
||||
currentOrder.getSessions().remove(orderSession);
|
||||
currentSession.getOrders().remove(orderSession);
|
||||
currentSession.setCapacity(currentSession.getCapacity() + orderSession.getCount());
|
||||
em.remove(orderSession);
|
||||
} else {
|
||||
orderSession.setCount(orderSession.getCount() - count);
|
||||
currentSession.setCapacity(currentSession.getMaxCount() - orderSession.getCount());
|
||||
em.merge(orderSession);
|
||||
em.merge(currentSession);
|
||||
}
|
||||
|
||||
return orderRepository.findById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.labwork1.app.student.repository.impl;
|
||||
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
import com.labwork1.app.student.model.OrderSession;
|
||||
import com.labwork1.app.student.model.Session;
|
||||
import com.labwork1.app.student.repository.CinemaRepository;
|
||||
import com.labwork1.app.student.repository.SessionRepository;
|
||||
import com.labwork1.app.student.repository.SessionRepositoryExtension;
|
||||
import com.labwork1.app.student.service.CinemaNotFoundException;
|
||||
import com.labwork1.app.student.service.SessionNotFoundException;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SessionRepositoryImpl implements SessionRepositoryExtension {
|
||||
@Autowired
|
||||
@Lazy
|
||||
private SessionRepository sessionRepository;
|
||||
@Autowired
|
||||
@Lazy
|
||||
private CinemaRepository cinemaRepository;
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
@Override
|
||||
public Session addSession(Session session, Long cinemaId) {
|
||||
if (session == null) return null;
|
||||
final Optional<Cinema> cinema = cinemaRepository.findById(cinemaId);
|
||||
if (cinema.isEmpty()) {
|
||||
throw new CinemaNotFoundException(cinemaId);
|
||||
}
|
||||
session.setCinema(cinema.get());
|
||||
em.persist(session);
|
||||
em.merge(cinema.get());
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Session deleteSession(Long id) {
|
||||
final Optional<Session> optionalSession = sessionRepository.findById(id);
|
||||
if (optionalSession.isEmpty()) {
|
||||
throw new SessionNotFoundException(id);
|
||||
}
|
||||
final Session currentSession = optionalSession.get();
|
||||
int size = currentSession.getOrders().size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
OrderSession temp = currentSession.getOrders().get(0);
|
||||
temp.getSession().removeOrder(temp);
|
||||
temp.getOrder().removeSession(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
em.remove(currentSession);
|
||||
return currentSession;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllSessions() {
|
||||
List<Session> sessions = em
|
||||
.createQuery("select s from Session s", Session.class)
|
||||
.getResultList();
|
||||
for (var session : sessions) {
|
||||
deleteSession(session.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Session> findAllSessionsWithCapacity() {
|
||||
return em.createQuery("select t from Session t where capacity > 0", Session.class)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
public class CinemaNotFoundException extends RuntimeException {
|
||||
public CinemaNotFoundException(Long id) {
|
||||
super(String.format("Cinema with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
import com.labwork1.app.student.repository.CinemaRepository;
|
||||
import com.labwork1.app.util.validation.ValidatorUtil;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CinemaService {
|
||||
private final CinemaRepository cinemaRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public CinemaService(CinemaRepository cinemaRepository, ValidatorUtil validatorUtil) {
|
||||
this.cinemaRepository = cinemaRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Cinema addCinema(String name) {
|
||||
final Cinema cinema = new Cinema(name);
|
||||
validatorUtil.validate(cinema);
|
||||
return cinemaRepository.save(cinema);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Cinema findCinema(Long id) {
|
||||
final Optional<Cinema> cinema = cinemaRepository.findById(id);
|
||||
return cinema.orElseThrow(() -> new CinemaNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Cinema> findAllCinemas() {
|
||||
return cinemaRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Cinema updateCinema(Long id, String name) {
|
||||
final Cinema currentCinema = findCinema(id);
|
||||
currentCinema.setName(name);
|
||||
validatorUtil.validate(currentCinema);
|
||||
return cinemaRepository.save(currentCinema);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Cinema deleteCinema(Long id) {
|
||||
final Cinema currentCinema = findCinema(id);
|
||||
cinemaRepository.delete(currentCinema);
|
||||
return currentCinema;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCinemas() {
|
||||
cinemaRepository.deleteAll();
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ public class CustomerService {
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public CustomerService(CustomerRepository customerRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.customerRepository = customerRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@ -49,13 +49,12 @@ public class CustomerService {
|
||||
|
||||
@Transactional
|
||||
public Customer deleteCustomer(Long id) {
|
||||
final Customer currentCustomer = findCustomer(id);
|
||||
customerRepository.delete(currentCustomer);
|
||||
return currentCustomer;
|
||||
final Optional<Customer> customer = customerRepository.deleteCustomerOrders(id);
|
||||
return customer.orElseThrow(() -> new CustomerNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCustomers() {
|
||||
customerRepository.deleteAll();
|
||||
customerRepository.deleteAllCustomers();
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,9 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
import com.labwork1.app.student.model.Order;
|
||||
import com.labwork1.app.student.model.Ticket;
|
||||
import com.labwork1.app.student.repository.CustomerRepository;
|
||||
import com.labwork1.app.student.repository.OrderRepository;
|
||||
import com.labwork1.app.student.repository.TicketRepository;
|
||||
import com.labwork1.app.util.validation.ValidatorUtil;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -17,59 +14,24 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class OrderService {
|
||||
private final OrderRepository orderRepository;
|
||||
private final TicketRepository ticketRepository;
|
||||
private final CustomerRepository customerRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public OrderService(OrderRepository orderRepository,
|
||||
TicketRepository ticketRepository,
|
||||
CustomerRepository customerRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
public OrderService(OrderRepository orderRepository, ValidatorUtil validatorUtil) {
|
||||
this.orderRepository = orderRepository;
|
||||
this.ticketRepository = ticketRepository;
|
||||
this.customerRepository = customerRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order addOrder(Long customer_id) {
|
||||
final Order order = new Order(new Date(System.currentTimeMillis()));
|
||||
final Customer customer = findCustomer(customer_id);
|
||||
order.setCustomer(customer);
|
||||
validatorUtil.validate(order);
|
||||
return orderRepository.save(order);
|
||||
}
|
||||
|
||||
/*@Transactional
|
||||
public Order addCustomer(Long id, Long customer_id) {
|
||||
final Order order = findOrder(id);
|
||||
final Customer customer = customerRepository.findById(customer_id).get();
|
||||
order.setCustomer(customer);
|
||||
validatorUtil.validate(order);
|
||||
return order;
|
||||
}*/
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
Customer findCustomer(Long customer_id) {
|
||||
final Optional<Customer> customer = customerRepository.findById(customer_id);
|
||||
return customer.orElseThrow(() -> new CustomerNotFoundException(customer_id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
Ticket findTicket(Long ticket_id) {
|
||||
final Optional<Ticket> ticket = ticketRepository.findById(ticket_id);
|
||||
return ticket.orElseThrow(() -> new TicketNotFoundException(ticket_id));
|
||||
return orderRepository.addOrder(order, customer_id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order addTicket(Long id, Long ticket) {
|
||||
final Order currentOrder = findOrder(id);
|
||||
final Ticket currentTicket = findTicket(ticket);
|
||||
currentOrder.addTicket(currentTicket);
|
||||
currentTicket.setOrder(currentOrder);
|
||||
validatorUtil.validate(currentOrder);
|
||||
ticketRepository.save(currentTicket);
|
||||
return orderRepository.save(currentOrder);
|
||||
public Order addSession(Long id, Long session, Integer count) {
|
||||
final Optional<Order> order = orderRepository.addSession(id, session, count);
|
||||
return order.orElseThrow(() -> new EntityNotFoundException("orderId: " + id + ", sessionId: " + session));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@ -91,13 +53,9 @@ public class OrderService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order deleteTicketInOrder(Long id, Ticket ticket) {
|
||||
final Order currentOrder = findOrder(id);
|
||||
final Optional<Ticket> currentTicket = ticketRepository.findById(id);
|
||||
currentTicket.ifPresent(x -> x.getOrders().remove(currentOrder));
|
||||
orderRepository.findById(currentOrder.getId())
|
||||
.ifPresent(x -> x.getTickets().remove(currentTicket));
|
||||
return currentOrder;
|
||||
public Order deleteSessionInOrder(Long id, Long session, Integer count) {
|
||||
final Optional<Order> order = orderRepository.deleteSessionInOrder(id, session, count);
|
||||
return order.orElseThrow(() -> new EntityNotFoundException("orderId: " + id + ", sessionId: " + session));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
public class SessionNotFoundException extends RuntimeException {
|
||||
public SessionNotFoundException(Long id) {
|
||||
super(String.format("Session with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
import com.labwork1.app.student.model.Session;
|
||||
import com.labwork1.app.student.repository.SessionRepository;
|
||||
import com.labwork1.app.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class SessionService {
|
||||
private final SessionRepository sessionRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public SessionService(SessionRepository sessionRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.sessionRepository = sessionRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Session addSession(Double price, Timestamp date, Long cinemaId, Integer capacity) {
|
||||
final Session session = new Session(price, date, capacity);
|
||||
validatorUtil.validate(session);
|
||||
return sessionRepository.addSession(session, cinemaId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Session findSession(Long id) {
|
||||
final Optional<Session> session = sessionRepository.findById(id);
|
||||
return session.orElseThrow(() -> new SessionNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Session> findAllSessions() {
|
||||
return sessionRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Session updateSession(Long id, Double price) {
|
||||
final Session currentSession = findSession(id);
|
||||
currentSession.setPrice(price);
|
||||
validatorUtil.validate(currentSession);
|
||||
return sessionRepository.save(currentSession);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Session deleteSession(Long id) {
|
||||
return sessionRepository.deleteSession(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllSessions() {
|
||||
sessionRepository.deleteAllSessions();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Session> findAllSessionsWithCapacity() {
|
||||
return sessionRepository.findAllSessionsWithCapacity();
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
public class TicketNotFoundException extends RuntimeException {
|
||||
public TicketNotFoundException(Long id) {
|
||||
super(String.format("Ticket with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package com.labwork1.app.student.service;
|
||||
|
||||
import com.labwork1.app.student.model.Ticket;
|
||||
import com.labwork1.app.student.repository.TicketRepository;
|
||||
import com.labwork1.app.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class TicketService {
|
||||
private final TicketRepository ticketRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public TicketService(TicketRepository ticketRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.ticketRepository = ticketRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ticket addTicket(String name, Double price, Timestamp date) {
|
||||
final Ticket ticket = new Ticket(name, price, date);
|
||||
validatorUtil.validate(ticket);
|
||||
return ticketRepository.save(ticket);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Ticket findTicket(Long id) {
|
||||
final Optional<Ticket> ticket = ticketRepository.findById(id);
|
||||
return ticket.orElseThrow(() -> new TicketNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Ticket> findAllTickets() {
|
||||
return ticketRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ticket updateTicket(Long id, String name, Double price) {
|
||||
final Ticket currentTicket = findTicket(id);
|
||||
currentTicket.setName(name);
|
||||
currentTicket.setPrice(price);
|
||||
validatorUtil.validate(currentTicket);
|
||||
return ticketRepository.save(currentTicket);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ticket deleteTicket(Long id) {
|
||||
final Ticket currentTicket = findTicket(id);
|
||||
ticketRepository.delete(currentTicket);
|
||||
return currentTicket;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllTickets() {
|
||||
ticketRepository.deleteAll();
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package com.labwork1.app.util.error;
|
||||
|
||||
import com.labwork1.app.student.service.CustomerNotFoundException;
|
||||
import com.labwork1.app.student.service.OrderNotFoundException;
|
||||
import com.labwork1.app.student.service.TicketNotFoundException;
|
||||
import com.labwork1.app.util.validation.ValidationException;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -18,7 +17,6 @@ public class AdviceController {
|
||||
@ExceptionHandler({
|
||||
CustomerNotFoundException.class,
|
||||
OrderNotFoundException.class,
|
||||
TicketNotFoundException.class,
|
||||
ValidationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleException(Throwable e) {
|
||||
|
@ -2,20 +2,18 @@ package com.labwork1.app;
|
||||
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
import com.labwork1.app.student.model.Order;
|
||||
import com.labwork1.app.student.model.Ticket;
|
||||
import com.labwork1.app.student.service.CustomerService;
|
||||
import com.labwork1.app.student.service.OrderService;
|
||||
import com.labwork1.app.student.service.TicketService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import com.labwork1.app.student.model.Session;
|
||||
import com.labwork1.app.student.service.*;
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaCustomerTests {
|
||||
@ -23,114 +21,99 @@ public class JpaCustomerTests {
|
||||
@Autowired
|
||||
private CustomerService customerService;
|
||||
@Autowired
|
||||
private TicketService ticketService;
|
||||
private SessionService sessionService;
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
@Autowired
|
||||
private CinemaService cinemaService;
|
||||
|
||||
/* @Test
|
||||
@Test
|
||||
void testOrder() {
|
||||
ticketService.deleteAllTickets();
|
||||
sessionService.deleteAllSessions();
|
||||
cinemaService.deleteAllCinemas();
|
||||
orderService.deleteAllOrders();
|
||||
customerService.deleteAllCustomers();
|
||||
// 2 билета
|
||||
final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis()));
|
||||
final Ticket ticket2 = ticketService.addTicket("Аватар", 200.0, new Timestamp(System.currentTimeMillis()));
|
||||
// 2 кино
|
||||
final Cinema cinema1 = cinemaService.addCinema("Меню");
|
||||
final Cinema cinema2 = cinemaService.addCinema("Аватар");
|
||||
|
||||
// 2 сеанса
|
||||
final Session session1 = sessionService.addSession(300.0,
|
||||
new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10);
|
||||
final Session session2 = sessionService.addSession( 200.0,
|
||||
new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10);
|
||||
// проверка 2 сеанса у 1 кино
|
||||
Assertions.assertEquals(cinemaService
|
||||
.findCinema(cinema1.getId()).getSessions().size(), 2);
|
||||
// 1 покупатель
|
||||
final Customer customer1 = customerService.addCustomer("Родион", "Иванов");
|
||||
// 1 заказ, 1 копия заказа
|
||||
final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId()));
|
||||
final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId()).getId());
|
||||
final Order order1 = orderService.findOrder(order0.getId());
|
||||
Assertions.assertEquals(order0, order1);
|
||||
|
||||
// у клиента точно есть заказ?
|
||||
Assertions.assertEquals(customerService
|
||||
.findCustomer(customer1.getId()).getOrders().size(), 1);
|
||||
// 0 заказов
|
||||
orderService.deleteAllOrders();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
|
||||
Assertions.assertThrows(OrderNotFoundException.class, () -> orderService.findOrder(-1L));
|
||||
// 2 покупателя
|
||||
final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
|
||||
// 3 билета
|
||||
final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
// 1 заказ
|
||||
final Order order2 = orderService
|
||||
.addOrder(customerService.findCustomer(customer2.getId()));
|
||||
// у заказа 2 билета
|
||||
orderService.addTicket(order2.getId(), ticket1);
|
||||
orderService.addTicket(order2.getId(), ticket2);
|
||||
// у заказа 1 билет
|
||||
orderService.deleteTicketInOrder(order2.getId(), ticket2);
|
||||
.addOrder(customerService.findCustomer(customer2.getId()).getId());
|
||||
// у заказа 2 сеанса
|
||||
orderService.addSession(order2.getId(), session1.getId(), 2);
|
||||
Assertions.assertEquals(sessionService.findSession(session1.getId()).getCapacity(), 8);
|
||||
|
||||
orderService.addSession(order2.getId(), session2.getId(), 5);
|
||||
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 5);
|
||||
|
||||
Assertions.assertThrows(InvalidDataAccessApiUsageException.class, () ->
|
||||
orderService.addSession(order2.getId(), session2.getId(), 6));
|
||||
|
||||
// у заказа 1 сеанс
|
||||
orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 10);
|
||||
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10);
|
||||
// заполнили всю 2 сессию
|
||||
orderService.addSession(order2.getId(), session2.getId(), 10);
|
||||
|
||||
Assertions.assertEquals(sessionService.findAllSessions().size(), 2);
|
||||
Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 1);
|
||||
Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().get(0), session1);
|
||||
|
||||
orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 4);
|
||||
Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 2);
|
||||
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 4);
|
||||
orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 6);
|
||||
Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 2);
|
||||
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10);
|
||||
|
||||
Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().size(), 1);
|
||||
Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().get(0).getId().getSessionId(), session1.getId());
|
||||
|
||||
// у заказа 1 сеанс
|
||||
// 3 сеанса всего
|
||||
final Session session3 = sessionService.addSession(300.0,
|
||||
new Timestamp(System.currentTimeMillis()), cinema2.getId(), 10);
|
||||
// осталось 2 сеанса, у заказа2 0 сеансов
|
||||
sessionService.deleteSession(session1.getId());
|
||||
Assertions.assertEquals(sessionService.findAllSessions().size(), 2);
|
||||
|
||||
Assertions.assertEquals(orderService
|
||||
.findOrder(order2.getId()).getTickets().size(), 1);
|
||||
.findOrder(order2.getId()).getSessions().size(), 0);
|
||||
// 2-ой покупатель удален
|
||||
// 0 заказов
|
||||
// 0 заказов после его удаления
|
||||
Assertions.assertEquals(orderService.findAllOrders().size(), 1);
|
||||
customerService.deleteCustomer(customer2.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));
|
||||
|
||||
Assertions.assertThrows(CustomerNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
|
||||
Assertions.assertThrows(OrderNotFoundException.class, () -> orderService.findOrder(order2.getId()));
|
||||
Assertions.assertEquals(orderService.findAllOrders().size(), 0);
|
||||
|
||||
Assertions.assertEquals(sessionService.findSession(session3.getId()), session3);
|
||||
cinemaService.deleteCinema(cinema2.getId());
|
||||
}
|
||||
|
||||
@Test()
|
||||
void testCustomer() {
|
||||
customerService.deleteAllCustomers();
|
||||
final Customer customer = customerService.addCustomer("Иван", "Иванов");
|
||||
log.info(customer.toString() + "testCustomerCreate");
|
||||
Assertions.assertNotNull(customer.getId());
|
||||
|
||||
customerService.deleteAllCustomers();
|
||||
final Customer customer1 = customerService.addCustomer("Иван", "Иванов");
|
||||
log.info(customer1.toString() + "testCustomerRead1");
|
||||
final Customer findCustomer = customerService.findCustomer(customer1.getId());
|
||||
log.info(findCustomer.toString() + "testCustomerRead2");
|
||||
Assertions.assertEquals(customer1, findCustomer);
|
||||
|
||||
customerService.deleteAllCustomers();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(-1L));
|
||||
log.info("testCustomerReadNotFound");
|
||||
|
||||
customerService.deleteAllCustomers();
|
||||
customerService.addCustomer("Иван", "Иванов");
|
||||
customerService.addCustomer("Петр", "Петров");
|
||||
final List<Customer> customers1 = customerService.findAllCustomers();
|
||||
log.info(customers1.toString() + "testCustomerReadAll");
|
||||
Assertions.assertEquals(customers1.size(), 2);
|
||||
|
||||
customerService.deleteAllCustomers();
|
||||
final List<Customer> customers2 = customerService.findAllCustomers();
|
||||
log.info(customers2.toString() + "testCustomerReadAllEmpty");
|
||||
Assertions.assertEquals(customers2.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTicket() {
|
||||
ticketService.deleteAllTickets();
|
||||
final Ticket ticket1 = ticketService.addTicket("Ледниковый период", 100.0, new Timestamp(System.currentTimeMillis()));
|
||||
log.info(ticket1.toString() + "testTicketCreate");
|
||||
Assertions.assertNotNull(ticket1.getId());
|
||||
|
||||
ticketService.deleteAllTickets();
|
||||
final Ticket ticket2 = ticketService.addTicket("Выживший", 100.0, new Timestamp(System.currentTimeMillis()));
|
||||
log.info(ticket2.toString() + "testTicketRead1");
|
||||
final Ticket findTicket = ticketService.findTicket(ticket2.getId());
|
||||
log.info(findTicket.toString() + "testTicketRead2");
|
||||
Assertions.assertEquals(ticket2, findTicket);
|
||||
|
||||
ticketService.deleteAllTickets();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> ticketService.findTicket(-1L));
|
||||
log.info("testTicketReadNotFound");
|
||||
|
||||
ticketService.deleteAllTickets();
|
||||
ticketService.addTicket("1+1", 100.0, new Timestamp(System.currentTimeMillis()));
|
||||
ticketService.addTicket("Титаник", 100.0, new Timestamp(System.currentTimeMillis()));
|
||||
final List<Ticket> tickets1 = ticketService.findAllTickets();
|
||||
log.info(tickets1.toString() + "testTicketReadAll");
|
||||
Assertions.assertEquals(tickets1.size(), 2);
|
||||
|
||||
ticketService.deleteAllTickets();
|
||||
final List<Ticket> tickets2 = ticketService.findAllTickets();
|
||||
log.info(tickets2.toString() + "testTicketReadAllEmpty");
|
||||
Assertions.assertEquals(tickets2.size(), 0);
|
||||
|
||||
|
||||
}*/
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user