diff --git a/src/main/java/com/labwork1/app/student/controller/OrderMvcController.java b/src/main/java/com/labwork1/app/student/controller/OrderMvcController.java index e760793..c003182 100644 --- a/src/main/java/com/labwork1/app/student/controller/OrderMvcController.java +++ b/src/main/java/com/labwork1/app/student/controller/OrderMvcController.java @@ -1,5 +1,6 @@ package com.labwork1.app.student.controller; +import com.labwork1.app.student.model.SessionExtension; import com.labwork1.app.student.service.CustomerService; import com.labwork1.app.student.service.OrderService; import com.labwork1.app.student.service.SessionService; diff --git a/src/main/java/com/labwork1/app/student/controller/SessionDto.java b/src/main/java/com/labwork1/app/student/controller/SessionDto.java index f994538..10ba106 100644 --- a/src/main/java/com/labwork1/app/student/controller/SessionDto.java +++ b/src/main/java/com/labwork1/app/student/controller/SessionDto.java @@ -1,6 +1,7 @@ package com.labwork1.app.student.controller; import com.labwork1.app.student.model.Session; +import com.labwork1.app.student.model.SessionExtension; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; @@ -17,7 +18,7 @@ public class SessionDto { public SessionDto() { } - public SessionDto(Session session) { + public SessionDto(SessionExtension session) { this.id = session.getId(); this.price = session.getPrice(); this.timestamp = session.getTimestamp(); @@ -28,6 +29,16 @@ public class SessionDto { } else this.cinema = null; } + public SessionDto(Session session) { + this.id = session.getId(); + this.price = session.getPrice(); + this.timestamp = session.getTimestamp(); + this.maxCount = session.getMaxCount(); + if (session.getCinema() != null) { + this.cinema = new CinemaDto(session.getCinema()); + } else this.cinema = null; + } + public CinemaDto getCinema() { return cinema; } diff --git a/src/main/java/com/labwork1/app/student/model/Session.java b/src/main/java/com/labwork1/app/student/model/Session.java index 1ff14c0..9573690 100644 --- a/src/main/java/com/labwork1/app/student/model/Session.java +++ b/src/main/java/com/labwork1/app/student/model/Session.java @@ -14,24 +14,22 @@ import java.util.Objects; public class Session { @Id @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; + protected Long id; @NotNull(message = "price can't be null or empty") - private Double price; + protected Double price; @NotNull(message = "timestamp can't be null or empty") @Column @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") - private LocalDateTime timestamp; + protected LocalDateTime timestamp; @OneToMany(mappedBy = "session", fetch = FetchType.EAGER) private List orders; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "cinema_fk") - private Cinema cinema; + protected Cinema cinema; @NotNull(message = "maxCount can't be null or empty") @Column - private Integer maxCount; - @Transient - private Long capacity; + protected Integer maxCount; public Session() { } @@ -46,14 +44,13 @@ public class Session { this.maxCount = maxCount; } - public Session(Session session, Long capacity) { + public Session(Session session) { this.id = session.getId(); this.price = session.getPrice(); this.timestamp = session.getTimestamp(); this.orders = session.getOrders(); this.cinema = session.getCinema(); this.maxCount = session.getMaxCount(); - this.capacity = capacity; } public Cinema getCinema() { @@ -98,7 +95,6 @@ public class Session { "id=" + id + ", price='" + price + '\'' + ", timestamp='" + timestamp.toString() + '\'' + - ", capacity='" + capacity + '\'' + ", maxCount='" + maxCount.toString() + '\'' + ", cinema='" + cinema.toString() + '\'' + '}'; @@ -139,12 +135,4 @@ public class Session { public List getOrders() { return orders; } - - public Long getCapacity() { - return capacity; - } - - public void setCapacity(Long capacity) { - this.capacity = capacity; - } } diff --git a/src/main/java/com/labwork1/app/student/model/SessionExtension.java b/src/main/java/com/labwork1/app/student/model/SessionExtension.java new file mode 100644 index 0000000..eaccc57 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/SessionExtension.java @@ -0,0 +1,17 @@ +package com.labwork1.app.student.model; + +public class SessionExtension extends Session { + private Long capacity; + public SessionExtension(Session session, Long capacity) { + super(session); + this.capacity = capacity; + } + + public Long getCapacity() { + return capacity; + } + + public void setCapacity(Long capacity) { + this.capacity = capacity; + } +} diff --git a/src/main/java/com/labwork1/app/student/repository/SessionRepository.java b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java index ba55e8f..7870db4 100644 --- a/src/main/java/com/labwork1/app/student/repository/SessionRepository.java +++ b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java @@ -1,6 +1,7 @@ package com.labwork1.app.student.repository; import com.labwork1.app.student.model.Session; +import com.labwork1.app.student.model.SessionExtension; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -11,13 +12,13 @@ import java.util.Optional; public interface SessionRepository extends JpaRepository { @Query("Select sum(os.count) from OrderSession os where os.session.id = :sessionId") Optional getCapacity(@Param("sessionId") Long sessionId); - @Query("Select new com.labwork1.app.student.model.Session(s, sum(os.count)) from Session s " + + @Query("Select new com.labwork1.app.student.model.SessionExtension(s, sum(os.count)) from Session s " + "left join OrderSession os on s.id = os.session.id " + "group by s") - List getSessionsWithCapacity(); - @Query("Select new com.labwork1.app.student.model.Session(s, sum(os.count)) from Session s " + + List getSessionsWithCapacity(); + @Query("Select new com.labwork1.app.student.model.SessionExtension(s, sum(os.count)) from Session s " + "left join OrderSession os on s.id = os.session.id " + "where s.id = :sessionId " + "group by s") - Optional getSessionWithCapacity(@Param("sessionId") Long sessionId); + Optional getSessionWithCapacity(@Param("sessionId") Long sessionId); } diff --git a/src/main/java/com/labwork1/app/student/service/SessionService.java b/src/main/java/com/labwork1/app/student/service/SessionService.java index 43967b0..3597b7e 100644 --- a/src/main/java/com/labwork1/app/student/service/SessionService.java +++ b/src/main/java/com/labwork1/app/student/service/SessionService.java @@ -2,6 +2,7 @@ package com.labwork1.app.student.service; import com.labwork1.app.student.model.Cinema; import com.labwork1.app.student.model.Session; +import com.labwork1.app.student.model.SessionExtension; import com.labwork1.app.student.repository.SessionRepository; import com.labwork1.app.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; @@ -33,13 +34,13 @@ public class SessionService { } @Transactional(readOnly = true) - public Session findSession(Long id) { + public SessionExtension findSession(Long id) { return sessionRepository.getSessionWithCapacity(id) .orElseThrow(() -> new SessionNotFoundException(id)); } @Transactional(readOnly = true) - public List findAllSessions() { + public List findAllSessions() { return sessionRepository.getSessionsWithCapacity(); } diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index 15a127a..26262a1 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import java.sql.Timestamp; +import java.time.LocalDateTime; import java.util.List; @SpringBootTest @@ -36,9 +36,9 @@ public class JpaCustomerTests { // 2 сеанса final Session session1 = sessionService.addSession(300.0, - new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10); + LocalDateTime.now(), cinema1.getId(), 10); final Session session2 = sessionService.addSession( 200.0, - new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10); + LocalDateTime.now(), cinema1.getId(), 10); // проверка 2 сеанса у 1 кино Assertions.assertEquals(cinemaService @@ -67,7 +67,7 @@ public class JpaCustomerTests { // у заказа 2 сеанса orderService.addSession(order2.getId(), session1.getId(), 2); - List result = sessionService.findAllSessions(); + List result = sessionService.findAllSessions(); Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2); @@ -96,7 +96,7 @@ public class JpaCustomerTests { // у заказа 1 сеанс // 3 сеанса всего final Session session3 = sessionService.addSession(300.0, - new Timestamp(System.currentTimeMillis()), cinema2.getId(), 10); + LocalDateTime.now(), cinema2.getId(), 10); // удалили заказ2, у которого был сеанс1 orderService.deleteOrder(order2.getId()); Assertions.assertEquals(orderService.findAllOrders().size(), 0);