так правильнее
This commit is contained in:
parent
63dcaa4d7a
commit
d1011d0537
@ -1,6 +1,7 @@
|
|||||||
package com.labwork1.app.student.controller;
|
package com.labwork1.app.student.controller;
|
||||||
|
|
||||||
import com.labwork1.app.student.model.Session;
|
import com.labwork1.app.student.model.Session;
|
||||||
|
import com.labwork1.app.student.model.SessionExtension;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
@ -16,14 +17,25 @@ public class SessionDto {
|
|||||||
public SessionDto() {
|
public SessionDto() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SessionDto(Session session) {
|
public SessionDto(SessionExtension session) {
|
||||||
this.id = session.getId();
|
this.id = session.getId();
|
||||||
this.price = session.getPrice();
|
this.price = session.getPrice();
|
||||||
this.timestamp = session.getTimestamp();
|
this.timestamp = session.getTimestamp();
|
||||||
this.capacity = session.getCapacity();
|
this.capacity = session.getCapacity();
|
||||||
this.maxCount = session.getMaxCount();
|
this.maxCount = session.getMaxCount();
|
||||||
if (session.getCinema() != null) this.cinema = new CinemaDto(session.getCinema());
|
if (session.getCinema() != null) {
|
||||||
else this.cinema = null;
|
this.cinema = new CinemaDto(session.getCinema());
|
||||||
|
} 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 int getMaxCount() {
|
public int getMaxCount() {
|
||||||
|
@ -12,23 +12,21 @@ import java.util.Objects;
|
|||||||
public class Session {
|
public class Session {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private Long id;
|
protected Long id;
|
||||||
@NotNull(message = "price can't be null or empty")
|
@NotNull(message = "price can't be null or empty")
|
||||||
private Double price;
|
protected Double price;
|
||||||
@NotNull(message = "timestamp can't be null or empty")
|
@NotNull(message = "timestamp can't be null or empty")
|
||||||
@Column
|
@Column
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Timestamp timestamp;
|
protected Timestamp timestamp;
|
||||||
@OneToMany(mappedBy = "session", fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "session", fetch = FetchType.EAGER)
|
||||||
private List<OrderSession> orders;
|
private List<OrderSession> orders;
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
@JoinColumn(name = "cinema_fk")
|
@JoinColumn(name = "cinema_fk")
|
||||||
private Cinema cinema;
|
protected Cinema cinema;
|
||||||
@NotNull(message = "maxCount can't be null or empty")
|
@NotNull(message = "maxCount can't be null or empty")
|
||||||
@Column
|
@Column
|
||||||
private Integer maxCount;
|
protected Integer maxCount;
|
||||||
@Transient
|
|
||||||
private Long capacity;
|
|
||||||
|
|
||||||
public Session() {
|
public Session() {
|
||||||
}
|
}
|
||||||
@ -43,14 +41,13 @@ public class Session {
|
|||||||
this.maxCount = maxCount;
|
this.maxCount = maxCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Session(Session session, Long capacity) {
|
public Session(Session session) {
|
||||||
this.id = session.getId();
|
this.id = session.getId();
|
||||||
this.price = session.getPrice();
|
this.price = session.getPrice();
|
||||||
this.timestamp = session.getTimestamp();
|
this.timestamp = session.getTimestamp();
|
||||||
this.orders = session.getOrders();
|
this.orders = session.getOrders();
|
||||||
this.cinema = session.getCinema();
|
this.cinema = session.getCinema();
|
||||||
this.maxCount = session.getMaxCount();
|
this.maxCount = session.getMaxCount();
|
||||||
this.capacity = capacity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cinema getCinema() {
|
public Cinema getCinema() {
|
||||||
@ -95,7 +92,6 @@ public class Session {
|
|||||||
"id=" + id +
|
"id=" + id +
|
||||||
", price='" + price + '\'' +
|
", price='" + price + '\'' +
|
||||||
", timestamp='" + timestamp.toString() + '\'' +
|
", timestamp='" + timestamp.toString() + '\'' +
|
||||||
", capacity='" + capacity + '\'' +
|
|
||||||
", maxCount='" + maxCount.toString() + '\'' +
|
", maxCount='" + maxCount.toString() + '\'' +
|
||||||
", cinema='" + cinema.toString() + '\'' +
|
", cinema='" + cinema.toString() + '\'' +
|
||||||
'}';
|
'}';
|
||||||
@ -136,12 +132,4 @@ public class Session {
|
|||||||
public List<OrderSession> getOrders() {
|
public List<OrderSession> getOrders() {
|
||||||
return orders;
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getCapacity() {
|
|
||||||
return capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCapacity(Long capacity) {
|
|
||||||
this.capacity = capacity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package com.labwork1.app.student.repository;
|
package com.labwork1.app.student.repository;
|
||||||
|
|
||||||
import com.labwork1.app.student.model.Session;
|
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.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
@ -11,13 +12,13 @@ import java.util.Optional;
|
|||||||
public interface SessionRepository extends JpaRepository<Session, Long> {
|
public interface SessionRepository extends JpaRepository<Session, Long> {
|
||||||
@Query("Select sum(os.count) from OrderSession os where os.session.id = :sessionId")
|
@Query("Select sum(os.count) from OrderSession os where os.session.id = :sessionId")
|
||||||
Optional<Integer> getCapacity(@Param("sessionId") Long sessionId);
|
Optional<Integer> 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 " +
|
"left join OrderSession os on s.id = os.session.id " +
|
||||||
"group by s")
|
"group by s")
|
||||||
List<Session> getSessionsWithCapacity();
|
List<SessionExtension> getSessionsWithCapacity();
|
||||||
@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 " +
|
"left join OrderSession os on s.id = os.session.id " +
|
||||||
"where s.id = :sessionId " +
|
"where s.id = :sessionId " +
|
||||||
"group by s")
|
"group by s")
|
||||||
Optional<Session> getSessionWithCapacity(@Param("sessionId") Long sessionId);
|
Optional<SessionExtension> getSessionWithCapacity(@Param("sessionId") Long sessionId);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.labwork1.app.student.service;
|
|||||||
|
|
||||||
import com.labwork1.app.student.model.Cinema;
|
import com.labwork1.app.student.model.Cinema;
|
||||||
import com.labwork1.app.student.model.Session;
|
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.student.repository.SessionRepository;
|
||||||
import com.labwork1.app.util.validation.ValidatorUtil;
|
import com.labwork1.app.util.validation.ValidatorUtil;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -32,13 +33,13 @@ public class SessionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Session findSession(Long id) {
|
public SessionExtension findSession(Long id) {
|
||||||
return sessionRepository.getSessionWithCapacity(id)
|
return sessionRepository.getSessionWithCapacity(id)
|
||||||
.orElseThrow(() -> new SessionNotFoundException(id));
|
.orElseThrow(() -> new SessionNotFoundException(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<Session> findAllSessions() {
|
public List<SessionExtension> findAllSessions() {
|
||||||
return sessionRepository.getSessionsWithCapacity();
|
return sessionRepository.getSessionsWithCapacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ public class JpaCustomerTests {
|
|||||||
// у заказа 2 сеанса
|
// у заказа 2 сеанса
|
||||||
orderService.addSession(order2.getId(), session1.getId(), 2);
|
orderService.addSession(order2.getId(), session1.getId(), 2);
|
||||||
|
|
||||||
List<Session> result = sessionService.findAllSessions();
|
List<SessionExtension> result = sessionService.findAllSessions();
|
||||||
|
|
||||||
Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2);
|
Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user