diff --git a/front/src/pages/Sessions.jsx b/front/src/pages/Sessions.jsx index 20986cc..b9d0ae5 100644 --- a/front/src/pages/Sessions.jsx +++ b/front/src/pages/Sessions.jsx @@ -154,7 +154,6 @@ export default function Sessions() { Price Cinema Timestamp - Capacity MaxCount diff --git a/front/src/pages/components/OrderItem.jsx b/front/src/pages/components/OrderItem.jsx index c2b08dc..4f08c0b 100644 --- a/front/src/pages/components/OrderItem.jsx +++ b/front/src/pages/components/OrderItem.jsx @@ -1,25 +1,11 @@ -import React, { useEffect, useState } from "react" +import React from "react" import MyButton from './MyButton' -import CustomerDto from "../models/CustomerDto"; -import Service from "../services/Service"; export default function OrderItem(props) { - const [customer, setCustomer] = useState(new CustomerDto()); - - useEffect(() => { - Service.read("customer/" + props.item.customer.id) - .then((data) => { - setCustomer(data) - }) - .catch((error) => { - console.error('Error:', error); - }); - }, []) - return ( {props.item.id} - {customer.login} + {props.item.customer.login} {props.item.dateOfPurchase} diff --git a/front/src/pages/components/SessionItem.jsx b/front/src/pages/components/SessionItem.jsx index 6a745c6..0dc0062 100644 --- a/front/src/pages/components/SessionItem.jsx +++ b/front/src/pages/components/SessionItem.jsx @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react" import MyButton from './MyButton' export default function SessionItem(props) { - const date = new Date(props.item.timestamp).toLocaleDateString('ru-RU') + const date = new Date(props.item.timestamp).toLocaleString('RU-ru') return ( @@ -10,7 +10,6 @@ export default function SessionItem(props) { {props.item.price} {props.item.cinema.name} {date} - {props.item.capacity} {props.item.maxCount} diff --git a/front/src/pages/models/SessionDto.js b/front/src/pages/models/SessionDto.js deleted file mode 100644 index c720b8c..0000000 --- a/front/src/pages/models/SessionDto.js +++ /dev/null @@ -1,11 +0,0 @@ -export default class SessionDto { - constructor(price, timestamp, maxCount, id) { - this.price = parseFloat(price); - this.timestamp = Date.parse(timestamp); - this.maxCount = parseInt(maxCount); - this.id = parseInt(id); - } - static createFrom(item) { - return new SessionDto(item.image, item.name, item.id); - } -} \ No newline at end of file diff --git a/src/main/java/com/labwork1/app/student/controller/SessionController.java b/src/main/java/com/labwork1/app/student/controller/SessionController.java index fc2e8cd..12eea04 100644 --- a/src/main/java/com/labwork1/app/student/controller/SessionController.java +++ b/src/main/java/com/labwork1/app/student/controller/SessionController.java @@ -45,7 +45,7 @@ public class SessionController { @PutMapping("/{id}") public SessionDto updateSession(@PathVariable Long id, @RequestParam("price") String price) { - return new SessionDto(sessionService.updateSession(id, Double.parseDouble(price), -1)); + return new SessionDto(sessionService.updateSession(id, Double.parseDouble(price))); } @DeleteMapping("/{id}") 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 c58cb49..dd0dd01 100644 --- a/src/main/java/com/labwork1/app/student/controller/SessionDto.java +++ b/src/main/java/com/labwork1/app/student/controller/SessionDto.java @@ -20,7 +20,6 @@ public class SessionDto { this.id = session.getId(); this.price = session.getPrice(); this.timestamp = session.getTimestamp(); - this.capacity = session.getCapacity(); this.maxCount = session.getMaxCount(); if (session.getCinema() != null) this.cinema = new CinemaDto(session.getCinema()); else this.cinema = null; @@ -34,6 +33,10 @@ public class SessionDto { return capacity; } + public void setCapacity(int capacity) { + this.capacity = capacity; + } + public long getId() { return id; } 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 49142aa..0749010 100644 --- a/src/main/java/com/labwork1/app/student/model/Session.java +++ b/src/main/java/com/labwork1/app/student/model/Session.java @@ -24,9 +24,6 @@ public class Session { @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "cinema_fk") private Cinema cinema; - @NotNull(message = "capacity can't be null or empty") - @Column - private Integer capacity; @NotNull(message = "maxCount can't be null or empty") @Column private Integer maxCount; @@ -42,7 +39,6 @@ public class Session { this.price = price; this.timestamp = timestamp; this.maxCount = maxCount; - this.capacity = maxCount; } public Cinema getCinema() { @@ -87,6 +83,8 @@ public class Session { "id=" + id + ", price='" + price + '\'' + ", timestamp='" + timestamp.toString() + '\'' + + ", maxCount='" + maxCount.toString() + '\'' + + ", cinema='" + cinema.toString() + '\'' + '}'; } @@ -113,12 +111,4 @@ public class Session { public List getOrders() { return orders; } - - public Integer getCapacity() { - return capacity; - } - - public void setCapacity(Integer 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 b4907fd..85ba4c4 100644 --- a/src/main/java/com/labwork1/app/student/repository/SessionRepository.java +++ b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java @@ -5,11 +5,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import java.util.List; +import java.util.Optional; public interface SessionRepository extends JpaRepository { - @Query("Select s from Session s where s.capacity > 0") - List findAllSessionsWithCapacity(); @Query("Select sum(os.count) from OrderSession os where os.session.id = :sessionId") - Integer getCapacity(@Param("sessionId") Long sessionId); + Optional getCapacity(@Param("sessionId") Long sessionId); } diff --git a/src/main/java/com/labwork1/app/student/service/CinemaService.java b/src/main/java/com/labwork1/app/student/service/CinemaService.java index a84839e..6b4c936 100644 --- a/src/main/java/com/labwork1/app/student/service/CinemaService.java +++ b/src/main/java/com/labwork1/app/student/service/CinemaService.java @@ -2,7 +2,6 @@ package com.labwork1.app.student.service; import com.labwork1.app.student.controller.CinemaDto; import com.labwork1.app.student.model.Cinema; -import com.labwork1.app.student.model.Order; import com.labwork1.app.student.repository.CinemaRepository; import com.labwork1.app.util.validation.ValidatorUtil; import jakarta.transaction.Transactional; diff --git a/src/main/java/com/labwork1/app/student/service/CustomerService.java b/src/main/java/com/labwork1/app/student/service/CustomerService.java index 1deffe5..63e6308 100644 --- a/src/main/java/com/labwork1/app/student/service/CustomerService.java +++ b/src/main/java/com/labwork1/app/student/service/CustomerService.java @@ -1,8 +1,6 @@ package com.labwork1.app.student.service; import com.labwork1.app.student.model.Customer; -import com.labwork1.app.student.model.Order; -import com.labwork1.app.student.model.OrderSession; import com.labwork1.app.student.repository.CustomerRepository; import com.labwork1.app.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; @@ -14,12 +12,10 @@ import java.util.Optional; @Service public class CustomerService { private final CustomerRepository customerRepository; - private final SessionService sessionService; private final ValidatorUtil validatorUtil; - public CustomerService(CustomerRepository customerRepository, SessionService sessionService, ValidatorUtil validatorUtil) { + public CustomerService(CustomerRepository customerRepository, ValidatorUtil validatorUtil) { this.customerRepository = customerRepository; - this.sessionService = sessionService; this.validatorUtil = validatorUtil; } @@ -53,18 +49,6 @@ public class CustomerService { @Transactional public Customer deleteCustomer(Long id) { final Customer customer = findCustomer(id); - // заказы покупателя - final List orders = customer.getOrders(); - - for (Order order : orders) { - // сеансы в заказе - for (OrderSession orderSession : order.getSessions()) { - // освобождаем сеансы - sessionService.updateSession(orderSession.getSession().getId(), - orderSession.getSession().getPrice(), orderSession.getSession().getMaxCount()); - } - } - customerRepository.deleteById(id); return customer; } diff --git a/src/main/java/com/labwork1/app/student/service/OrderService.java b/src/main/java/com/labwork1/app/student/service/OrderService.java index 9919051..a65390a 100644 --- a/src/main/java/com/labwork1/app/student/service/OrderService.java +++ b/src/main/java/com/labwork1/app/student/service/OrderService.java @@ -40,12 +40,13 @@ public class OrderService { final Order currentOrder = findOrder(id); final OrderSession currentOrderSession = orderRepository.getOrderSession(id, sessionId); - if (currentSession.getCapacity() < count || + final Integer currentSessionCapacity = currentSession.getMaxCount() - sessionService.getCapacity(sessionId); + if (currentSessionCapacity < count || (currentOrderSession != null && currentOrderSession.getCount() + count > currentSession.getMaxCount())) { throw new IllegalArgumentException(String.format("No more tickets in session. Capacity: %1$s. Count: %2$s", - currentSession.getCapacity(), count)); + currentSessionCapacity, count)); } - currentSession.setCapacity(currentSession.getCapacity() - count); + if (currentOrderSession == null) { currentOrder.addSession(new OrderSession(currentOrder, currentSession, count)); } @@ -72,14 +73,6 @@ public class OrderService { @Transactional public Order deleteOrder(Long id) { final Order currentOrder = findOrder(id); - - // сеансы в заказе - for (OrderSession orderSession : currentOrder.getSessions()) { - // освобождаем сеансы - sessionService.updateSession(orderSession.getSession().getId(), - orderSession.getSession().getPrice(), orderSession.getSession().getMaxCount()); - } - orderRepository.delete(currentOrder); return currentOrder; } @@ -94,12 +87,9 @@ public class OrderService { if (count >= currentOrderSession.getCount()) { currentOrder.removeSession(currentOrderSession); - sessionService.updateSession(session, currentSession.getPrice(), - currentSession.getCapacity() + currentOrderSession.getCount()); } else { currentOrder.removeSession(currentOrderSession); - currentSession.setCapacity(currentSession.getCapacity() + count); currentOrder.addSession(new OrderSession(currentOrder, currentSession, currentOrderSession.getCount() - count)); } 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 983f525..c1c56dd 100644 --- a/src/main/java/com/labwork1/app/student/service/SessionService.java +++ b/src/main/java/com/labwork1/app/student/service/SessionService.java @@ -45,12 +45,9 @@ public class SessionService { } @Transactional - public Session updateSession(Long id, Double price, Integer capacity) { + public Session updateSession(Long id, Double price) { final Session currentSession = findSession(id); currentSession.setPrice(price); - // для обновления полей класса (цены) - if (capacity != -1) - currentSession.setCapacity(capacity); validatorUtil.validate(currentSession); return sessionRepository.save(currentSession); } @@ -72,7 +69,7 @@ public class SessionService { } @Transactional - public List findAllSessionsWithCapacity() { - return sessionRepository.findAllSessionsWithCapacity(); + public Integer getCapacity(Long sessionId) { + return sessionRepository.getCapacity(sessionId).orElse(0); } } diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index 5379cdc..84ffdd4 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -68,30 +68,26 @@ public class JpaCustomerTests { .addOrder(customerService.findCustomer(customer2.getId()).getId()); // у заказа 2 сеанса orderService.addSession(order2.getId(), session1.getId(), 2); - Assertions.assertEquals(sessionService.findSession(session1.getId()).getCapacity(), 8); + Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 8); orderService.addSession(order2.getId(), session2.getId(), 5); - Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 5); + Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 5); Assertions.assertThrows(IllegalArgumentException.class, () -> orderService.addSession(order2.getId(), session2.getId(), 6)); // у заказа 1 сеанс orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 10); - Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10); + Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 10); // заполнили всю 2 сессию orderService.addSession(order2.getId(), session2.getId(), 10); Assertions.assertEquals(sessionService.findAllSessions().size(), 2); - Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 1); - Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().get(0), session1); orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 4); - Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 2); - Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 4); + Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 4); orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 6); - Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 2); - Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10); + Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 10); Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().size(), 1); Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().get(0).getId().getSessionId(), session1.getId()); @@ -113,14 +109,14 @@ public class JpaCustomerTests { orderService.addSession(order3.getId(), session1.getId(), 8); // 2-ой покупатель удален // 0 заказов после его удаления - Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 8); + Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 8); customerService.deleteCustomer(customer2.getId()); Assertions.assertThrows(CustomerNotFoundException.class, () -> customerService.findCustomer(customer2.getId())); Assertions.assertThrows(OrderNotFoundException.class, () -> orderService.findOrder(order3.getId())); Assertions.assertEquals(orderService.findAllOrders().size(), 0); - Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10); - Assertions.assertEquals(sessionService.findSession(session3.getId()).getCapacity(), 10); + Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 10); + Assertions.assertEquals(sessionService.getCapacity(session3.getId()), 10); Assertions.assertEquals(cinemaService.findAllCinemas().size(), 2); Assertions.assertEquals(sessionService.findAllSessions().size(), 3);