вроде все

This commit is contained in:
dasha 2023-04-17 15:52:23 +04:00
parent 1544973e7b
commit 1f4f290522
13 changed files with 28 additions and 98 deletions

View File

@ -154,7 +154,6 @@ export default function Sessions() {
<th scope="col">Price</th> <th scope="col">Price</th>
<th scope="col">Cinema</th> <th scope="col">Cinema</th>
<th scope="col">Timestamp</th> <th scope="col">Timestamp</th>
<th scope="col">Capacity</th>
<th scope="col">MaxCount</th> <th scope="col">MaxCount</th>
<th scope="col"></th> <th scope="col"></th>
</tr> </tr>

View File

@ -1,25 +1,11 @@
import React, { useEffect, useState } from "react" import React from "react"
import MyButton from './MyButton' import MyButton from './MyButton'
import CustomerDto from "../models/CustomerDto";
import Service from "../services/Service";
export default function OrderItem(props) { 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 ( return (
<tr> <tr>
<td>{props.item.id}</td> <td>{props.item.id}</td>
<td>{customer.login}</td> <td>{props.item.customer.login}</td>
<td>{props.item.dateOfPurchase}</td> <td>{props.item.dateOfPurchase}</td>
<td><MyButton value={props} /></td> <td><MyButton value={props} /></td>
</tr> </tr>

View File

@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react"
import MyButton from './MyButton' import MyButton from './MyButton'
export default function SessionItem(props) { 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 ( return (
<tr> <tr>
@ -10,7 +10,6 @@ export default function SessionItem(props) {
<td>{props.item.price}</td> <td>{props.item.price}</td>
<td>{props.item.cinema.name}</td> <td>{props.item.cinema.name}</td>
<td>{date}</td> <td>{date}</td>
<td>{props.item.capacity}</td>
<td>{props.item.maxCount}</td> <td>{props.item.maxCount}</td>
<td><MyButton value={props} /></td> <td><MyButton value={props} /></td>
</tr> </tr>

View File

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

View File

@ -45,7 +45,7 @@ public class SessionController {
@PutMapping("/{id}") @PutMapping("/{id}")
public SessionDto updateSession(@PathVariable Long id, public SessionDto updateSession(@PathVariable Long id,
@RequestParam("price") String price) { @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}") @DeleteMapping("/{id}")

View File

@ -20,7 +20,6 @@ public class SessionDto {
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.maxCount = session.getMaxCount(); this.maxCount = session.getMaxCount();
if (session.getCinema() != null) this.cinema = new CinemaDto(session.getCinema()); if (session.getCinema() != null) this.cinema = new CinemaDto(session.getCinema());
else this.cinema = null; else this.cinema = null;
@ -34,6 +33,10 @@ public class SessionDto {
return capacity; return capacity;
} }
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public long getId() { public long getId() {
return id; return id;
} }

View File

@ -24,9 +24,6 @@ public class Session {
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "cinema_fk") @JoinColumn(name = "cinema_fk")
private Cinema cinema; 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") @NotNull(message = "maxCount can't be null or empty")
@Column @Column
private Integer maxCount; private Integer maxCount;
@ -42,7 +39,6 @@ public class Session {
this.price = price; this.price = price;
this.timestamp = timestamp; this.timestamp = timestamp;
this.maxCount = maxCount; this.maxCount = maxCount;
this.capacity = maxCount;
} }
public Cinema getCinema() { public Cinema getCinema() {
@ -87,6 +83,8 @@ public class Session {
"id=" + id + "id=" + id +
", price='" + price + '\'' + ", price='" + price + '\'' +
", timestamp='" + timestamp.toString() + '\'' + ", timestamp='" + timestamp.toString() + '\'' +
", maxCount='" + maxCount.toString() + '\'' +
", cinema='" + cinema.toString() + '\'' +
'}'; '}';
} }
@ -113,12 +111,4 @@ public class Session {
public List<OrderSession> getOrders() { public List<OrderSession> getOrders() {
return orders; return orders;
} }
public Integer getCapacity() {
return capacity;
}
public void setCapacity(Integer capacity) {
this.capacity = capacity;
}
} }

View File

@ -5,11 +5,9 @@ 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;
import java.util.List; import java.util.Optional;
public interface SessionRepository extends JpaRepository<Session, Long> { public interface SessionRepository extends JpaRepository<Session, Long> {
@Query("Select s from Session s where s.capacity > 0")
List<Session> findAllSessionsWithCapacity();
@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")
Integer getCapacity(@Param("sessionId") Long sessionId); Optional<Integer> getCapacity(@Param("sessionId") Long sessionId);
} }

View File

@ -2,7 +2,6 @@ package com.labwork1.app.student.service;
import com.labwork1.app.student.controller.CinemaDto; import com.labwork1.app.student.controller.CinemaDto;
import com.labwork1.app.student.model.Cinema; 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.student.repository.CinemaRepository;
import com.labwork1.app.util.validation.ValidatorUtil; import com.labwork1.app.util.validation.ValidatorUtil;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;

View File

@ -1,8 +1,6 @@
package com.labwork1.app.student.service; package com.labwork1.app.student.service;
import com.labwork1.app.student.model.Customer; 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.student.repository.CustomerRepository;
import com.labwork1.app.util.validation.ValidatorUtil; import com.labwork1.app.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -14,12 +12,10 @@ import java.util.Optional;
@Service @Service
public class CustomerService { public class CustomerService {
private final CustomerRepository customerRepository; private final CustomerRepository customerRepository;
private final SessionService sessionService;
private final ValidatorUtil validatorUtil; private final ValidatorUtil validatorUtil;
public CustomerService(CustomerRepository customerRepository, SessionService sessionService, ValidatorUtil validatorUtil) { public CustomerService(CustomerRepository customerRepository, ValidatorUtil validatorUtil) {
this.customerRepository = customerRepository; this.customerRepository = customerRepository;
this.sessionService = sessionService;
this.validatorUtil = validatorUtil; this.validatorUtil = validatorUtil;
} }
@ -53,18 +49,6 @@ public class CustomerService {
@Transactional @Transactional
public Customer deleteCustomer(Long id) { public Customer deleteCustomer(Long id) {
final Customer customer = findCustomer(id); final Customer customer = findCustomer(id);
// заказы покупателя
final List<Order> 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); customerRepository.deleteById(id);
return customer; return customer;
} }

View File

@ -40,12 +40,13 @@ public class OrderService {
final Order currentOrder = findOrder(id); final Order currentOrder = findOrder(id);
final OrderSession currentOrderSession = orderRepository.getOrderSession(id, sessionId); 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())) { (currentOrderSession != null && currentOrderSession.getCount() + count > currentSession.getMaxCount())) {
throw new IllegalArgumentException(String.format("No more tickets in session. Capacity: %1$s. Count: %2$s", 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) { if (currentOrderSession == null) {
currentOrder.addSession(new OrderSession(currentOrder, currentSession, count)); currentOrder.addSession(new OrderSession(currentOrder, currentSession, count));
} }
@ -72,14 +73,6 @@ public class OrderService {
@Transactional @Transactional
public Order deleteOrder(Long id) { public Order deleteOrder(Long id) {
final Order currentOrder = findOrder(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); orderRepository.delete(currentOrder);
return currentOrder; return currentOrder;
} }
@ -94,12 +87,9 @@ public class OrderService {
if (count >= currentOrderSession.getCount()) { if (count >= currentOrderSession.getCount()) {
currentOrder.removeSession(currentOrderSession); currentOrder.removeSession(currentOrderSession);
sessionService.updateSession(session, currentSession.getPrice(),
currentSession.getCapacity() + currentOrderSession.getCount());
} }
else { else {
currentOrder.removeSession(currentOrderSession); currentOrder.removeSession(currentOrderSession);
currentSession.setCapacity(currentSession.getCapacity() + count);
currentOrder.addSession(new OrderSession(currentOrder, currentSession, currentOrder.addSession(new OrderSession(currentOrder, currentSession,
currentOrderSession.getCount() - count)); currentOrderSession.getCount() - count));
} }

View File

@ -45,12 +45,9 @@ public class SessionService {
} }
@Transactional @Transactional
public Session updateSession(Long id, Double price, Integer capacity) { public Session updateSession(Long id, Double price) {
final Session currentSession = findSession(id); final Session currentSession = findSession(id);
currentSession.setPrice(price); currentSession.setPrice(price);
// для обновления полей класса (цены)
if (capacity != -1)
currentSession.setCapacity(capacity);
validatorUtil.validate(currentSession); validatorUtil.validate(currentSession);
return sessionRepository.save(currentSession); return sessionRepository.save(currentSession);
} }
@ -72,7 +69,7 @@ public class SessionService {
} }
@Transactional @Transactional
public List<Session> findAllSessionsWithCapacity() { public Integer getCapacity(Long sessionId) {
return sessionRepository.findAllSessionsWithCapacity(); return sessionRepository.getCapacity(sessionId).orElse(0);
} }
} }

View File

@ -68,30 +68,26 @@ public class JpaCustomerTests {
.addOrder(customerService.findCustomer(customer2.getId()).getId()); .addOrder(customerService.findCustomer(customer2.getId()).getId());
// у заказа 2 сеанса // у заказа 2 сеанса
orderService.addSession(order2.getId(), session1.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); 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, () -> Assertions.assertThrows(IllegalArgumentException.class, () ->
orderService.addSession(order2.getId(), session2.getId(), 6)); orderService.addSession(order2.getId(), session2.getId(), 6));
// у заказа 1 сеанс // у заказа 1 сеанс
orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 10); orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 10);
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10); Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 10);
// заполнили всю 2 сессию // заполнили всю 2 сессию
orderService.addSession(order2.getId(), session2.getId(), 10); orderService.addSession(order2.getId(), session2.getId(), 10);
Assertions.assertEquals(sessionService.findAllSessions().size(), 2); 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); orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 4);
Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 2); Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 4);
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 4);
orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 6); orderService.deleteSessionInOrder(order2.getId(), session2.getId(), 6);
Assertions.assertEquals(sessionService.findAllSessionsWithCapacity().size(), 2); Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 10);
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10);
Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().size(), 1); Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().size(), 1);
Assertions.assertEquals(orderService.findOrder(order2.getId()).getSessions().get(0).getId().getSessionId(), session1.getId()); 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); orderService.addSession(order3.getId(), session1.getId(), 8);
// 2-ой покупатель удален // 2-ой покупатель удален
// 0 заказов после его удаления // 0 заказов после его удаления
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 8); Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 8);
customerService.deleteCustomer(customer2.getId()); customerService.deleteCustomer(customer2.getId());
Assertions.assertThrows(CustomerNotFoundException.class, () -> customerService.findCustomer(customer2.getId())); Assertions.assertThrows(CustomerNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
Assertions.assertThrows(OrderNotFoundException.class, () -> orderService.findOrder(order3.getId())); Assertions.assertThrows(OrderNotFoundException.class, () -> orderService.findOrder(order3.getId()));
Assertions.assertEquals(orderService.findAllOrders().size(), 0); Assertions.assertEquals(orderService.findAllOrders().size(), 0);
Assertions.assertEquals(sessionService.findSession(session2.getId()).getCapacity(), 10); Assertions.assertEquals(sessionService.getCapacity(session2.getId()), 10);
Assertions.assertEquals(sessionService.findSession(session3.getId()).getCapacity(), 10); Assertions.assertEquals(sessionService.getCapacity(session3.getId()), 10);
Assertions.assertEquals(cinemaService.findAllCinemas().size(), 2); Assertions.assertEquals(cinemaService.findAllCinemas().size(), 2);
Assertions.assertEquals(sessionService.findAllSessions().size(), 3); Assertions.assertEquals(sessionService.findAllSessions().size(), 3);