вроде все

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">Cinema</th>
<th scope="col">Timestamp</th>
<th scope="col">Capacity</th>
<th scope="col">MaxCount</th>
<th scope="col"></th>
</tr>

View File

@ -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 (
<tr>
<td>{props.item.id}</td>
<td>{customer.login}</td>
<td>{props.item.customer.login}</td>
<td>{props.item.dateOfPurchase}</td>
<td><MyButton value={props} /></td>
</tr>

View File

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

View File

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

View File

@ -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<OrderSession> getOrders() {
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.repository.query.Param;
import java.util.List;
import java.util.Optional;
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")
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.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;

View File

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

View File

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

View File

@ -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<Session> findAllSessionsWithCapacity() {
return sessionRepository.findAllSessionsWithCapacity();
public Integer getCapacity(Long sessionId) {
return sessionRepository.getCapacity(sessionId).orElse(0);
}
}

View File

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