From 5d8837809a78ecf469fdd394273dd00bfeacdf1e Mon Sep 17 00:00:00 2001 From: dasha Date: Sun, 9 Apr 2023 19:54:51 +0400 Subject: [PATCH] bugfix --- .../com/labwork1/app/student/model/OrderSession.java | 1 - .../student/repository/impl/OrderRepositoryImpl.java | 10 +++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/labwork1/app/student/model/OrderSession.java b/src/main/java/com/labwork1/app/student/model/OrderSession.java index 2e2fc8f..3859afa 100644 --- a/src/main/java/com/labwork1/app/student/model/OrderSession.java +++ b/src/main/java/com/labwork1/app/student/model/OrderSession.java @@ -7,7 +7,6 @@ import jakarta.persistence.*; @Table(name = "order_session") public class OrderSession { @EmbeddedId - @JsonIgnore private OrderSessionKey id; @ManyToOne @MapsId("sessionId") 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 index 6c666fd..51a1f99 100644 --- a/src/main/java/com/labwork1/app/student/repository/impl/OrderRepositoryImpl.java +++ b/src/main/java/com/labwork1/app/student/repository/impl/OrderRepositoryImpl.java @@ -10,6 +10,7 @@ import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; +import org.springframework.transaction.annotation.Transactional; import java.util.Optional; @@ -38,6 +39,7 @@ public class OrderRepositoryImpl implements OrderRepositoryExtension { } @Override + @Transactional public Optional addSession(Long id, Long session, Integer count) { if (session <= 0 || count <= 0) { throw new IllegalArgumentException("addOrder empty fields"); @@ -54,19 +56,21 @@ public class OrderRepositoryImpl implements OrderRepositoryExtension { if (currentSession.getCapacity() < count || (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) 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); + } currentOrder.addSession(orderSession); currentSession.addOrder(orderSession); currentSession.setCapacity(currentSession.getCapacity() - count); - em.merge(orderSession); em.merge(currentSession); em.merge(currentOrder); + em.merge(orderSession); return orderRepository.findById(id); }