From 465dcc3fae4859045bf6941c3cf89c1c2ea22981 Mon Sep 17 00:00:00 2001 From: dasha Date: Fri, 7 Apr 2023 14:55:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BA=D0=BE=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.mv.db | Bin 24576 -> 49152 bytes .../app/student/controller/CinemaDto.java | 26 +++ .../controller/CustomerController.java | 2 +- .../app/student/controller/CustomerDto.java | 1 - .../student/controller/OrderController.java | 15 +- .../app/student/controller/OrderDto.java | 11 +- .../student/controller/OrderSessionDto.java | 24 +++ .../app/student/controller/SessionDto.java | 44 +++++ .../student/controller/TicketController.java | 55 ------ .../app/student/controller/TicketDto.java | 30 ---- .../labwork1/app/student/model/Cinema.java | 52 ++++++ .../com/labwork1/app/student/model/Order.java | 33 ++-- .../app/student/model/OrderSession.java | 64 +++++++ .../app/student/model/OrderSessionKey.java | 48 ++++++ .../labwork1/app/student/model/Session.java | 124 +++++++++++++ .../labwork1/app/student/model/Ticket.java | 95 ---------- ...tRepository.java => CinemaRepository.java} | 4 +- .../repository/CustomerRepository.java | 2 +- .../CustomerRepositoryExtension.java | 10 ++ .../student/repository/OrderRepository.java | 2 +- .../repository/OrderRepositoryExtension.java | 11 ++ .../student/repository/SessionRepository.java | 7 + .../SessionRepositoryExtension.java | 12 ++ .../impl/CustomerRepositoryImpl.java | 67 +++++++ .../repository/impl/OrderRepositoryImpl.java | 100 +++++++++++ .../impl/SessionRepositoryImpl.java | 74 ++++++++ .../service/CinemaNotFoundException.java | 7 + .../app/student/service/CinemaService.java | 58 +++++++ .../app/student/service/CustomerService.java | 9 +- .../app/student/service/OrderService.java | 60 +------ .../service/SessionNotFoundException.java | 7 + .../app/student/service/SessionService.java | 63 +++++++ .../service/TicketNotFoundException.java | 7 - .../app/student/service/TicketService.java | 62 ------- .../app/util/error/AdviceController.java | 2 - .../com/labwork1/app/JpaCustomerTests.java | 163 ++++++++---------- 36 files changed, 914 insertions(+), 437 deletions(-) create mode 100644 src/main/java/com/labwork1/app/student/controller/CinemaDto.java create mode 100644 src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java create mode 100644 src/main/java/com/labwork1/app/student/controller/SessionDto.java delete mode 100644 src/main/java/com/labwork1/app/student/controller/TicketController.java delete mode 100644 src/main/java/com/labwork1/app/student/controller/TicketDto.java create mode 100644 src/main/java/com/labwork1/app/student/model/Cinema.java create mode 100644 src/main/java/com/labwork1/app/student/model/OrderSession.java create mode 100644 src/main/java/com/labwork1/app/student/model/OrderSessionKey.java create mode 100644 src/main/java/com/labwork1/app/student/model/Session.java delete mode 100644 src/main/java/com/labwork1/app/student/model/Ticket.java rename src/main/java/com/labwork1/app/student/repository/{TicketRepository.java => CinemaRepository.java} (51%) create mode 100644 src/main/java/com/labwork1/app/student/repository/CustomerRepositoryExtension.java create mode 100644 src/main/java/com/labwork1/app/student/repository/OrderRepositoryExtension.java create mode 100644 src/main/java/com/labwork1/app/student/repository/SessionRepository.java create mode 100644 src/main/java/com/labwork1/app/student/repository/SessionRepositoryExtension.java create mode 100644 src/main/java/com/labwork1/app/student/repository/impl/CustomerRepositoryImpl.java create mode 100644 src/main/java/com/labwork1/app/student/repository/impl/OrderRepositoryImpl.java create mode 100644 src/main/java/com/labwork1/app/student/repository/impl/SessionRepositoryImpl.java create mode 100644 src/main/java/com/labwork1/app/student/service/CinemaNotFoundException.java create mode 100644 src/main/java/com/labwork1/app/student/service/CinemaService.java create mode 100644 src/main/java/com/labwork1/app/student/service/SessionNotFoundException.java create mode 100644 src/main/java/com/labwork1/app/student/service/SessionService.java delete mode 100644 src/main/java/com/labwork1/app/student/service/TicketNotFoundException.java delete mode 100644 src/main/java/com/labwork1/app/student/service/TicketService.java diff --git a/data.mv.db b/data.mv.db index 8a96586aa6b8073107af613b747a1158f8f54952..e75cdd1b28a64a4ba2429322b4156de5fedbec39 100644 GIT binary patch literal 49152 zcmeHQO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F!XV2fgSes}L0IhEXV~>y6^hct0Wf&wH2q`LXI$zn@xR^Un$2s8(A@0xksQ z)}$s}Ony0|Tslk<#Jd$1=@uju;@y-*x&;Y|z}L|T8NJCAPPy&>|yGI~+ Y|37*E9tAgI>_6=n;QhbzmfrvW4|h#v 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 getSessions() { + return sessions; + } +} diff --git a/src/main/java/com/labwork1/app/student/controller/CustomerController.java b/src/main/java/com/labwork1/app/student/controller/CustomerController.java index 94db1e4..ead463d 100644 --- a/src/main/java/com/labwork1/app/student/controller/CustomerController.java +++ b/src/main/java/com/labwork1/app/student/controller/CustomerController.java @@ -43,4 +43,4 @@ public class CustomerController { public CustomerDto deleteCustomer(@PathVariable Long id) { return new CustomerDto(customerService.deleteCustomer(id)); } -} \ No newline at end of file +} diff --git a/src/main/java/com/labwork1/app/student/controller/CustomerDto.java b/src/main/java/com/labwork1/app/student/controller/CustomerDto.java index 49cc1a9..f74af67 100644 --- a/src/main/java/com/labwork1/app/student/controller/CustomerDto.java +++ b/src/main/java/com/labwork1/app/student/controller/CustomerDto.java @@ -17,7 +17,6 @@ public class CustomerDto { this.password = customer.getPassword(); this.orders = customer.getOrders(); } - public long getId() { return id; } diff --git a/src/main/java/com/labwork1/app/student/controller/OrderController.java b/src/main/java/com/labwork1/app/student/controller/OrderController.java index 700de29..404ad64 100644 --- a/src/main/java/com/labwork1/app/student/controller/OrderController.java +++ b/src/main/java/com/labwork1/app/student/controller/OrderController.java @@ -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}") diff --git a/src/main/java/com/labwork1/app/student/controller/OrderDto.java b/src/main/java/com/labwork1/app/student/controller/OrderDto.java index f572f82..3fbc651 100644 --- a/src/main/java/com/labwork1/app/student/controller/OrderDto.java +++ b/src/main/java/com/labwork1/app/student/controller/OrderDto.java @@ -10,14 +10,15 @@ public class OrderDto { private final long id; private final Date dateOfPurchase; private final Customer customer; - private final List tickets; + private final List 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 getTickets() { - return tickets; + public List getSessions() { + return sessions; } } diff --git a/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java b/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java new file mode 100644 index 0000000..51fb7ac --- /dev/null +++ b/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java @@ -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; + } +} diff --git a/src/main/java/com/labwork1/app/student/controller/SessionDto.java b/src/main/java/com/labwork1/app/student/controller/SessionDto.java new file mode 100644 index 0000000..5fb9671 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/controller/SessionDto.java @@ -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 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 getOrders() { + return orders; + } + public CinemaDto getCinema() { + return cinema; + } +} diff --git a/src/main/java/com/labwork1/app/student/controller/TicketController.java b/src/main/java/com/labwork1/app/student/controller/TicketController.java deleted file mode 100644 index fa255a7..0000000 --- a/src/main/java/com/labwork1/app/student/controller/TicketController.java +++ /dev/null @@ -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 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)); - } -} diff --git a/src/main/java/com/labwork1/app/student/controller/TicketDto.java b/src/main/java/com/labwork1/app/student/controller/TicketDto.java deleted file mode 100644 index 43f911a..0000000 --- a/src/main/java/com/labwork1/app/student/controller/TicketDto.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/com/labwork1/app/student/model/Cinema.java b/src/main/java/com/labwork1/app/student/model/Cinema.java new file mode 100644 index 0000000..22e5f50 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/Cinema.java @@ -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 sessions; + public Cinema() { + } + + public Cinema(String name) { + this.name = name; + this.sessions = new ArrayList<>(); + } + + public List 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; + } +} diff --git a/src/main/java/com/labwork1/app/student/model/Order.java b/src/main/java/com/labwork1/app/student/model/Order.java index 7417612..2304682 100644 --- a/src/main/java/com/labwork1/app/student/model/Order.java +++ b/src/main/java/com/labwork1/app/student/model/Order.java @@ -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 tickets; + @OneToMany(mappedBy = "order", fetch = FetchType.EAGER, cascade = CascadeType.ALL) + private List 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 getTickets() { - return tickets; + public List getSessions() { + return sessions; } - public void setTickets(List tickets) { - this.tickets = tickets; + public void setSessions(List sessions) { + this.sessions = sessions; } } diff --git a/src/main/java/com/labwork1/app/student/model/OrderSession.java b/src/main/java/com/labwork1/app/student/model/OrderSession.java new file mode 100644 index 0000000..70653cc --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/OrderSession.java @@ -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; + } +} diff --git a/src/main/java/com/labwork1/app/student/model/OrderSessionKey.java b/src/main/java/com/labwork1/app/student/model/OrderSessionKey.java new file mode 100644 index 0000000..05dd8fc --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/OrderSessionKey.java @@ -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()); + } +} diff --git a/src/main/java/com/labwork1/app/student/model/Session.java b/src/main/java/com/labwork1/app/student/model/Session.java new file mode 100644 index 0000000..e073d1d --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/Session.java @@ -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 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 getOrders() { + return orders; + } + + public Integer getCapacity() { + return capacity; + } + + public void setCapacity(Integer capacity) { + this.capacity = capacity; + } +} diff --git a/src/main/java/com/labwork1/app/student/model/Ticket.java b/src/main/java/com/labwork1/app/student/model/Ticket.java deleted file mode 100644 index 1f7eb3b..0000000 --- a/src/main/java/com/labwork1/app/student/model/Ticket.java +++ /dev/null @@ -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 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 getOrders() { - return orders; - } - -} diff --git a/src/main/java/com/labwork1/app/student/repository/TicketRepository.java b/src/main/java/com/labwork1/app/student/repository/CinemaRepository.java similarity index 51% rename from src/main/java/com/labwork1/app/student/repository/TicketRepository.java rename to src/main/java/com/labwork1/app/student/repository/CinemaRepository.java index b447ba2..53200d0 100644 --- a/src/main/java/com/labwork1/app/student/repository/TicketRepository.java +++ b/src/main/java/com/labwork1/app/student/repository/CinemaRepository.java @@ -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 { +public interface CinemaRepository extends JpaRepository { } diff --git a/src/main/java/com/labwork1/app/student/repository/CustomerRepository.java b/src/main/java/com/labwork1/app/student/repository/CustomerRepository.java index 5e8c86f..6266b77 100644 --- a/src/main/java/com/labwork1/app/student/repository/CustomerRepository.java +++ b/src/main/java/com/labwork1/app/student/repository/CustomerRepository.java @@ -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 { +public interface CustomerRepository extends JpaRepository, CustomerRepositoryExtension { } diff --git a/src/main/java/com/labwork1/app/student/repository/CustomerRepositoryExtension.java b/src/main/java/com/labwork1/app/student/repository/CustomerRepositoryExtension.java new file mode 100644 index 0000000..d4dfdeb --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/CustomerRepositoryExtension.java @@ -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 deleteCustomerOrders(Long id); + void deleteAllCustomers(); +} diff --git a/src/main/java/com/labwork1/app/student/repository/OrderRepository.java b/src/main/java/com/labwork1/app/student/repository/OrderRepository.java index 314a285..689766b 100644 --- a/src/main/java/com/labwork1/app/student/repository/OrderRepository.java +++ b/src/main/java/com/labwork1/app/student/repository/OrderRepository.java @@ -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 { +public interface OrderRepository extends JpaRepository, OrderRepositoryExtension { } diff --git a/src/main/java/com/labwork1/app/student/repository/OrderRepositoryExtension.java b/src/main/java/com/labwork1/app/student/repository/OrderRepositoryExtension.java new file mode 100644 index 0000000..180b173 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/OrderRepositoryExtension.java @@ -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 addSession(Long id, Long session, Integer count); + Optional deleteSessionInOrder(Long id, Long session, Integer count); +} diff --git a/src/main/java/com/labwork1/app/student/repository/SessionRepository.java b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java new file mode 100644 index 0000000..6178209 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java @@ -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, SessionRepositoryExtension { +} diff --git a/src/main/java/com/labwork1/app/student/repository/SessionRepositoryExtension.java b/src/main/java/com/labwork1/app/student/repository/SessionRepositoryExtension.java new file mode 100644 index 0000000..8dcf3ec --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/SessionRepositoryExtension.java @@ -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 findAllSessionsWithCapacity(); +} diff --git a/src/main/java/com/labwork1/app/student/repository/impl/CustomerRepositoryImpl.java b/src/main/java/com/labwork1/app/student/repository/impl/CustomerRepositoryImpl.java new file mode 100644 index 0000000..1224e48 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/impl/CustomerRepositoryImpl.java @@ -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 deleteCustomerOrders(Long id) { + final Optional currentCustomer = customerRepository.findById(id); + if (currentCustomer.isPresent()) { + final List currentCustomerOrders = currentCustomer.get().getOrders(); + + for (int i = 0; i < currentCustomerOrders.size(); i++) { + final Optional tempOrder = orderRepository.findById(currentCustomerOrders.get(i).getId()); + if (tempOrder.isPresent()) { + final Order currentOrder = tempOrder.get(); + List 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 customers = em + .createQuery("select c from Customer c", Customer.class) + .getResultList(); + for (var customer : customers) { + deleteCustomerOrders(customer.getId()); + } + } +} diff --git a/src/main/java/com/labwork1/app/student/repository/impl/OrderRepositoryImpl.java b/src/main/java/com/labwork1/app/student/repository/impl/OrderRepositoryImpl.java new file mode 100644 index 0000000..402110d --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/impl/OrderRepositoryImpl.java @@ -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 = 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 addSession(Long id, Long session, Integer count) { + if (session <= 0 || count <= 0) { + throw new IllegalArgumentException("addOrder empty fields"); + } + Optional optionalOrder = orderRepository.findById(id); + Optional 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 deleteSessionInOrder(Long id, Long session, Integer count) { + final Optional optionalOrder = orderRepository.findById(id); + final Optional 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); + } +} diff --git a/src/main/java/com/labwork1/app/student/repository/impl/SessionRepositoryImpl.java b/src/main/java/com/labwork1/app/student/repository/impl/SessionRepositoryImpl.java new file mode 100644 index 0000000..5b67c1d --- /dev/null +++ b/src/main/java/com/labwork1/app/student/repository/impl/SessionRepositoryImpl.java @@ -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 = 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 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 sessions = em + .createQuery("select s from Session s", Session.class) + .getResultList(); + for (var session : sessions) { + deleteSession(session.getId()); + } + } + + @Override + public List findAllSessionsWithCapacity() { + return em.createQuery("select t from Session t where capacity > 0", Session.class) + .getResultList(); + } +} diff --git a/src/main/java/com/labwork1/app/student/service/CinemaNotFoundException.java b/src/main/java/com/labwork1/app/student/service/CinemaNotFoundException.java new file mode 100644 index 0000000..d8f7c68 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/CinemaNotFoundException.java @@ -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)); + } +} diff --git a/src/main/java/com/labwork1/app/student/service/CinemaService.java b/src/main/java/com/labwork1/app/student/service/CinemaService.java new file mode 100644 index 0000000..82559b2 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/CinemaService.java @@ -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 = cinemaRepository.findById(id); + return cinema.orElseThrow(() -> new CinemaNotFoundException(id)); + } + + @Transactional + public List 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(); + } +} diff --git a/src/main/java/com/labwork1/app/student/service/CustomerService.java b/src/main/java/com/labwork1/app/student/service/CustomerService.java index 8578929..9c7cb9e 100644 --- a/src/main/java/com/labwork1/app/student/service/CustomerService.java +++ b/src/main/java/com/labwork1/app/student/service/CustomerService.java @@ -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 = customerRepository.deleteCustomerOrders(id); + return customer.orElseThrow(() -> new CustomerNotFoundException(id)); } @Transactional public void deleteAllCustomers() { - customerRepository.deleteAll(); + customerRepository.deleteAllCustomers(); } } diff --git a/src/main/java/com/labwork1/app/student/service/OrderService.java b/src/main/java/com/labwork1/app/student/service/OrderService.java index 8bde4e8..59eb46e 100644 --- a/src/main/java/com/labwork1/app/student/service/OrderService.java +++ b/src/main/java/com/labwork1/app/student/service/OrderService.java @@ -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 = customerRepository.findById(customer_id); - return customer.orElseThrow(() -> new CustomerNotFoundException(customer_id)); - } - - @Transactional(readOnly = true) - Ticket findTicket(Long ticket_id) { - final Optional 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 = 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 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 = orderRepository.deleteSessionInOrder(id, session, count); + return order.orElseThrow(() -> new EntityNotFoundException("orderId: " + id + ", sessionId: " + session)); } @Transactional diff --git a/src/main/java/com/labwork1/app/student/service/SessionNotFoundException.java b/src/main/java/com/labwork1/app/student/service/SessionNotFoundException.java new file mode 100644 index 0000000..44de8be --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/SessionNotFoundException.java @@ -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)); + } +} diff --git a/src/main/java/com/labwork1/app/student/service/SessionService.java b/src/main/java/com/labwork1/app/student/service/SessionService.java new file mode 100644 index 0000000..bcee6ec --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/SessionService.java @@ -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 = sessionRepository.findById(id); + return session.orElseThrow(() -> new SessionNotFoundException(id)); + } + + @Transactional(readOnly = true) + public List 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 findAllSessionsWithCapacity() { + return sessionRepository.findAllSessionsWithCapacity(); + } +} diff --git a/src/main/java/com/labwork1/app/student/service/TicketNotFoundException.java b/src/main/java/com/labwork1/app/student/service/TicketNotFoundException.java deleted file mode 100644 index 1e41aed..0000000 --- a/src/main/java/com/labwork1/app/student/service/TicketNotFoundException.java +++ /dev/null @@ -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)); - } -} diff --git a/src/main/java/com/labwork1/app/student/service/TicketService.java b/src/main/java/com/labwork1/app/student/service/TicketService.java deleted file mode 100644 index f99a225..0000000 --- a/src/main/java/com/labwork1/app/student/service/TicketService.java +++ /dev/null @@ -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 = ticketRepository.findById(id); - return ticket.orElseThrow(() -> new TicketNotFoundException(id)); - } - - @Transactional(readOnly = true) - public List 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(); - } -} diff --git a/src/main/java/com/labwork1/app/util/error/AdviceController.java b/src/main/java/com/labwork1/app/util/error/AdviceController.java index 28acbc2..db82852 100644 --- a/src/main/java/com/labwork1/app/util/error/AdviceController.java +++ b/src/main/java/com/labwork1/app/util/error/AdviceController.java @@ -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 handleException(Throwable e) { diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index 15e69b1..ea65857 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -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 customers1 = customerService.findAllCustomers(); - log.info(customers1.toString() + "testCustomerReadAll"); - Assertions.assertEquals(customers1.size(), 2); - - customerService.deleteAllCustomers(); - final List 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 tickets1 = ticketService.findAllTickets(); - log.info(tickets1.toString() + "testTicketReadAll"); - Assertions.assertEquals(tickets1.size(), 2); - - ticketService.deleteAllTickets(); - final List tickets2 = ticketService.findAllTickets(); - log.info(tickets2.toString() + "testTicketReadAllEmpty"); - Assertions.assertEquals(tickets2.size(), 0); - - - }*/ }