Merge remote-tracking branch 'origin/LabWork04' into LabWork04

# Conflicts:
#	src/main/java/ip/labwork/shop/service/OrderService.java
This commit is contained in:
Nikita Sergeev 2023-04-21 14:29:20 +04:00
commit 52a842d8c9
10 changed files with 173 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import Footer from "./components/common/Footer";
import CatalogStudents from './components/catalogs/CatalogStudents'; import CatalogStudents from './components/catalogs/CatalogStudents';
import Menu from './components/catalogs/Menu'; import Menu from './components/catalogs/Menu';
import Basket from './components/catalogs/Basket'; import Basket from './components/catalogs/Basket';
import History from './components/catalogs/History';
import { useState } from 'react'; import { useState } from 'react';
function Router(props) { function Router(props) {
@ -17,7 +18,8 @@ export default function App() {
{ index: true, element: <CatalogStudents /> }, { index: true, element: <CatalogStudents /> },
{ path: "catalogs/menu", element: <Menu product={product} setProduct={setProduct}/>, label: "Меню" }, { path: "catalogs/menu", element: <Menu product={product} setProduct={setProduct}/>, label: "Меню" },
{ path: "catalogs/component", element: <CatalogStudents />, label: "Компоненты" }, { path: "catalogs/component", element: <CatalogStudents />, label: "Компоненты" },
{ path: "catalogs/basket", element: <Basket product={product} setProduct={setProduct}/>, label: "Корзина" } { path: "catalogs/basket", element: <Basket product={product} setProduct={setProduct}/>, label: "Корзина" },
{ path: "catalogs/history", element: <History />, label: "История" }
]; ];
const links = routes.filter(route => route.hasOwnProperty('label')); const links = routes.filter(route => route.hasOwnProperty('label'));
const rootRoute = [ const rootRoute = [

View File

@ -0,0 +1,66 @@
import { useState, useEffect } from "react";
import Table from "../common/Table";
import Modal from "../common/Modal";
import DataService from '../../services/DataService';
export default function CatalogHistory(props) {
const [items, setItems] = useState([]);
const [modalHeader, setModalHeader] = useState('');
const [modalConfirm, setModalConfirm] = useState('');
const [modalVisible, setModalVisible] = useState(false);
const [isEdit, setEdit] = useState(false);
useEffect(() => {
loadItems();
}, []);
function loadItems() {
DataService.readAll(props.getAllUrl, props.transformer)
.then(data => setItems(data));
}
function saveItem() {
if (!isEdit) {
DataService.create(props.getAllUrl, props.data).then(() => loadItems());
} else {
DataService.update(props.url + props.data.id, props.data).then(() => loadItems());
}
}
function edit(editedId) {
DataService.read(props.url + editedId, props.transformer)
.then(data => {
setEdit(true);
setModalHeader('Редактирование элемента');
setModalConfirm('Сохранить');
setModalVisible(true);
props.onEdit(data);
});
}
function handleModalHide() {
setModalVisible(false);
}
function handleModalDone() {
saveItem();
}
return (
<>
<Table
headers={props.headers}
items={items}
/>
<Modal
header={modalHeader}
confirm={modalConfirm}
visible={modalVisible}
onHide={handleModalHide}
onDone={handleModalDone}>
{props.children}
</Modal>
</>
);
}

View File

@ -0,0 +1,54 @@
import { useState, useEffect } from 'react';
import Catalog from './CatalogHistory';
import Component from '../../models/Component';
import DataService from '../../services/DataService';
import Order from '../../models/Order';
export default function CatalogStudents(props) {
const getAllUrl = '/order';
const url = '/order/';
const transformer = (data) => new Order(data);
const catalogStudHeaders = [
{ name: 'date', label: 'Дата оформления' },
{ name: 'price', label: 'Общая стоимость' },
{ name: 'status', label: 'Статус' }
];
const [data, setData] = useState(new Order());
function handleOnAdd() {
setData(new Order());
}
function handleOnEdit(data) {
setData(new Order(data));
}
function handleFormChange(event) {
setData({ ...data, [event.target.id]: event.target.value })
}
return (
<main className="flex-shrink-0" style={{ backgroundColor: "white" }}>
<Catalog
headers={catalogStudHeaders}
getAllUrl={getAllUrl}
url={url}
transformer={transformer}
data={data}
onAdd={handleOnAdd}
onEdit={handleOnEdit}>
<div className="mb-3">
<label htmlFor="componentName" className="form-label">Название компонента</label>
<input type="text" id="componentName" className="form-control" required
value={data.componentName} onChange={handleFormChange}/>
</div>
<div className="mb-3">
<label htmlFor="price" className="form-label">Цена</label>
<input type="text" id="price" className="form-control" required
value={data.price} onChange={handleFormChange}/>
</div>
</Catalog>
</main>
);
}

View File

@ -43,13 +43,8 @@ export default function TableOrder(props) {
loadItems(); loadItems();
} }
} }
async function needWait(){
setOrder({...order, ["price"]:cost});
}
async function acceptOrder(){ async function acceptOrder(){
await needWait(); await DataService.create("/order",{...order, ["price"]:cost, ["status"]: "1"} ).then(data => {
await DataService.create("/order",order ).then(data => {
props.setProduct([]); props.setProduct([]);
setCost(0); setCost(0);
}); });

View File

@ -4,5 +4,6 @@ export default class Order {
this.date = data?.date || ""; this.date = data?.date || "";
this.price = data?.price || 0; this.price = data?.price || 0;
this.productDTOList = data?.productDTOList || []; this.productDTOList = data?.productDTOList || [];
this.status = data?.status || "0";
} }
} }

View File

@ -1,8 +1,15 @@
package ip.labwork; package ip.labwork;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.http.HttpStatus;
@Configuration @Configuration
class WebConfiguration implements WebMvcConfigurer { class WebConfiguration implements WebMvcConfigurer {
@ -10,4 +17,17 @@ class WebConfiguration implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry){ public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**").allowedMethods("*"); registry.addMapping("/**").allowedMethods("*");
} }
@Override
public void addViewControllers(ViewControllerRegistry registry) {
ViewControllerRegistration registration = registry.addViewController("/notFound");
registration.setViewName("forward:/index.html");
registration.setStatusCode(HttpStatus.OK);
}
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
return container -> {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound"));
};
}
} }

View File

@ -1,6 +1,7 @@
package ip.labwork.shop.controller; package ip.labwork.shop.controller;
import ip.labwork.shop.model.Order; import ip.labwork.shop.model.Order;
import ip.labwork.shop.model.OrderStatus;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -10,6 +11,7 @@ public class OrderDTO {
private long id; private long id;
private Date date = new Date(); private Date date = new Date();
private int price; private int price;
private OrderStatus status = OrderStatus.Неизвестен;
private List<ProductDTO> productDTOList; private List<ProductDTO> productDTOList;
public OrderDTO(Order order) { public OrderDTO(Order order) {
this.id = order.getId(); this.id = order.getId();
@ -19,6 +21,7 @@ public class OrderDTO {
.filter(x -> Objects.equals(x.getId().getOrderId(), order.getId())) .filter(x -> Objects.equals(x.getId().getOrderId(), order.getId()))
.map(y -> new ProductDTO(y.getProduct(), y.getCount())) .map(y -> new ProductDTO(y.getProduct(), y.getCount()))
.toList(); .toList();
this.status = Objects.equals(order.getStatus().toString(), "") ? OrderStatus.Неизвестен : order.getStatus();
} }
public OrderDTO() { public OrderDTO() {
@ -44,6 +47,14 @@ public class OrderDTO {
this.date = date; this.date = date;
} }
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
public List<ProductDTO> getProductDTOList() { public List<ProductDTO> getProductDTOList() {
return productDTOList; return productDTOList;
} }

View File

@ -22,13 +22,15 @@ public class Order {
private Integer price; private Integer price;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<OrderProducts> products; private List<OrderProducts> products;
private OrderStatus status;
public Order(){ public Order(){
} }
public Order(Date date, Integer price) { public Order(Date date, Integer price, OrderStatus status) {
this.date = date; this.date = date;
this.price = price; this.price = price;
this.status = status;
} }
public Long getId() { public Long getId() {
@ -74,6 +76,14 @@ public class Order {
this.products.remove(orderProducts); this.products.remove(orderProducts);
} }
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -0,0 +1,5 @@
package ip.labwork.shop.model;
public enum OrderStatus {
Неизвестен, Готов
}

View File

@ -28,7 +28,7 @@ public class OrderService {
for(int i = 0; i < orderDTO.getProductDTOList().size(); i++){ for(int i = 0; i < orderDTO.getProductDTOList().size(); i++){
price += orderDTO.getProductDTOList().get(i).getPrice() * orderDTO.getProductDTOList().get(i).getCount(); price += orderDTO.getProductDTOList().get(i).getPrice() * orderDTO.getProductDTOList().get(i).getCount();
} }
final Order order = new Order(new Date(), price); final Order order = new Order(new Date(), price, orderDTO.getStatus());
validatorUtil.validate(order); validatorUtil.validate(order);
orderRepository.save(order); orderRepository.save(order);
for (int i = 0; i < orderDTO.getProductDTOList().size(); i++) { for (int i = 0; i < orderDTO.getProductDTOList().size(); i++) {