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 Menu from './components/catalogs/Menu';
import Basket from './components/catalogs/Basket';
import History from './components/catalogs/History';
import { useState } from 'react';
function Router(props) {
@ -17,7 +18,8 @@ export default function App() {
{ index: true, element: <CatalogStudents /> },
{ path: "catalogs/menu", element: <Menu product={product} setProduct={setProduct}/>, 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 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();
}
}
async function needWait(){
setOrder({...order, ["price"]:cost});
}
async function acceptOrder(){
await needWait();
await DataService.create("/order",order ).then(data => {
await DataService.create("/order",{...order, ["price"]:cost, ["status"]: "1"} ).then(data => {
props.setProduct([]);
setCost(0);
});

View File

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

View File

@ -1,8 +1,15 @@
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.web.servlet.config.annotation.CorsRegistry;
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
class WebConfiguration implements WebMvcConfigurer {
@ -10,4 +17,17 @@ class WebConfiguration implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry){
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;
import ip.labwork.shop.model.Order;
import ip.labwork.shop.model.OrderStatus;
import java.util.Date;
import java.util.List;
@ -10,6 +11,7 @@ public class OrderDTO {
private long id;
private Date date = new Date();
private int price;
private OrderStatus status = OrderStatus.Неизвестен;
private List<ProductDTO> productDTOList;
public OrderDTO(Order order) {
this.id = order.getId();
@ -19,6 +21,7 @@ public class OrderDTO {
.filter(x -> Objects.equals(x.getId().getOrderId(), order.getId()))
.map(y -> new ProductDTO(y.getProduct(), y.getCount()))
.toList();
this.status = Objects.equals(order.getStatus().toString(), "") ? OrderStatus.Неизвестен : order.getStatus();
}
public OrderDTO() {
@ -44,6 +47,14 @@ public class OrderDTO {
this.date = date;
}
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
public List<ProductDTO> getProductDTOList() {
return productDTOList;
}

View File

@ -22,13 +22,15 @@ public class Order {
private Integer price;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<OrderProducts> products;
private OrderStatus status;
public Order(){
}
public Order(Date date, Integer price) {
public Order(Date date, Integer price, OrderStatus status) {
this.date = date;
this.price = price;
this.status = status;
}
public Long getId() {
@ -74,6 +76,14 @@ public class Order {
this.products.remove(orderProducts);
}
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
@Override
public boolean equals(Object o) {
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++){
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);
orderRepository.save(order);
for (int i = 0; i < orderDTO.getProductDTOList().size(); i++) {