сделала кое-как как правильно делать не знаю

This commit is contained in:
dasha 2023-04-18 18:44:02 +04:00
parent 3aff5cb712
commit 63dcaa4d7a
11 changed files with 81 additions and 60 deletions

View File

@ -154,6 +154,7 @@ 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,39 +1,17 @@
import React, { useEffect, useState } from "react" import React from "react"
import CinemaDto from "../models/CinemaDto";
import Service from "../services/Service";
import MyButton from "./MyButton"; import MyButton from "./MyButton";
export default function OrderSessionItem(props) { 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 ( return (
<> <>
{load ?
<tr> <tr>
<td>{item.id}</td> <td>{props.item.session.id}</td>
<td>{item.price}</td> <td>{props.item.session.price}</td>
<td>{item.cinema.name}</td> <td>{props.item.session.cinema.name}</td>
<td>{date}</td> <td>{new Date(props.item.session.timestamp).toLocaleString('RU-ru')}</td>
<td>{props.item.count}</td> <td>{props.item.count}</td>
<td><MyButton value={props} /></td> <td><MyButton value={props} /></td>
</tr> : null} </tr>
</> </>
) )
} }

View File

@ -10,6 +10,7 @@ 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.maxCount - 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

@ -23,7 +23,7 @@ public class OrderDto {
if (order.getSessions() != null && order.getSessions().size() > 0) if (order.getSessions() != null && order.getSessions().size() > 0)
this.sessions = order.getSessions() this.sessions = order.getSessions()
.stream() .stream()
.map(x -> new OrderSessionDto(x.getId().getSessionId(), .map(x -> new OrderSessionDto(new SessionDto(x.getSession()),
x.getId().getOrderId(), x.getCount())).toList(); x.getId().getOrderId(), x.getCount())).toList();
} }

View File

@ -1,21 +1,21 @@
package com.labwork1.app.student.controller; package com.labwork1.app.student.controller;
public class OrderSessionDto { public class OrderSessionDto {
private Long sessionId; private SessionDto session;
private Long orderId; private Long orderId;
private Integer count; private Integer count;
public OrderSessionDto() { public OrderSessionDto() {
} }
public OrderSessionDto(Long sessionId, Long orderId, Integer count) { public OrderSessionDto(SessionDto session, Long orderId, Integer count) {
this.sessionId = sessionId; this.session = session;
this.orderId = orderId; this.orderId = orderId;
this.count = count; this.count = count;
} }
public Long getSessionId() { public SessionDto getSession() {
return sessionId; return session;
} }
public Long getOrderId() { public Long getOrderId() {

View File

@ -38,14 +38,16 @@ public class SessionController {
SimpleDateFormat format = new SimpleDateFormat(); SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyy-MM-dd-HH:ss"); format.applyPattern("yyyy-MM-dd-HH:ss");
Date docDate = format.parse(timestamp.replace('T', '-')); Date docDate = format.parse(timestamp.replace('T', '-'));
return new SessionDto(sessionService.addSession(Double.parseDouble(price), return new SessionDto(sessionService.findSession(
new Timestamp(docDate.getTime()), cinemaId, capacity)); sessionService.addSession(Double.parseDouble(price),
new Timestamp(docDate.getTime()), cinemaId, capacity).getId()));
} }
@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))); return new SessionDto(sessionService.findSession(sessionService
.updateSession(id, Double.parseDouble(price)).getId()));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -1,6 +1,5 @@
package com.labwork1.app.student.controller; package com.labwork1.app.student.controller;
import com.labwork1.app.student.model.Cinema;
import com.labwork1.app.student.model.Session; import com.labwork1.app.student.model.Session;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -10,7 +9,8 @@ public class SessionDto {
private Double price; private Double price;
private Timestamp timestamp; private Timestamp timestamp;
private CinemaDto cinema; private CinemaDto cinema;
private int capacity; private Long capacity;
private int maxCount; private int maxCount;
public SessionDto() { public SessionDto() {
@ -20,6 +20,7 @@ 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;
@ -29,14 +30,6 @@ public class SessionDto {
return maxCount; return maxCount;
} }
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public long getId() { public long getId() {
return id; return id;
} }
@ -49,6 +42,10 @@ public class SessionDto {
return timestamp; return timestamp;
} }
public Long getCapacity() {
return capacity;
}
public CinemaDto getCinema() { public CinemaDto getCinema() {
return cinema; return cinema;
} }

View File

@ -27,6 +27,8 @@ public class Session {
@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;
@Transient
private Long capacity;
public Session() { public Session() {
} }
@ -41,6 +43,16 @@ public class Session {
this.maxCount = maxCount; 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() { public Cinema getCinema() {
return cinema; return cinema;
} }
@ -83,6 +95,7 @@ public class Session {
"id=" + id + "id=" + id +
", price='" + price + '\'' + ", price='" + price + '\'' +
", timestamp='" + timestamp.toString() + '\'' + ", timestamp='" + timestamp.toString() + '\'' +
", capacity='" + capacity + '\'' +
", maxCount='" + maxCount.toString() + '\'' + ", maxCount='" + maxCount.toString() + '\'' +
", cinema='" + cinema.toString() + '\'' + ", cinema='" + cinema.toString() + '\'' +
'}'; '}';
@ -96,6 +109,18 @@ public class Session {
return price; 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) { public void setPrice(Double price) {
this.price = price; this.price = price;
} }
@ -111,4 +136,12 @@ public class Session {
public List<OrderSession> getOrders() { public List<OrderSession> getOrders() {
return orders; return orders;
} }
public Long getCapacity() {
return capacity;
}
public void setCapacity(Long capacity) {
this.capacity = capacity;
}
} }

View File

@ -5,9 +5,19 @@ 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; import java.util.Optional;
public interface SessionRepository extends JpaRepository<Session, Long> { public interface SessionRepository extends JpaRepository<Session, Long> {
@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")
Optional<Integer> getCapacity(@Param("sessionId") Long 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);
} }

View File

@ -8,9 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class SessionService { public class SessionService {
@ -35,13 +33,13 @@ public class SessionService {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Session findSession(Long id) { public Session findSession(Long id) {
final Optional<Session> session = sessionRepository.findById(id); return sessionRepository.getSessionWithCapacity(id)
return session.orElseThrow(() -> new SessionNotFoundException(id)); .orElseThrow(() -> new SessionNotFoundException(id));
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Session> findAllSessions() { public List<Session> findAllSessions() {
return sessionRepository.findAll(); return sessionRepository.getSessionsWithCapacity();
} }
@Transactional @Transactional

View File

@ -1,10 +1,7 @@
package com.labwork1.app; package com.labwork1.app;
import com.labwork1.app.student.model.Customer; import com.labwork1.app.student.model.*;
import com.labwork1.app.student.model.Order;
import com.labwork1.app.student.model.Session;
import com.labwork1.app.student.service.*; import com.labwork1.app.student.service.*;
import com.labwork1.app.student.model.Cinema;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -42,6 +39,7 @@ public class JpaCustomerTests {
new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10); new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10);
final Session session2 = sessionService.addSession( 200.0, final Session session2 = sessionService.addSession( 200.0,
new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10); new Timestamp(System.currentTimeMillis()), cinema1.getId(), 10);
// проверка 2 сеанса у 1 кино // проверка 2 сеанса у 1 кино
Assertions.assertEquals(cinemaService Assertions.assertEquals(cinemaService
.findCinema(cinema1.getId()).getSessions().size(), 2); .findCinema(cinema1.getId()).getSessions().size(), 2);
@ -68,6 +66,9 @@ 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);
List<Session> result = sessionService.findAllSessions();
Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2); Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2);
orderService.addSession(order2.getId(), session2.getId(), 5); orderService.addSession(order2.getId(), session2.getId(), 5);