This commit is contained in:
dasha 2023-04-09 19:54:51 +04:00
parent cf01392e65
commit 5d8837809a
2 changed files with 7 additions and 4 deletions

View File

@ -7,7 +7,6 @@ import jakarta.persistence.*;
@Table(name = "order_session") @Table(name = "order_session")
public class OrderSession { public class OrderSession {
@EmbeddedId @EmbeddedId
@JsonIgnore
private OrderSessionKey id; private OrderSessionKey id;
@ManyToOne @ManyToOne
@MapsId("sessionId") @MapsId("sessionId")

View File

@ -10,6 +10,7 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional; import java.util.Optional;
@ -38,6 +39,7 @@ public class OrderRepositoryImpl implements OrderRepositoryExtension {
} }
@Override @Override
@Transactional
public Optional<Order> addSession(Long id, Long session, Integer count) { public Optional<Order> addSession(Long id, Long session, Integer count) {
if (session <= 0 || count <= 0) { if (session <= 0 || count <= 0) {
throw new IllegalArgumentException("addOrder empty fields"); throw new IllegalArgumentException("addOrder empty fields");
@ -54,19 +56,21 @@ public class OrderRepositoryImpl implements OrderRepositoryExtension {
if (currentSession.getCapacity() < count || if (currentSession.getCapacity() < count ||
(orderSession != null && orderSession.getCount() + count > currentSession.getMaxCount())) { (orderSession != null && orderSession.getCount() + count > currentSession.getMaxCount())) {
throw new IllegalArgumentException("No more tickets in session. Capacity - "+currentSession.getCapacity()+". Count - "+count); throw new IllegalArgumentException(String.format("No more tickets in session. Capacity: %1$s. Count: %2$s",
currentSession.getCapacity(), count));
} }
if (orderSession == null) if (orderSession == null)
orderSession = new OrderSession(currentOrder, currentSession, count); orderSession = new OrderSession(currentOrder, currentSession, count);
else if (orderSession.getCount() + count <= currentSession.getCapacity()) else if (orderSession.getCount() + count <= currentSession.getMaxCount()) {
orderSession.setCount(orderSession.getCount() + count); orderSession.setCount(orderSession.getCount() + count);
}
currentOrder.addSession(orderSession); currentOrder.addSession(orderSession);
currentSession.addOrder(orderSession); currentSession.addOrder(orderSession);
currentSession.setCapacity(currentSession.getCapacity() - count); currentSession.setCapacity(currentSession.getCapacity() - count);
em.merge(orderSession);
em.merge(currentSession); em.merge(currentSession);
em.merge(currentOrder); em.merge(currentOrder);
em.merge(orderSession);
return orderRepository.findById(id); return orderRepository.findById(id);
} }