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); }