From 63dcaa4d7a42cfc9028dd2fc0039156fcc3a307c Mon Sep 17 00:00:00 2001 From: dasha Date: Tue, 18 Apr 2023 18:44:02 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B5-=D0=BA=D0=B0=D0=BA=20=D0=BA=D0=B0=D0=BA=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=BD=D0=B5=20=D0=B7=D0=BD?= =?UTF-8?q?=D0=B0=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/pages/Sessions.jsx | 1 + .../src/pages/components/OrderSessionItem.jsx | 40 +++++-------------- front/src/pages/components/SessionItem.jsx | 3 +- .../app/student/controller/OrderDto.java | 2 +- .../student/controller/OrderSessionDto.java | 10 ++--- .../student/controller/SessionController.java | 8 ++-- .../app/student/controller/SessionDto.java | 17 ++++---- .../labwork1/app/student/model/Session.java | 33 +++++++++++++++ .../student/repository/SessionRepository.java | 10 +++++ .../app/student/service/SessionService.java | 8 ++-- .../com/labwork1/app/JpaCustomerTests.java | 9 +++-- 11 files changed, 81 insertions(+), 60 deletions(-) diff --git a/front/src/pages/Sessions.jsx b/front/src/pages/Sessions.jsx index b9d0ae5..20986cc 100644 --- a/front/src/pages/Sessions.jsx +++ b/front/src/pages/Sessions.jsx @@ -154,6 +154,7 @@ export default function Sessions() { Price Cinema Timestamp + Capacity MaxCount diff --git a/front/src/pages/components/OrderSessionItem.jsx b/front/src/pages/components/OrderSessionItem.jsx index 26f9af4..a283d93 100644 --- a/front/src/pages/components/OrderSessionItem.jsx +++ b/front/src/pages/components/OrderSessionItem.jsx @@ -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 ? - - {item.id} - {item.price} - {item.cinema.name} - {date} - {props.item.count} - - : null} + + {props.item.session.id} + {props.item.session.price} + {props.item.session.cinema.name} + {new Date(props.item.session.timestamp).toLocaleString('RU-ru')} + {props.item.count} + + ) } \ No newline at end of file diff --git a/front/src/pages/components/SessionItem.jsx b/front/src/pages/components/SessionItem.jsx index 0dc0062..4d865da 100644 --- a/front/src/pages/components/SessionItem.jsx +++ b/front/src/pages/components/SessionItem.jsx @@ -3,13 +3,14 @@ import MyButton from './MyButton' export default function SessionItem(props) { const date = new Date(props.item.timestamp).toLocaleString('RU-ru') - + return ( {props.item.id} {props.item.price} {props.item.cinema.name} {date} + {props.item.maxCount - props.item.capacity} {props.item.maxCount} diff --git a/src/main/java/com/labwork1/app/student/controller/OrderDto.java b/src/main/java/com/labwork1/app/student/controller/OrderDto.java index 74727c0..775fae0 100644 --- a/src/main/java/com/labwork1/app/student/controller/OrderDto.java +++ b/src/main/java/com/labwork1/app/student/controller/OrderDto.java @@ -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(); } diff --git a/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java b/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java index 843de7c..8aa8234 100644 --- a/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java +++ b/src/main/java/com/labwork1/app/student/controller/OrderSessionDto.java @@ -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() { diff --git a/src/main/java/com/labwork1/app/student/controller/SessionController.java b/src/main/java/com/labwork1/app/student/controller/SessionController.java index 12eea04..9d04b6e 100644 --- a/src/main/java/com/labwork1/app/student/controller/SessionController.java +++ b/src/main/java/com/labwork1/app/student/controller/SessionController.java @@ -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}") diff --git a/src/main/java/com/labwork1/app/student/controller/SessionDto.java b/src/main/java/com/labwork1/app/student/controller/SessionDto.java index dd0dd01..aa91f6a 100644 --- a/src/main/java/com/labwork1/app/student/controller/SessionDto.java +++ b/src/main/java/com/labwork1/app/student/controller/SessionDto.java @@ -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; } diff --git a/src/main/java/com/labwork1/app/student/model/Session.java b/src/main/java/com/labwork1/app/student/model/Session.java index 0749010..b7986ed 100644 --- a/src/main/java/com/labwork1/app/student/model/Session.java +++ b/src/main/java/com/labwork1/app/student/model/Session.java @@ -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 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 getOrders() { return orders; } + + public Long getCapacity() { + return capacity; + } + + public void setCapacity(Long capacity) { + this.capacity = capacity; + } } diff --git a/src/main/java/com/labwork1/app/student/repository/SessionRepository.java b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java index 85ba4c4..ba55e8f 100644 --- a/src/main/java/com/labwork1/app/student/repository/SessionRepository.java +++ b/src/main/java/com/labwork1/app/student/repository/SessionRepository.java @@ -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 { @Query("Select sum(os.count) from OrderSession os where os.session.id = :sessionId") Optional 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 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 getSessionWithCapacity(@Param("sessionId") Long sessionId); } diff --git a/src/main/java/com/labwork1/app/student/service/SessionService.java b/src/main/java/com/labwork1/app/student/service/SessionService.java index c1c56dd..30bffbe 100644 --- a/src/main/java/com/labwork1/app/student/service/SessionService.java +++ b/src/main/java/com/labwork1/app/student/service/SessionService.java @@ -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 = sessionRepository.findById(id); - return session.orElseThrow(() -> new SessionNotFoundException(id)); + return sessionRepository.getSessionWithCapacity(id) + .orElseThrow(() -> new SessionNotFoundException(id)); } @Transactional(readOnly = true) public List findAllSessions() { - return sessionRepository.findAll(); + return sessionRepository.getSessionsWithCapacity(); } @Transactional diff --git a/src/test/java/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java index 28da02c..15a127a 100644 --- a/src/test/java/com/labwork1/app/JpaCustomerTests.java +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -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 result = sessionService.findAllSessions(); + Assertions.assertEquals(sessionService.getCapacity(session1.getId()), 2); orderService.addSession(order2.getId(), session2.getId(), 5);