сделала кое-как как правильно делать не знаю
This commit is contained in:
parent
3aff5cb712
commit
63dcaa4d7a
@ -154,6 +154,7 @@ 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>
|
||||
|
@ -1,39 +1,17 @@
|
||||
import React, { useEffect, useState } from "react"
|
||||
import CinemaDto from "../models/CinemaDto";
|
||||
import Service from "../services/Service";
|
||||
import React from "react"
|
||||
import MyButton from "./MyButton";
|
||||
|
||||
export default function OrderSessionItem(props) {
|
||||
const [item, setItem] = useState(new CinemaDto())
|
||||
const [load, setLoad] = useState(false)
|
||||
const [date, setDate] = useState(new Date())
|
||||
|
||||
useEffect(() => {
|
||||
Service.read("session/" + props.item.sessionId)
|
||||
.then((data) => {
|
||||
setItem(data)
|
||||
setLoad(true)
|
||||
setDate(new Date(data.timestamp).toLocaleString('RU-ru'))
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
return () => {
|
||||
setLoad(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
{load ?
|
||||
<tr>
|
||||
<td>{item.id}</td>
|
||||
<td>{item.price}</td>
|
||||
<td>{item.cinema.name}</td>
|
||||
<td>{date}</td>
|
||||
<td>{props.item.count}</td>
|
||||
<td><MyButton value={props} /></td>
|
||||
</tr> : null}
|
||||
<tr>
|
||||
<td>{props.item.session.id}</td>
|
||||
<td>{props.item.session.price}</td>
|
||||
<td>{props.item.session.cinema.name}</td>
|
||||
<td>{new Date(props.item.session.timestamp).toLocaleString('RU-ru')}</td>
|
||||
<td>{props.item.count}</td>
|
||||
<td><MyButton value={props} /></td>
|
||||
</tr>
|
||||
</>
|
||||
)
|
||||
}
|
@ -3,13 +3,14 @@ import MyButton from './MyButton'
|
||||
|
||||
export default function SessionItem(props) {
|
||||
const date = new Date(props.item.timestamp).toLocaleString('RU-ru')
|
||||
|
||||
|
||||
return (
|
||||
<tr>
|
||||
<td>{props.item.id}</td>
|
||||
<td>{props.item.price}</td>
|
||||
<td>{props.item.cinema.name}</td>
|
||||
<td>{date}</td>
|
||||
<td>{props.item.maxCount - props.item.capacity}</td>
|
||||
<td>{props.item.maxCount}</td>
|
||||
<td><MyButton value={props} /></td>
|
||||
</tr>
|
||||
|
@ -23,7 +23,7 @@ public class OrderDto {
|
||||
if (order.getSessions() != null && order.getSessions().size() > 0)
|
||||
this.sessions = order.getSessions()
|
||||
.stream()
|
||||
.map(x -> new OrderSessionDto(x.getId().getSessionId(),
|
||||
.map(x -> new OrderSessionDto(new SessionDto(x.getSession()),
|
||||
x.getId().getOrderId(), x.getCount())).toList();
|
||||
}
|
||||
|
||||
|
@ -1,21 +1,21 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
public class OrderSessionDto {
|
||||
private Long sessionId;
|
||||
private SessionDto session;
|
||||
private Long orderId;
|
||||
private Integer count;
|
||||
|
||||
public OrderSessionDto() {
|
||||
}
|
||||
|
||||
public OrderSessionDto(Long sessionId, Long orderId, Integer count) {
|
||||
this.sessionId = sessionId;
|
||||
public OrderSessionDto(SessionDto session, Long orderId, Integer count) {
|
||||
this.session = session;
|
||||
this.orderId = orderId;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public Long getSessionId() {
|
||||
return sessionId;
|
||||
public SessionDto getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
public Long getOrderId() {
|
||||
|
@ -38,14 +38,16 @@ public class SessionController {
|
||||
SimpleDateFormat format = new SimpleDateFormat();
|
||||
format.applyPattern("yyyy-MM-dd-HH:ss");
|
||||
Date docDate = format.parse(timestamp.replace('T', '-'));
|
||||
return new SessionDto(sessionService.addSession(Double.parseDouble(price),
|
||||
new Timestamp(docDate.getTime()), cinemaId, capacity));
|
||||
return new SessionDto(sessionService.findSession(
|
||||
sessionService.addSession(Double.parseDouble(price),
|
||||
new Timestamp(docDate.getTime()), cinemaId, capacity).getId()));
|
||||
}
|
||||
|
||||
@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.findSession(sessionService
|
||||
.updateSession(id, Double.parseDouble(price)).getId()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.labwork1.app.student.controller;
|
||||
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
import com.labwork1.app.student.model.Session;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
@ -10,7 +9,8 @@ public class SessionDto {
|
||||
private Double price;
|
||||
private Timestamp timestamp;
|
||||
private CinemaDto cinema;
|
||||
private int capacity;
|
||||
private Long capacity;
|
||||
|
||||
private int maxCount;
|
||||
|
||||
public SessionDto() {
|
||||
@ -20,6 +20,7 @@ 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;
|
||||
@ -29,14 +30,6 @@ public class SessionDto {
|
||||
return maxCount;
|
||||
}
|
||||
|
||||
public int getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public void setCapacity(int capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
@ -49,6 +42,10 @@ public class SessionDto {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public Long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public CinemaDto getCinema() {
|
||||
return cinema;
|
||||
}
|
||||
|
@ -27,6 +27,8 @@ public class Session {
|
||||
@NotNull(message = "maxCount can't be null or empty")
|
||||
@Column
|
||||
private Integer maxCount;
|
||||
@Transient
|
||||
private Long capacity;
|
||||
|
||||
public Session() {
|
||||
}
|
||||
@ -41,6 +43,16 @@ public class Session {
|
||||
this.maxCount = maxCount;
|
||||
}
|
||||
|
||||
public Session(Session session, Long capacity) {
|
||||
this.id = session.getId();
|
||||
this.price = session.getPrice();
|
||||
this.timestamp = session.getTimestamp();
|
||||
this.orders = session.getOrders();
|
||||
this.cinema = session.getCinema();
|
||||
this.maxCount = session.getMaxCount();
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public Cinema getCinema() {
|
||||
return cinema;
|
||||
}
|
||||
@ -83,6 +95,7 @@ public class Session {
|
||||
"id=" + id +
|
||||
", price='" + price + '\'' +
|
||||
", timestamp='" + timestamp.toString() + '\'' +
|
||||
", capacity='" + capacity + '\'' +
|
||||
", maxCount='" + maxCount.toString() + '\'' +
|
||||
", cinema='" + cinema.toString() + '\'' +
|
||||
'}';
|
||||
@ -96,6 +109,18 @@ public class Session {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setOrders(List<OrderSession> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public void setMaxCount(Integer maxCount) {
|
||||
this.maxCount = maxCount;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
@ -111,4 +136,12 @@ public class Session {
|
||||
public List<OrderSession> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public Long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public void setCapacity(Long capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,19 @@ 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 sum(os.count) from OrderSession os where os.session.id = :sessionId")
|
||||
Optional<Integer> getCapacity(@Param("sessionId") Long sessionId);
|
||||
@Query("Select new com.labwork1.app.student.model.Session(s, sum(os.count)) from Session s " +
|
||||
"left join OrderSession os on s.id = os.session.id " +
|
||||
"group by s")
|
||||
List<Session> getSessionsWithCapacity();
|
||||
@Query("Select new com.labwork1.app.student.model.Session(s, sum(os.count)) from Session s " +
|
||||
"left join OrderSession os on s.id = os.session.id " +
|
||||
"where s.id = :sessionId " +
|
||||
"group by s")
|
||||
Optional<Session> getSessionWithCapacity(@Param("sessionId") Long sessionId);
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class SessionService {
|
||||
@ -35,13 +33,13 @@ public class SessionService {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Session findSession(Long id) {
|
||||
final Optional<Session> session = sessionRepository.findById(id);
|
||||
return session.orElseThrow(() -> new SessionNotFoundException(id));
|
||||
return sessionRepository.getSessionWithCapacity(id)
|
||||
.orElseThrow(() -> new SessionNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Session> findAllSessions() {
|
||||
return sessionRepository.findAll();
|
||||
return sessionRepository.getSessionsWithCapacity();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -1,10 +1,7 @@
|
||||
package com.labwork1.app;
|
||||
|
||||
import com.labwork1.app.student.model.Customer;
|
||||
import com.labwork1.app.student.model.Order;
|
||||
import com.labwork1.app.student.model.Session;
|
||||
import com.labwork1.app.student.model.*;
|
||||
import com.labwork1.app.student.service.*;
|
||||
import com.labwork1.app.student.model.Cinema;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
@ -42,6 +39,7 @@ public class JpaCustomerTests {
|
||||
new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10);
|
||||
final Session session2 = sessionService.addSession( 200.0,
|
||||
new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10);
|
||||
|
||||
// проверка 2 сеанса у 1 кино
|
||||
Assertions.assertEquals(cinemaService
|
||||
.findCinema(cinema1.getId()).getSessions().size(), 2);
|
||||
@ -68,6 +66,9 @@ public class JpaCustomerTests {
|
||||
.addOrder(customerService.findCustomer(customer2.getId()).getId());
|
||||
// у заказа 2 сеанса
|
||||
orderService.addSession(order2.getId(), session1.getId(), 2);
|
||||
|
||||
List<Session> result = sessionService.findAllSessions();
|
||||
|
||||
Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2);
|
||||
|
||||
orderService.addSession(order2.getId(), session2.getId(), 5);
|
||||
|
Loading…
Reference in New Issue
Block a user