potom dodelaetsya

This commit is contained in:
dasha 2023-04-14 14:21:30 +04:00
parent e2a29f4b65
commit 0fafffd509
11 changed files with 2447 additions and 1018 deletions

3342
front/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -140,7 +140,7 @@ export default function Orders() {
.catch(e => { console.log('Error get orders') })
}
async function handleDeleteOrderSession(id, sessionId) {
async function handleDeleteOrderSession(e, id, sessionId) {
console.info('Start delete session');
console.info(id+'-order, session-'+sessionId)
const requestParams = {
@ -233,7 +233,7 @@ export default function Orders() {
<OrderSessionItem
item={item}
key={parseInt(item.sessionId+''+item.orderId)}
removeFunc={(e) => handleDeleteOrderSession(e, currEditItem, item.sessionId)}
removeFunc={e => handleDeleteOrderSession(e, currEditItem, item.sessionId)}
/>
) : null}
</tbody>

View File

@ -13,12 +13,12 @@ export default function SearchSame() {
useEffect(() => {
let temp = JSON.stringify(searchResult);
temp = JSON.parse(temp);
setItems(temp.filter(elem => elem.name.toLowerCase().includes(params.request.toLowerCase())));
setItems(temp);
}, [searchResult, params]);
useState(() => {
useEffect(() => {
const fetchData = async () => {
const url = new URL('http://localhost:8080/cinema')
const url = new URL(`http://localhost:8080/cinema/search?request=${params.request}`)
try {
const response = await fetch(url.href);
const json = await response.json();

View File

@ -27,6 +27,13 @@ public class CinemaController {
.toList();
}
@GetMapping("/search")
public List<CinemaDto> getCinemas(@RequestParam("request") String request) {
return cinemaService.findAllCinemas(request).stream()
.map(CinemaDto::new)
.toList();
}
@PostMapping
public CinemaDto createCinema(@RequestBody @Valid CinemaDto cinemaDto) {
return new CinemaDto(cinemaService.addCinema(cinemaDto));

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)));
return new SessionDto(sessionService.updateSession(id, Double.parseDouble(price), -1));
}
@DeleteMapping("/{id}")

View File

@ -2,6 +2,12 @@ package com.labwork1.app.student.repository;
import com.labwork1.app.student.model.Cinema;
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;
public interface CinemaRepository extends JpaRepository<Cinema, Long> {
@Query("select p from Cinema p where p.name like CONCAT('%',:search,'%')")
List<Cinema> findAll(@Param("search") String search);
}

View File

@ -2,6 +2,14 @@ package com.labwork1.app.student.repository;
import com.labwork1.app.student.model.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface CustomerRepository extends JpaRepository<Customer, Long>, CustomerRepositoryExtension {
import java.util.Optional;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
@Query("delete from Order p where p.customer.id = :id")
Optional<Customer> deleteCustomerOrders(@Param("id") Long id);
@Query()
void deleteAllCustomers();
}

View File

@ -2,6 +2,23 @@ package com.labwork1.app.student.repository;
import com.labwork1.app.student.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface OrderRepository extends JpaRepository<Order, Long>, OrderRepositoryExtension {
}
public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("insert into OrderSession values (orderId, sessionId, count) values (:sessionId, :orderId, :count)")
Order addSession(@Param("orderId") Long orderId, @Param("sessionId") Long sessionId,
@Param("count") Integer count);
@Query(value = "Select count from OrderSession where orderId = :orderId and sessionId = :sessionId",
nativeQuery = true)
Integer getCountOrderSession(@Param("orderId") Long orderId, @Param("sessionId") Long sessionId);
@Query(value = "delete from OrderSession where orderId = :orderId and sessionId = :sessionId",
nativeQuery = true)
void deleteSessionInOrder(@Param("orderId") Long orderId, @Param("sessionId") Long sessionId);
@Query(value = "update OrderSession set count = :count where orderId = :orderId and sessionId = :sessionId",
nativeQuery = true)
void updateSessionInOrder(@Param("orderId") Long orderId, @Param("sessionId") Long sessionId, @Param("count") Integer count);
}

View File

@ -2,6 +2,7 @@ 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;
@ -40,6 +41,11 @@ public class CinemaService {
return cinema.orElseThrow(() -> new CinemaNotFoundException(id));
}
@Transactional
public List<Cinema> findAllCinemas(String search) {
return cinemaRepository.findAll(search);
}
@Transactional
public List<Cinema> findAllCinemas() {
return cinemaRepository.findAll();

View File

@ -1,6 +1,6 @@
package com.labwork1.app.student.service;
import com.labwork1.app.student.model.Order;
import com.labwork1.app.student.model.*;
import com.labwork1.app.student.repository.OrderRepository;
import com.labwork1.app.util.validation.ValidatorUtil;
import jakarta.persistence.EntityNotFoundException;
@ -14,24 +14,51 @@ import java.util.Optional;
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final CustomerService customerService;
private final SessionService sessionService;
private final ValidatorUtil validatorUtil;
public OrderService(OrderRepository orderRepository, ValidatorUtil validatorUtil) {
public OrderService(OrderRepository orderRepository, CustomerService customerService, SessionService sessionService, ValidatorUtil validatorUtil) {
this.orderRepository = orderRepository;
this.customerService = customerService;
this.sessionService = sessionService;
this.validatorUtil = validatorUtil;
}
@Transactional
public Order addOrder(Long customer_id) {
public Order addOrder(Long customerId) {
final Order order = new Order(new Date(System.currentTimeMillis()));
final Customer customer = customerService.findCustomer(customerId);
order.setCustomer(customer);
validatorUtil.validate(order);
return orderRepository.addOrder(order, customer_id);
return orderRepository.save(order);
}
@Transactional
public Order addSession(Long id, Long session, Integer count) {
final Optional<Order> order = orderRepository.addSession(id, session, count);
return order.orElseThrow(() -> new EntityNotFoundException("orderId: " + id + ", sessionId: " + session));
public Order addSession(Long id, Long sessionId, Integer count) {
Optional<Order> optionalOrder = orderRepository.findById(id);
final Session currentSession = sessionService.findSession(sessionId);
if (optionalOrder.isEmpty()) {
throw new OrderNotFoundException(id);
}
Order currentOrder = optionalOrder.get();
final Integer countOrderSession = orderRepository.getCountOrderSession(id, sessionId);
if (currentSession.getCapacity() < count ||
(countOrderSession != null && countOrderSession + count > currentSession.getMaxCount())) {
throw new IllegalArgumentException(String.format("No more tickets in sessionId. Capacity: %1$s. Count: %2$s",
currentSession.getCapacity(), count));
}
if (countOrderSession == null) {
currentOrder.addSession(new OrderSession(currentOrder, currentSession, count));
return orderRepository.save(currentOrder);
}
else if (countOrderSession + count <= currentSession.getMaxCount()) {
orderRepository.delete(currentOrder);
orderRepository.addSession(id, sessionId, countOrderSession + count);
}
return optionalOrder.get();
}
@Transactional(readOnly = true)
@ -53,9 +80,19 @@ public class OrderService {
}
@Transactional
public Order deleteSessionInOrder(Long id, Long session, Integer count) {
final Optional<Order> order = orderRepository.deleteSessionInOrder(id, session, count);
return order.orElseThrow(() -> new EntityNotFoundException("orderId: " + id + ", sessionId: " + session));
public void deleteSessionInOrder(Long id, Long session, Integer count) {
final Order currentOrder = findOrder(id);
final Session currentSession = sessionService.findSession(session);
Integer countOrderSession = orderRepository.getCountOrderSession(id, session);
if (countOrderSession == null)
throw new EntityNotFoundException();
if (count >= countOrderSession) {
orderRepository.deleteSessionInOrder(id, session);
sessionService.updateSession(session, currentSession.getPrice(),
currentSession.getCapacity() + countOrderSession);
}
else
orderRepository.updateSessionInOrder(id, session, count);
}
@Transactional

View File

@ -39,9 +39,11 @@ public class SessionService {
}
@Transactional
public Session updateSession(Long id, Double price) {
public Session updateSession(Long id, Double price, Integer capacity) {
final Session currentSession = findSession(id);
currentSession.setPrice(price);
if (capacity != -1)
currentSession.setCapacity(capacity);
validatorUtil.validate(currentSession);
return sessionRepository.save(currentSession);
}