Вроде бы сделал...
This commit is contained in:
parent
2d6c7a0029
commit
4a9ed45050
@ -1,10 +1,10 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'org.springframework.boot' version '3.0.2'
|
id 'org.springframework.boot' version '2.7.8'
|
||||||
id 'io.spring.dependency-management' version '1.1.0'
|
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'com.example'
|
group = 'ru.ulsru.is'
|
||||||
version = '0.0.1-SNAPSHOT'
|
version = '0.0.1-SNAPSHOT'
|
||||||
sourceCompatibility = '17'
|
sourceCompatibility = '17'
|
||||||
|
|
||||||
@ -14,10 +14,9 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'com.h2database:h2:2.1.210'
|
implementation 'com.h2database:h2:2.1.210'
|
||||||
|
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
31166
front/package-lock.json
generated
31166
front/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,48 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "pages_react",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"private": true,
|
|
||||||
"dependencies": {
|
|
||||||
"@fortawesome/fontawesome-free": "^6.2.1",
|
|
||||||
"axios": "^1.1.3",
|
|
||||||
"bootstrap": "^5.2.3",
|
|
||||||
"react": "^18.2.0",
|
|
||||||
"react-bootstrap": "^2.7.2",
|
|
||||||
"react-dom": "^18.2.0",
|
|
||||||
"react-router-dom": "^6.6.1",
|
|
||||||
"react-scripts": "5.0.1",
|
|
||||||
"web-vitals": "^2.1.4"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/react": "^18.0.24",
|
|
||||||
"@types/react-dom": "^18.0.8",
|
|
||||||
"@vitejs/plugin-react": "^2.2.0",
|
|
||||||
"json-server": "^0.17.1",
|
|
||||||
"npm-run-all": "^4.1.5",
|
|
||||||
"vite": "^3.2.3"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"start": "react-scripts start",
|
|
||||||
"build": "react-scripts build",
|
|
||||||
"test": "react-scripts test",
|
|
||||||
"eject": "react-scripts eject"
|
|
||||||
},
|
|
||||||
"eslintConfig": {
|
|
||||||
"extends": [
|
|
||||||
"react-app",
|
|
||||||
"react-app/jest"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"browserslist": {
|
|
||||||
"production": [
|
|
||||||
">0.2%",
|
|
||||||
"not dead",
|
|
||||||
"not op_mini all"
|
|
||||||
],
|
|
||||||
"development": [
|
|
||||||
"last 1 chrome version",
|
|
||||||
"last 1 firefox version",
|
|
||||||
"last 1 safari version"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
|
|
||||||
<link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
|
|
||||||
<link rel="stylesheet" href="/node_modules/@fortawesome/fontawesome-free/css/all.min.css">
|
|
||||||
<title>Компании, сотрудники и должности</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
|
||||||
<div id="root"></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -1,44 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
import { useRoutes, Outlet, BrowserRouter } from 'react-router-dom';
|
|
||||||
import Cars from './components/Cars.jsx';
|
|
||||||
import Header from "./components/commons/Header.jsx"
|
|
||||||
import 'bootstrap/dist/css/bootstrap.min.css';
|
|
||||||
import Buyers from './components/Buyers.jsx';
|
|
||||||
import Stores from './components/Stores.jsx';
|
|
||||||
import OneBuyer from './components/OneBuyer.jsx';
|
|
||||||
function Router(props) {
|
|
||||||
return useRoutes(props.rootRoute);
|
|
||||||
}
|
|
||||||
|
|
||||||
function App() {
|
|
||||||
const routes = [
|
|
||||||
{ index: true, element: <Buyers /> },
|
|
||||||
{ path: '/', element: <Buyers />},
|
|
||||||
{ path: '/cars', element: <Cars />, label: 'Машины' },
|
|
||||||
{ path: '/buyers', element: <Buyers />, label: 'Покупатель' },
|
|
||||||
{ path: '/stores', element: <Stores />, label: 'Магазины' },
|
|
||||||
{ path: '/buyer/:id', element: <OneBuyer />},
|
|
||||||
];
|
|
||||||
const links = routes.filter(route => route.hasOwnProperty('label'));
|
|
||||||
const rootRoute = [
|
|
||||||
{ path: '/', element: render(links), children: routes }
|
|
||||||
];
|
|
||||||
function render(links) {
|
|
||||||
return (
|
|
||||||
<div className="App">
|
|
||||||
<Header links={links} />
|
|
||||||
<div className="w-100">
|
|
||||||
<Outlet />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<BrowserRouter>
|
|
||||||
<Router rootRoute={ rootRoute } />
|
|
||||||
</BrowserRouter>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App;
|
|
@ -1,155 +0,0 @@
|
|||||||
import Form from 'react-bootstrap/Form';
|
|
||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
import { useState, useEffect } from 'react';
|
|
||||||
import DataService from '../services/DataService';
|
|
||||||
import Catalog from "./Catalog.jsx";
|
|
||||||
import Buyer from "../models/Buyer";
|
|
||||||
import ModalForm from './commons/ModalForm';
|
|
||||||
import Car from '../models/Car';
|
|
||||||
import Store from '../models/Store';
|
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
|
|
||||||
|
|
||||||
export default function Companies(props) {
|
|
||||||
const headers = [
|
|
||||||
{name: 'name', label: "Название"},
|
|
||||||
{name: 'countCars', label: "Машины"},
|
|
||||||
];
|
|
||||||
const nameCatalog = "Покупатели";
|
|
||||||
|
|
||||||
const url = '/buyer';
|
|
||||||
const requestParams = '?name=nameData';
|
|
||||||
|
|
||||||
const [items, setItems] = useState([]);
|
|
||||||
|
|
||||||
|
|
||||||
const [data, setData] = useState(new Buyer());
|
|
||||||
const [isEditing, setEditing] = useState(false);
|
|
||||||
useEffect(() => {
|
|
||||||
loadItems();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
function loadItems() {
|
|
||||||
DataService.readAll(url, (data) => new Buyer(data))
|
|
||||||
.then(data => setItems(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function handleAdd() {
|
|
||||||
DataService.create(url +requestParams
|
|
||||||
.replace("nameData", data.name))
|
|
||||||
.then(() => loadItems());
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleEdit(editedId) {
|
|
||||||
DataService.read(url + "/" + editedId, (e) => new Buyer(e))
|
|
||||||
.then(data => {
|
|
||||||
setData(new Buyer(data));
|
|
||||||
});
|
|
||||||
setEditing(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
function handleEditIsDone() {
|
|
||||||
|
|
||||||
DataService.update(url + "/" + data.id + requestParams
|
|
||||||
.replace("nameData", data.name))
|
|
||||||
.then(() => loadItems());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleDelete(item) {
|
|
||||||
if (window.confirm('Удалить выбранный элемент?')) {
|
|
||||||
const promises = [];
|
|
||||||
promises.push(DataService.delete(url + "/" + item));
|
|
||||||
Promise.all(promises).then(() => {
|
|
||||||
loadItems();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// при каждом изменении поля формы происходит вызов этого метода, обновляя данные объекта
|
|
||||||
function handleFormChange(event) {
|
|
||||||
setData({ ...data, [event.target.name]: event.target.value });
|
|
||||||
}
|
|
||||||
|
|
||||||
// определяет действия формы по нажатию Отправить
|
|
||||||
function submitForm() {
|
|
||||||
if (!isEditing) {
|
|
||||||
// если добавление элемента
|
|
||||||
handleAdd();
|
|
||||||
} else {
|
|
||||||
// если редактирование элемента;
|
|
||||||
handleEditIsDone();
|
|
||||||
setEditing(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// вызывается при закрытии модального окна или по нажатию кнопки Добавить и очищает данные объекта
|
|
||||||
function reset() {
|
|
||||||
setData(new Buyer());
|
|
||||||
setEditing(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// для каждого типа сущности своя форма,
|
|
||||||
// которая передается дальше в абстрактный компонент Catalog в качестве props.form
|
|
||||||
const form = <Form onSubmit={submitForm}>
|
|
||||||
<Form.Group className="mb-3" controlId="name">
|
|
||||||
<Form.Label>Название</Form.Label>
|
|
||||||
<Form.Control name="name" value={data.name} type="input" placeholder="Enter text" onChange={handleFormChange} required/>
|
|
||||||
</Form.Group>
|
|
||||||
<Button variant="info" type="submit">
|
|
||||||
Отправить
|
|
||||||
</Button>
|
|
||||||
</Form>;
|
|
||||||
|
|
||||||
|
|
||||||
const [showModalForm, setShowChoosing] = useState(false);
|
|
||||||
const formChooseBuyer = <Form onSubmit={redirectToBuyer}>
|
|
||||||
<Form.Group className="mb-3" controlId="name">
|
|
||||||
<Form.Label>Покупатель</Form.Label>
|
|
||||||
<Form.Select name="name_select" type="input" onChange={(e) => {setChosenBuyer(e.target.value)}} required>
|
|
||||||
<option selected disabled>Выберите покупателя</option>
|
|
||||||
{
|
|
||||||
items.map((buyer) => <option key={`buyer_${buyer.id}`} value={`${buyer.id}`}>{`${buyer.name}`}</option>)
|
|
||||||
}
|
|
||||||
</Form.Select>
|
|
||||||
</Form.Group>
|
|
||||||
<Button variant="info" type="submit">
|
|
||||||
Перейти
|
|
||||||
</Button>
|
|
||||||
</Form>;
|
|
||||||
|
|
||||||
function showModalFormChoosing() {
|
|
||||||
setShowChoosing(true);
|
|
||||||
}
|
|
||||||
function unshowModalFormChoosing() {
|
|
||||||
setShowChoosing(false);
|
|
||||||
}
|
|
||||||
const [chosenBuyer, setChosenBuyer] = useState(0);
|
|
||||||
const navigate = useNavigate();
|
|
||||||
function redirectToBuyer() {
|
|
||||||
|
|
||||||
if (chosenBuyer !== 0) {
|
|
||||||
navigate(`/buyer/${chosenBuyer}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return <div className="container-lg pt-5 min-vh-100">
|
|
||||||
<Button onClick={showModalFormChoosing} variant="info">Перейти к покупателю</Button>
|
|
||||||
<ModalForm show={showModalForm} onClose={unshowModalFormChoosing} modalTitle={"Выбор покупателя"} form={formChooseBuyer}></ModalForm>
|
|
||||||
<Catalog name={nameCatalog}
|
|
||||||
headers={headers}
|
|
||||||
items={items}
|
|
||||||
onAdd={handleAdd}
|
|
||||||
onEdit={handleEdit}
|
|
||||||
onDelete={handleDelete}
|
|
||||||
onClose={reset}
|
|
||||||
onBtnAdd={reset}
|
|
||||||
form={form}>
|
|
||||||
</Catalog>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
import Car from "../models/Car";
|
|
||||||
import Form from 'react-bootstrap/Form';
|
|
||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
import { useState, useEffect } from 'react';
|
|
||||||
import DataService from '../services/DataService';
|
|
||||||
import Catalog from "./Catalog.jsx";
|
|
||||||
export default function Cars(props) {
|
|
||||||
const headers = [
|
|
||||||
{name: 'surname', label: "Модель"},
|
|
||||||
{name: 'name', label: "Марка"},
|
|
||||||
{name: 'price', label: "Цена"},
|
|
||||||
{name: 'storesString', label: 'Магазин'}
|
|
||||||
];
|
|
||||||
const nameCatalog = "Машины";
|
|
||||||
const [items, setItems] = useState([]);
|
|
||||||
const url = '/car';
|
|
||||||
const requestParams = '?name=nameData&surname=surnameData&price=phoneNameData';
|
|
||||||
const [data, setData] = useState(new Car());
|
|
||||||
const [isEditing, setEditing] = useState(false);
|
|
||||||
useEffect(() => {
|
|
||||||
loadItems();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
|
|
||||||
function loadItems() {
|
|
||||||
DataService.readAll(url, (data) => new Car(data))
|
|
||||||
.then(data => setItems(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleAdd() {
|
|
||||||
DataService.create(url +requestParams
|
|
||||||
.replace("nameData", data.name)
|
|
||||||
.replace("surnameData", data.surname)
|
|
||||||
.replace("phoneNameData", data.price))
|
|
||||||
.then(() => loadItems());
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleEdit(editedId) {
|
|
||||||
DataService.read(url + "/" + editedId, (e) => new Car(e))
|
|
||||||
.then(data => {
|
|
||||||
setData(new Car(data));
|
|
||||||
});
|
|
||||||
setEditing(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
function handleEditIsDone() {
|
|
||||||
|
|
||||||
DataService.update(url + "/" + data.id + requestParams
|
|
||||||
.replace("nameData", data.name)
|
|
||||||
.replace("surnameData", data.surname)
|
|
||||||
.replace("phoneNameData", data.price)).then(() => loadItems());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleDelete(item) {
|
|
||||||
if (window.confirm('Удалить выбранный элемент?')) {
|
|
||||||
const promises = [];
|
|
||||||
promises.push(DataService.delete(url + "/" + item));
|
|
||||||
Promise.all(promises).then(() => {
|
|
||||||
loadItems();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// при каждом изменении поля формы происходит вызов этого метода, обновляя данные объекта
|
|
||||||
function handleFormChange(event) {
|
|
||||||
setData({ ...data, [event.target.name]: event.target.value });
|
|
||||||
}
|
|
||||||
|
|
||||||
// определяет действия формы по нажатию Отправить
|
|
||||||
function submitForm() {
|
|
||||||
if (!isEditing) {
|
|
||||||
// если добавление элемента
|
|
||||||
handleAdd();
|
|
||||||
} else {
|
|
||||||
// если редактирование элемента;
|
|
||||||
handleEditIsDone();
|
|
||||||
setEditing(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// вызывается при закрытии модального окна или по нажатию кнопки Добавить и очищает данные объекта
|
|
||||||
function reset() {
|
|
||||||
setData(new Car());
|
|
||||||
setEditing(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// для каждого типа сущности своя форма,
|
|
||||||
// которая передается дальше в абстрактный компонент Catalog в качестве props.form
|
|
||||||
const form = <Form onSubmit={submitForm}>
|
|
||||||
<Form.Group className="mb-3" controlId="name">
|
|
||||||
<Form.Label>Модель</Form.Label>
|
|
||||||
<Form.Control name="surname" value={data.surname} type="input" placeholder="Enter text" onChange={handleFormChange} required/>
|
|
||||||
<Form.Label>Марка</Form.Label>
|
|
||||||
<Form.Control name="name" value={data.name} type="input" placeholder="Enter text" onChange={handleFormChange} required/>
|
|
||||||
<Form.Label>Цена</Form.Label>
|
|
||||||
<Form.Control name="price" value={data.price} type="input" placeholder="Enter text" onChange={handleFormChange} required/>
|
|
||||||
</Form.Group>
|
|
||||||
<Button variant="info" type="submit">
|
|
||||||
Отправить
|
|
||||||
</Button>
|
|
||||||
</Form>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return <div className="container-lg pt-5 min-vh-100">
|
|
||||||
<Catalog name={nameCatalog}
|
|
||||||
headers={headers}
|
|
||||||
items={items}
|
|
||||||
onAdd={handleAdd}
|
|
||||||
onEdit={handleEdit}
|
|
||||||
onDelete={handleDelete}
|
|
||||||
onClose={reset}
|
|
||||||
onBtnAdd={reset}
|
|
||||||
form={form}>
|
|
||||||
</Catalog>
|
|
||||||
</div>
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
import Table from "./commons/Table.jsx";
|
|
||||||
import { useState} from 'react';
|
|
||||||
import ModalForm from './commons/ModalForm.jsx';
|
|
||||||
// это абстрактный компонент для всех справочников
|
|
||||||
export default function Catalog(props) {
|
|
||||||
const [show, setShow] = useState(false);
|
|
||||||
const [modalTitle, setModalTitle] = useState("");
|
|
||||||
|
|
||||||
const handleClose = () => setShow(false);
|
|
||||||
const handleShow = () => setShow(true);
|
|
||||||
|
|
||||||
|
|
||||||
function handleAdd() {
|
|
||||||
setModalTitle("Добавление");
|
|
||||||
props.onBtnAdd();
|
|
||||||
handleShow();
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleEdit(itemId) {
|
|
||||||
setModalTitle("Редактирование");
|
|
||||||
props.onEdit(itemId);
|
|
||||||
handleShow();
|
|
||||||
}
|
|
||||||
function handleRemove(item) {
|
|
||||||
props.onDelete(item);
|
|
||||||
}
|
|
||||||
function changeData(event) {
|
|
||||||
props.onFormChanged(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
return <>
|
|
||||||
<div>{props.name}</div>
|
|
||||||
<Button variant="info" onClick={handleAdd}>Добавить</Button>
|
|
||||||
<Table
|
|
||||||
headers={props.headers}
|
|
||||||
items={props.items}
|
|
||||||
onEdit={handleEdit}
|
|
||||||
onDelete={handleRemove}
|
|
||||||
/>
|
|
||||||
<ModalForm
|
|
||||||
show={show}
|
|
||||||
onClose={handleClose}
|
|
||||||
modalTitle={modalTitle}
|
|
||||||
// onSubmit={submitForm}
|
|
||||||
onChange={changeData}
|
|
||||||
form={props.form}
|
|
||||||
/></>
|
|
||||||
|
|
||||||
}
|
|
@ -1,246 +0,0 @@
|
|||||||
import Buyer from "../models/Buyer";
|
|
||||||
import { useState, useEffect } from 'react';
|
|
||||||
import { Link, useParams } from "react-router-dom";
|
|
||||||
import DataService from '../services/DataService';
|
|
||||||
import Car from '../models/Car';
|
|
||||||
import Store from '../models/Store';
|
|
||||||
import Form from 'react-bootstrap/Form';
|
|
||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
import ModalForm from './commons/ModalForm';
|
|
||||||
import styles from "./OneBuyer.module.css";
|
|
||||||
|
|
||||||
export default function OneBuyer(props) {
|
|
||||||
const { id } = useParams();
|
|
||||||
|
|
||||||
const url = '/buyer/id';
|
|
||||||
const urlBuyerEmp = '/buyer/id/cars';
|
|
||||||
const urlEmp = '/car/free';
|
|
||||||
const urlPos = '/store';
|
|
||||||
const urlHire = '/buyer/id/hire';
|
|
||||||
const urlDismiss = '/buyer/id/dismiss';
|
|
||||||
const urlAddPos = '/car/id/addPos';
|
|
||||||
const urlDelPos = '/car/id/delPos';
|
|
||||||
const requestParamsStore = '?store=posId';
|
|
||||||
const requestParamsHire = '?carId=empId';
|
|
||||||
|
|
||||||
const [buyer, setBuyer] = useState(new Buyer());
|
|
||||||
const [itemsEmpFree, setItemsEmpFree] = useState([]);
|
|
||||||
const [itemsEmpBuyer, setItemsEmpBuyer] = useState([]);
|
|
||||||
const [itemsPos, setItemsPos] = useState([]);
|
|
||||||
|
|
||||||
const headersEmp = [
|
|
||||||
{name: 'surname', label: "Фамилия"},
|
|
||||||
{name: 'name', label: "Имя"},
|
|
||||||
{name: 'storesString', label: 'Магазин'},
|
|
||||||
{name: 'price', label: "Цена"}
|
|
||||||
];
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
loadBuyer();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
function loadBuyer() {
|
|
||||||
DataService.read(url.replace("id", id), (data) => new Buyer(data)).then((data) => setBuyer(new Buyer(data)));
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadItemsCars() {
|
|
||||||
DataService.readAll(urlEmp, (data) => new Car(data))
|
|
||||||
.then(data => setItemsEmpFree(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadItemsStores() {
|
|
||||||
DataService.readAll(urlPos, (data) => new Store(data))
|
|
||||||
.then(data => setItemsPos(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadItemsCarsBuyer() {
|
|
||||||
DataService.readAll(urlBuyerEmp.replace("id", buyer.id), (data) => new Car(data))
|
|
||||||
.then(data => setItemsEmpBuyer(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// логика для найма или увольнения
|
|
||||||
const [isShowHire, setShowHire] = useState(false);
|
|
||||||
|
|
||||||
function showModalFormHire() {
|
|
||||||
loadItemsCars();
|
|
||||||
setShowHire(true);
|
|
||||||
}
|
|
||||||
function unshowModalFormHire() {
|
|
||||||
setShowHire(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [isShowDismiss, setShowDismiss] = useState(false);
|
|
||||||
|
|
||||||
function showModalFormDismiss(e) {
|
|
||||||
loadItemsCarsBuyer();
|
|
||||||
setShowDismiss(true);
|
|
||||||
}
|
|
||||||
function unshowModalFormDismiss() {
|
|
||||||
setShowDismiss(false);
|
|
||||||
}
|
|
||||||
const [car, setCar] = useState(new Car());
|
|
||||||
|
|
||||||
function carChosenFree(e) {
|
|
||||||
setCar(itemsEmpFree.filter((emp) => emp.id == e.target.value)[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function carChosenBusy(e) {
|
|
||||||
setCar(Array.from(buyer.cars).filter((emp) => emp.id == e.target.value)[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function formHireSubmit() {
|
|
||||||
DataService.update(urlHire.replace("id",buyer.id) + requestParamsHire
|
|
||||||
.replace("empId", car.id))
|
|
||||||
.then(() => loadBuyer());
|
|
||||||
|
|
||||||
|
|
||||||
setCar(new Car());
|
|
||||||
}
|
|
||||||
|
|
||||||
function formDismissSubmit() {
|
|
||||||
DataService.update(urlDismiss.replace("id",buyer.id) + requestParamsHire
|
|
||||||
.replace("empId", car.id))
|
|
||||||
.then(() => loadBuyer());
|
|
||||||
|
|
||||||
setCar(new Car());
|
|
||||||
}
|
|
||||||
|
|
||||||
const [isShowChooseStore, setShowChooseStore] = useState(false);
|
|
||||||
|
|
||||||
function showModalFormChooseStore() {
|
|
||||||
loadItemsStores();
|
|
||||||
loadItemsCarsBuyer();
|
|
||||||
setShowChooseStore(true);
|
|
||||||
}
|
|
||||||
function unshowModalFormChooseStore() {
|
|
||||||
setShowChooseStore(false);
|
|
||||||
setStoreEmp([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [storesChosen, setStoreEmp] = useState([]);
|
|
||||||
function checkBoxChanged(e) {
|
|
||||||
// если чекбокс был выбран, то дабавляем в массив магазин
|
|
||||||
if (e.target.checked)
|
|
||||||
{
|
|
||||||
storesChosen.push(e.target.value);
|
|
||||||
} // если чекбокс был убран, то исключаем его значение из массива
|
|
||||||
else
|
|
||||||
{
|
|
||||||
let i = storesChosen.indexOf(e.target.value);
|
|
||||||
if(i >= 0) {
|
|
||||||
storesChosen.splice(i,1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function formChooseStoresSubmit() {
|
|
||||||
for (let i = 0; i < storesChosen.length; i++) {
|
|
||||||
DataService.update(urlAddPos.replace("id",car.id) + requestParamsStore
|
|
||||||
.replace("posId", storesChosen[i]))
|
|
||||||
.then(() => loadBuyer());
|
|
||||||
}
|
|
||||||
|
|
||||||
let storesEmp = car.stores;
|
|
||||||
|
|
||||||
for (let i = 0; i < storesEmp.length; i++) {
|
|
||||||
if (storesChosen.indexOf(''+storesEmp[i].id) == -1) {
|
|
||||||
// удаление магазина
|
|
||||||
DataService.update(urlDelPos.replace("id",car.id) + requestParamsStore
|
|
||||||
.replace("posId", storesEmp[i].id))
|
|
||||||
.then(() => loadBuyer());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const formHire = <Form onSubmit={formHireSubmit}>
|
|
||||||
<Form.Group className="mb-3" controlId="car">
|
|
||||||
<Form.Label>Машина</Form.Label>
|
|
||||||
<Form.Select name="car_select" type="input" onChange={carChosenFree} required>
|
|
||||||
<option selected disabled>Выберите машину</option>
|
|
||||||
{
|
|
||||||
itemsEmpFree.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
|
|
||||||
}
|
|
||||||
</Form.Select>
|
|
||||||
</Form.Group>
|
|
||||||
<Button class="btn btn-outline-info" type="submit">
|
|
||||||
Отправить
|
|
||||||
</Button>
|
|
||||||
</Form>;
|
|
||||||
|
|
||||||
|
|
||||||
const formDismiss = <Form onSubmit={formDismissSubmit}>
|
|
||||||
<Form.Group className="mb-3" controlId="car">
|
|
||||||
<Form.Label>Машина</Form.Label>
|
|
||||||
<Form.Select name="car_select" type="input" onChange={carChosenBusy} required>
|
|
||||||
<option selected disabled>Выберите машину</option>
|
|
||||||
{
|
|
||||||
itemsEmpBuyer.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
|
|
||||||
}
|
|
||||||
</Form.Select>
|
|
||||||
</Form.Group>
|
|
||||||
<Button class="btn btn-outline-info" type="submit">
|
|
||||||
Отправить
|
|
||||||
</Button>
|
|
||||||
</Form>;
|
|
||||||
|
|
||||||
const formCheckBoxesStore = <Form onSubmit={formChooseStoresSubmit}>
|
|
||||||
<Form.Group className="mb-3" controlId="car">
|
|
||||||
<Form.Label>Машина</Form.Label>
|
|
||||||
<Form.Select name="car_select" type="input" onChange={carChosenBusy} required>
|
|
||||||
<option selected disabled>Выберите машину</option>
|
|
||||||
{
|
|
||||||
itemsEmpBuyer.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
|
|
||||||
}
|
|
||||||
</Form.Select>
|
|
||||||
</Form.Group>
|
|
||||||
<Form.Group className="mb-3" controlId="store">
|
|
||||||
<Form.Label>Магазин</Form.Label>
|
|
||||||
<div className={`${styles.prokrutka}`}>
|
|
||||||
{
|
|
||||||
itemsPos.map((p) => <div style={{width: `150 px`}}>
|
|
||||||
<input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>
|
|
||||||
{/* {!carHasStore(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>}
|
|
||||||
{carHasStore(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} checked onChange={checkBoxChanged}/>} */}
|
|
||||||
{`${p.name}`}
|
|
||||||
</div>)
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
</Form.Group>
|
|
||||||
<Button class="btn btn-outline-info" type="submit">
|
|
||||||
Отправить
|
|
||||||
</Button>
|
|
||||||
</Form>
|
|
||||||
|
|
||||||
return <div className="container-lg pt-5 min-vh-100">
|
|
||||||
<h1>Название: {buyer.name}</h1>
|
|
||||||
<h2>Количество машин: {buyer.countCars}</h2>
|
|
||||||
<Button name="Добавить" variant="info" onClick={showModalFormHire} >Добавить машину</Button>
|
|
||||||
<Button name='Удалить' variant="info" onClick={showModalFormDismiss} >Удалить машину</Button>
|
|
||||||
<Button name='Назначение магазина'variant="info" onClick={showModalFormChooseStore}>Назначение магазина</Button>
|
|
||||||
<div >
|
|
||||||
<table className={`table table-condensed`}>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
{
|
|
||||||
headersEmp.map((header) => <th key={header.name}>{header.label}</th>)
|
|
||||||
}
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{
|
|
||||||
Array.from(buyer.cars).map((item, index) => <tr key={item.id}>
|
|
||||||
{
|
|
||||||
headersEmp.map((header) => <td key={`${header.name}_${item.id}`}>{item[header.name]}</td>)
|
|
||||||
}
|
|
||||||
</tr>)
|
|
||||||
}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
<ModalForm show={isShowHire} onClose={unshowModalFormHire} modalTitle={"Найм"} form={formHire}></ModalForm>
|
|
||||||
<ModalForm show={isShowDismiss} onClose={unshowModalFormDismiss} modalTitle={"Увольнение"} form={formDismiss}></ModalForm>
|
|
||||||
<ModalForm show={isShowChooseStore} onClose={unshowModalFormChooseStore} modalTitle={"Управление магазинами"} form={formCheckBoxesStore}></ModalForm>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
.prokrutka {
|
|
||||||
background: #9c9c9c; /* цвет фона, белый */
|
|
||||||
border: 1px solid #000000; /* размер и цвет границы блока */
|
|
||||||
overflow: auto;
|
|
||||||
width:200px;
|
|
||||||
height:100px;
|
|
||||||
}
|
|
@ -1,108 +0,0 @@
|
|||||||
import Store from "../models/Store";
|
|
||||||
import Form from 'react-bootstrap/Form';
|
|
||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
import { useState, useEffect } from 'react';
|
|
||||||
import DataService from '../services/DataService';
|
|
||||||
import Catalog from "./Catalog.jsx";
|
|
||||||
|
|
||||||
|
|
||||||
export default function Stores(props) {
|
|
||||||
const headers = [
|
|
||||||
{name: 'name', label: "Название"},
|
|
||||||
];
|
|
||||||
|
|
||||||
const nameCatalog = "Магазин";
|
|
||||||
const url = '/store';
|
|
||||||
const requestParams = '?name=nameData';
|
|
||||||
|
|
||||||
const [items, setItems] = useState([]);
|
|
||||||
const [data, setData] = useState(new Store());
|
|
||||||
|
|
||||||
const [isEditing, setEditing] = useState(false);
|
|
||||||
useEffect(() => {
|
|
||||||
loadItems();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
|
|
||||||
function loadItems() {
|
|
||||||
DataService.readAll(url, (data) => new Store(data))
|
|
||||||
.then(data => setItems(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleAdd() {
|
|
||||||
DataService.create(url +requestParams
|
|
||||||
.replace("nameData", data.name))
|
|
||||||
.then(() => loadItems());
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleEdit(editedId) {
|
|
||||||
DataService.read(url + "/" + editedId, (e) => new Store(e))
|
|
||||||
.then(data => {
|
|
||||||
setData(new Store(data));
|
|
||||||
});
|
|
||||||
setEditing(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
function handleEditIsDone() {
|
|
||||||
DataService.update(url + "/" + data.id + requestParams
|
|
||||||
.replace("nameData", data.name)).then(() => loadItems());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleDelete(item) {
|
|
||||||
if (window.confirm('Удалить выбранный элемент?')) {
|
|
||||||
const promises = [];
|
|
||||||
promises.push(DataService.delete(url + "/" + item));
|
|
||||||
Promise.all(promises).then(() => {
|
|
||||||
loadItems();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// при каждом изменении поля формы происходит вызов этого метода, обновляя данные объекта
|
|
||||||
function handleFormChange(event) {
|
|
||||||
setData({ ...data, [event.target.name]: event.target.value });
|
|
||||||
}
|
|
||||||
|
|
||||||
// определяет действия формы по нажатию Отправить
|
|
||||||
function submitForm() {
|
|
||||||
if (!isEditing) {
|
|
||||||
// если добавление элемента
|
|
||||||
handleAdd();
|
|
||||||
} else {
|
|
||||||
// если редактирование элемента;
|
|
||||||
handleEditIsDone();
|
|
||||||
setEditing(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// вызывается при закрытии модального окна или по нажатию кнопки Добавить и очищает данные объекта
|
|
||||||
function reset() {
|
|
||||||
setData(new Store());
|
|
||||||
setEditing(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// для каждого типа сущности своя форма,
|
|
||||||
// которая передается дальше в абстрактный компонент Catalog в качестве props.form
|
|
||||||
const form = <Form onSubmit={submitForm}>
|
|
||||||
<Form.Group className="mb-3" controlId="name">
|
|
||||||
<Form.Label>Название</Form.Label>
|
|
||||||
<Form.Control name="name" value={data.name} type="input" placeholder="Enter text" onChange={handleFormChange} required/>
|
|
||||||
</Form.Group>
|
|
||||||
<Button variant="info" type="submit">
|
|
||||||
Отправить
|
|
||||||
</Button>
|
|
||||||
</Form>
|
|
||||||
|
|
||||||
return <div className="container-lg pt-5 min-vh-100">
|
|
||||||
<Catalog name={nameCatalog}
|
|
||||||
headers={headers}
|
|
||||||
items={items}
|
|
||||||
onAdd={handleAdd}
|
|
||||||
onEdit={handleEdit}
|
|
||||||
onDelete={handleDelete}
|
|
||||||
onClose={reset}
|
|
||||||
onBtnAdd={reset}
|
|
||||||
form={form}>
|
|
||||||
</Catalog>
|
|
||||||
</div>
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
import { NavLink } from 'react-router-dom';
|
|
||||||
import Container from 'react-bootstrap/Container';
|
|
||||||
import Nav from 'react-bootstrap/Nav';
|
|
||||||
import Navbar from 'react-bootstrap/Navbar';
|
|
||||||
|
|
||||||
export default function Header(props) {
|
|
||||||
return (
|
|
||||||
<Navbar collapseOnSelect expand="lg" bg="white" variant="white">
|
|
||||||
<Container className='lg justify-content-center'>
|
|
||||||
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
|
|
||||||
<Navbar.Collapse id="responsive-navbar-nav">
|
|
||||||
<Nav className="me-auto">
|
|
||||||
{
|
|
||||||
props.links.map(route =>
|
|
||||||
<NavLink key={route.path} className="nav-link" to={route.path}>
|
|
||||||
<div>{route.label}</div>
|
|
||||||
</NavLink>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
</Nav>
|
|
||||||
</Navbar.Collapse>
|
|
||||||
</Container>
|
|
||||||
</Navbar>
|
|
||||||
);
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
export default function ItemTable(props) {
|
|
||||||
function edit() {
|
|
||||||
props.onEdit(props.item.id);
|
|
||||||
}
|
|
||||||
function remove() {
|
|
||||||
props.onDelete(props.item.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return <tr key={props.item.id}>
|
|
||||||
{
|
|
||||||
props.headers.map((header) => <td key={`${header.name}_${props.item.id}`}>{props.item[header.name]}</td>)
|
|
||||||
}
|
|
||||||
{props.isOnlyView || <td key={`controls_${props.item.id}`}>
|
|
||||||
<Button variant="info" onClick={edit}>Редактировать</Button>
|
|
||||||
<Button variant="info" onClick={remove}>Удалить</Button></td>}
|
|
||||||
</tr>
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
import Form from 'react-bootstrap/Form';
|
|
||||||
import Modal from 'react-bootstrap/Modal';
|
|
||||||
import Button from 'react-bootstrap/Button';
|
|
||||||
import { React} from 'react';
|
|
||||||
export default function ModalForm(props) {
|
|
||||||
return <Modal show={props.show} onHide={props.onClose}>
|
|
||||||
<Modal.Header closeButton>
|
|
||||||
<Modal.Title>{props.modalTitle}</Modal.Title>
|
|
||||||
</Modal.Header>
|
|
||||||
<Modal.Body>
|
|
||||||
{props.form}
|
|
||||||
</Modal.Body>
|
|
||||||
</Modal>
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
import ItemTable from './ItemTable';
|
|
||||||
export default function Table(props) {
|
|
||||||
function edit(itemId) {
|
|
||||||
props.onEdit(itemId)
|
|
||||||
}
|
|
||||||
function remove(itemId) {
|
|
||||||
props.onDelete(itemId);
|
|
||||||
}
|
|
||||||
return <div >
|
|
||||||
<table className={`table table-hover`}>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
{
|
|
||||||
props.headers.map((header) => <th key={header.name}>{header.label}</th>)
|
|
||||||
}
|
|
||||||
{props.isOnlyView || <th key='controls'>Элементы управления</th>}
|
|
||||||
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{
|
|
||||||
props.items.map((item, index) =>
|
|
||||||
<ItemTable
|
|
||||||
key={index}
|
|
||||||
headers={props.headers}
|
|
||||||
item={item}
|
|
||||||
onDelete={remove}
|
|
||||||
onEdit={edit}
|
|
||||||
isOnlyView={props.isOnlyView}/>)
|
|
||||||
}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import ReactDOM from 'react-dom/client';
|
|
||||||
import App from './App';
|
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
||||||
root.render(
|
|
||||||
<App />
|
|
||||||
);
|
|
@ -1,13 +0,0 @@
|
|||||||
import Car from "./Car";
|
|
||||||
|
|
||||||
export default class Buyer {
|
|
||||||
constructor(data) {
|
|
||||||
this.id = data?.id;
|
|
||||||
this.name = data?.name || '';
|
|
||||||
this.cars = data?.cars.map((e) => new Car(e)) || '';
|
|
||||||
this.countCars = '';
|
|
||||||
if (this.cars !== '') {
|
|
||||||
this.countCars = this.cars.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
import Store from "./Store";
|
|
||||||
|
|
||||||
export default class Car {
|
|
||||||
constructor(data) {
|
|
||||||
this.id = data?.id;
|
|
||||||
this.surname = data?.surname || '';
|
|
||||||
this.name = data?.name || '';
|
|
||||||
this.price = data?.price || '';
|
|
||||||
this.stores = data?.stores.map((p) => new Store(p)) || '';
|
|
||||||
this.storesString = '';
|
|
||||||
if (this.stores !== '') {
|
|
||||||
this.stores.forEach((p) => {this.storesString += p.name + " "});
|
|
||||||
}
|
|
||||||
this.buyer = data?.buyer || '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Car.prototype.equals = function (obj){
|
|
||||||
if(typeof obj != typeof this)
|
|
||||||
return false;
|
|
||||||
if (this.id === obj.id) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
export default class Store {
|
|
||||||
constructor(data) {
|
|
||||||
this.id = data?.id;
|
|
||||||
this.name = data?.name || '';
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
function toJSON(data) {
|
|
||||||
const jsonObj = {};
|
|
||||||
const fields = Object.getOwnPropertyNames(data);
|
|
||||||
for (const field of fields) {
|
|
||||||
if (data[field] === undefined) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
jsonObj[field] = data[field];
|
|
||||||
}
|
|
||||||
return jsonObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class DataService {
|
|
||||||
static dataUrlPrefix = 'http://localhost:8080';
|
|
||||||
|
|
||||||
static async readAll(url, transformer) {
|
|
||||||
const response = await axios.get(this.dataUrlPrefix + url);
|
|
||||||
return response.data.map(item => transformer(item));
|
|
||||||
}
|
|
||||||
|
|
||||||
static async read(url, transformer) {
|
|
||||||
const response = await axios.get(this.dataUrlPrefix + url);
|
|
||||||
return transformer(response.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static async create(url, data) {
|
|
||||||
const response = await axios.post(this.dataUrlPrefix + url);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static async update(url, data) {
|
|
||||||
const response = await axios.put(this.dataUrlPrefix + url);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static async delete(url) {
|
|
||||||
const response = await axios.delete(this.dataUrlPrefix + url);
|
|
||||||
return response.data.id;
|
|
||||||
}
|
|
||||||
}
|
|
24
frontend/.gitignore
vendored
Normal file
24
frontend/.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
19
frontend/data.json
Normal file
19
frontend/data.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"posts": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"title": "json-server",
|
||||||
|
"author": "typicode"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"comments": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"body": "some comment",
|
||||||
|
"postId": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"profile": {
|
||||||
|
"name": "typicode"
|
||||||
|
}
|
||||||
|
}
|
32
frontend/index.html
Normal file
32
frontend/index.html
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
|
||||||
|
<script
|
||||||
|
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"
|
||||||
|
integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3"
|
||||||
|
crossorigin="anonymous"
|
||||||
|
></script>
|
||||||
|
<script
|
||||||
|
src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.min.js"
|
||||||
|
integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk"
|
||||||
|
crossorigin="anonymous"
|
||||||
|
></script>
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="/node_modules/bootstrap/dist/css/bootstrap.min.css"
|
||||||
|
/>
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="/node_modules/@fortawesome/fontawesome-free/css/all.min.css"
|
||||||
|
/>
|
||||||
|
<title>Кашин Максим ПИбд-22</title>
|
||||||
|
</head>
|
||||||
|
<body class="d-flex flex-column h-100">
|
||||||
|
<div id="app"></div>
|
||||||
|
<script type="module" src="/src/main.jsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
9838
frontend/package-lock.json
generated
Normal file
9838
frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
frontend/package.json
Normal file
29
frontend/package.json
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "spa-react",
|
||||||
|
"private": true,
|
||||||
|
"version": "1.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"fake-server": "json-server --watch data.json -p 8081",
|
||||||
|
"start": "npm-run-all --parallel dev fake-server",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"react": "^18.2.0",
|
||||||
|
"react-dom": "^18.2.0",
|
||||||
|
"react-router-dom": "^6.4.4",
|
||||||
|
"axios": "^1.1.3",
|
||||||
|
"bootstrap": "^5.2.2",
|
||||||
|
"@fortawesome/fontawesome-free": "^6.2.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "^18.0.24",
|
||||||
|
"@types/react-dom": "^18.0.8",
|
||||||
|
"vite": "^3.2.3",
|
||||||
|
"@vitejs/plugin-react": "^2.2.0",
|
||||||
|
"npm-run-all": "^4.1.5",
|
||||||
|
"json-server": "^0.17.1"
|
||||||
|
}
|
||||||
|
}
|
93
frontend/src/App.css
Normal file
93
frontend/src/App.css
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
nav {
|
||||||
|
background-color: #708090;
|
||||||
|
}
|
||||||
|
main {
|
||||||
|
background-color: #ffffff;
|
||||||
|
/* min-height: 90vh; */
|
||||||
|
}
|
||||||
|
footer {
|
||||||
|
background-color: #708090;
|
||||||
|
color: black;
|
||||||
|
position: absolute;
|
||||||
|
bottom: 0;
|
||||||
|
width: 100vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
.size {
|
||||||
|
overflow-x: hidden;
|
||||||
|
}
|
||||||
|
.card {
|
||||||
|
background-color: #bdb1b1;
|
||||||
|
border-color: #8c7b7b;
|
||||||
|
}
|
||||||
|
.card-body {
|
||||||
|
background-color: #8c7b7b;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
#banner {
|
||||||
|
margin: 0px 15px 15px 15px;
|
||||||
|
padding-top: 15px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
#banner img {
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
#banner img.show {
|
||||||
|
width: 100%;
|
||||||
|
opacity: 1;
|
||||||
|
transition: opacity 1s, visibility 0s;
|
||||||
|
}
|
||||||
|
|
||||||
|
#banner img.hide {
|
||||||
|
height: 0;
|
||||||
|
width: 0;
|
||||||
|
opacity: 0;
|
||||||
|
visibility: hidden;
|
||||||
|
transition: opacity 1s, visibility 0s 1s;
|
||||||
|
}
|
||||||
|
@media (max-width: 767px) {
|
||||||
|
.btn {
|
||||||
|
padding: 1px 5px;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.5;
|
||||||
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media (min-width: 768px) and (max-width: 991px) {
|
||||||
|
.btn {
|
||||||
|
padding: 5px 10px;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.5;
|
||||||
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media (min-width: 992px) and (max-width: 1199px) {
|
||||||
|
.btn {
|
||||||
|
padding: 6px 12px;
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.42857143;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media (min-width: 1200px) {
|
||||||
|
.btn {
|
||||||
|
padding: 10px 16px;
|
||||||
|
font-size: 18px;
|
||||||
|
line-height: 1.3333333;
|
||||||
|
border-radius: 6px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@media (max-width: 600px) {
|
||||||
|
.tem {
|
||||||
|
padding-right: 50px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.vl {
|
||||||
|
border-left: 5px solid;
|
||||||
|
height: 100vh;
|
||||||
|
}
|
47
frontend/src/App.jsx
Normal file
47
frontend/src/App.jsx
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import './App.css';
|
||||||
|
import { useRoutes, Outlet, BrowserRouter } from 'react-router-dom';
|
||||||
|
import Header from './components/common/Header';
|
||||||
|
import Footer from "./components/common/Footer";
|
||||||
|
import CatalogBuyers from './components/catalogs/CatalogBuyers';
|
||||||
|
import { useState } from 'react';
|
||||||
|
import CarInfo from './components/catalogs/CarInfo';
|
||||||
|
import CatalogStores from './components/catalogs/CatalogStores';
|
||||||
|
import Find from './components/catalogs/Find';
|
||||||
|
|
||||||
|
function Router(props) {
|
||||||
|
return useRoutes(props.rootRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function App() {
|
||||||
|
const [car,setCar] = useState([]);
|
||||||
|
const routes = [
|
||||||
|
{ index: true, element: <CatalogBuyers /> },
|
||||||
|
|
||||||
|
{ path: "catalogs/store", element: <CatalogStores/> , label: "Магазины" },
|
||||||
|
{ path: "catalogs/buyer", element: <CatalogBuyers />, label: "Покупатели" },
|
||||||
|
{ path: "catalogs/car", element: <CarInfo car={car} setCar={setCar}/>, label: "Машины" },
|
||||||
|
{ path: "catalogs/find", element: <Find/>, label: "Поиск" }
|
||||||
|
];
|
||||||
|
const links = routes.filter(route => route.hasOwnProperty('label'));
|
||||||
|
const rootRoute = [
|
||||||
|
{ path: '/', element: render(links), children: routes }
|
||||||
|
];
|
||||||
|
|
||||||
|
function render(links) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Header links={links} />
|
||||||
|
<div className="container-fluid p-0">
|
||||||
|
<Outlet />
|
||||||
|
</div>
|
||||||
|
<Footer></Footer>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<BrowserRouter>
|
||||||
|
<Router rootRoute={ rootRoute } />
|
||||||
|
</BrowserRouter>
|
||||||
|
);
|
||||||
|
}
|
135
frontend/src/components/catalogs/CarInfo.jsx
Normal file
135
frontend/src/components/catalogs/CarInfo.jsx
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
import { useState, useEffect, Component } from "react";
|
||||||
|
import CatalogCar from "./CatalogCars";
|
||||||
|
import Store from "../../models/Store";
|
||||||
|
import Buyer from "../../models/Buyer";
|
||||||
|
import Car from "../../models/Car";
|
||||||
|
import DataService from "../../services/DataService";
|
||||||
|
export default function Menustores(props) {
|
||||||
|
const url = "/car";
|
||||||
|
const prodUrl = "/store";
|
||||||
|
const workUrl = "/buyer"
|
||||||
|
const transformer = (data) => new Car(data);
|
||||||
|
const catalogProdHeaders = [
|
||||||
|
{ name: "storeName", label: "Название магазина" },
|
||||||
|
];
|
||||||
|
const catalogWorkHeaders = [
|
||||||
|
{ name: "buyerFirstName", label: "Имя" },
|
||||||
|
{ name: "buyerSecondName", label: "Фамилия" },
|
||||||
|
];
|
||||||
|
const [data, setData] = useState(new Car());
|
||||||
|
const [store, setStore] = useState([]);
|
||||||
|
const [workStore, setWorkStore] = useState([]);
|
||||||
|
const [buyer, setBuyer] = useState([]);
|
||||||
|
const [workBuyer, setWorkBuyer] = useState([]);
|
||||||
|
useEffect(() => {
|
||||||
|
DataService.readAll(prodUrl, (data) => new Store(data)).then(
|
||||||
|
(data) => setStore(data)
|
||||||
|
);
|
||||||
|
DataService.readAll(workUrl, (data) => new Buyer(data)).then(
|
||||||
|
(data) => setBuyer(data)
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function handleOnAdd() {
|
||||||
|
setData(new Car());
|
||||||
|
setWorkStore([]);
|
||||||
|
setWorkBuyer([]);
|
||||||
|
}
|
||||||
|
function handleOnEdit(data) {
|
||||||
|
setData(new Car(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleFormChange(event) {
|
||||||
|
setData({ ...data, [event.target.id]: event.target.value });
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleUpdateStores(value) {
|
||||||
|
let temp = data.storeDTOList.filter((x) => x.id != value.id);
|
||||||
|
data.storeDTOList = [];
|
||||||
|
for (let i = 0; i < temp.length; i++) {
|
||||||
|
data.storeDTOList.push(temp[i]);
|
||||||
|
}
|
||||||
|
data.storeDTOList.push(value);
|
||||||
|
setData(data);
|
||||||
|
console.log(data);
|
||||||
|
setWorkStore();
|
||||||
|
}
|
||||||
|
function setCar() {
|
||||||
|
data.storeDTOList = [];
|
||||||
|
for (let i = 0; i < workStore.length; i++) {
|
||||||
|
data.storeDTOList.push(workStore[i]);
|
||||||
|
}
|
||||||
|
data.buyerDTOList = [];
|
||||||
|
for (let i = 0; i < workBuyer.length; i++) {
|
||||||
|
data.buyerDTOList.push(workBuyer[i]);
|
||||||
|
}
|
||||||
|
setData(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDeleteStores(value) {
|
||||||
|
let temp = data.storeDTOList.filter((x) => x.id != value);
|
||||||
|
data.storeDTOList = [];
|
||||||
|
for (let i = 0; i < temp.length; i++) {
|
||||||
|
data.storeDTOList.push(temp[i]);
|
||||||
|
}
|
||||||
|
console.log(data);
|
||||||
|
setData(data);
|
||||||
|
setWorkStore(workStore.filter((x) => x.id !== value));
|
||||||
|
console.log(workStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleUpdateBuyers(value){
|
||||||
|
let temp = data.buyerDTOList.filter((x) => x.id != value.id);
|
||||||
|
data.buyerDTOList = [];
|
||||||
|
for (let i = 0; i < temp.length; i++) {
|
||||||
|
data.buyerDTOList.push(temp[i]);
|
||||||
|
}
|
||||||
|
data.buyerDTOList.push(value);
|
||||||
|
setData(data);
|
||||||
|
console.log(data);
|
||||||
|
setWorkBuyer(data.buyerDTOList);
|
||||||
|
console.log(workBuyer);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDeleteBuyers(value) {
|
||||||
|
let temp = data.buyerDTOList.filter((x) => x.id != value);
|
||||||
|
data.buyerDTOList = [];
|
||||||
|
for (let i = 0; i < temp.length; i++) {
|
||||||
|
data.buyerDTOList.push(temp[i]);
|
||||||
|
}
|
||||||
|
console.log(data);
|
||||||
|
setData(data);
|
||||||
|
setWorkBuyer(workBuyer.filter((x) => x.id !== value));
|
||||||
|
console.log(workBuyer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="flex-shrink-0">
|
||||||
|
<CatalogCar
|
||||||
|
url={url}
|
||||||
|
transformer={transformer}
|
||||||
|
data={data}
|
||||||
|
onAdd={handleOnAdd}
|
||||||
|
onEdit={handleOnEdit}
|
||||||
|
handleFormChange={handleFormChange}
|
||||||
|
set={setCar}
|
||||||
|
car={props.car}
|
||||||
|
setCar={props.setCar}
|
||||||
|
|
||||||
|
store={store}
|
||||||
|
catalogProdHeaders={catalogProdHeaders}
|
||||||
|
workStore={workStore}
|
||||||
|
setWorkStore={setWorkStore}
|
||||||
|
updateStores={handleUpdateStores}
|
||||||
|
deleteStores={handleDeleteStores}
|
||||||
|
|
||||||
|
buyer={buyer}
|
||||||
|
catalogWorkHeaders={catalogWorkHeaders}
|
||||||
|
workBuyer={workBuyer}
|
||||||
|
setWorkBuyer={setWorkBuyer}
|
||||||
|
updateBuyers={handleUpdateBuyers}
|
||||||
|
deleteBuyers={handleDeleteBuyers}
|
||||||
|
/>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
114
frontend/src/components/catalogs/Catalog.jsx
Normal file
114
frontend/src/components/catalogs/Catalog.jsx
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import Toolbar from "../common/Toolbar";
|
||||||
|
import Table from "../common/Table";
|
||||||
|
import Modal from "../common/Modal";
|
||||||
|
import DataService from '../../services/DataService';
|
||||||
|
|
||||||
|
export default function CatalogW(props) {
|
||||||
|
const [items, setItems] = useState([]);
|
||||||
|
const [modalHeader, setModalHeader] = useState('');
|
||||||
|
const [modalConfirm, setModalConfirm] = useState('');
|
||||||
|
const [modalVisible, setModalVisible] = useState(false);
|
||||||
|
const [isEdit, setEdit] = useState(false);
|
||||||
|
|
||||||
|
let selectedItems = [];
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadItems();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function loadItems() {
|
||||||
|
DataService.readAll(props.getAllUrl, props.transformer)
|
||||||
|
.then(data => setItems(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveItem() {
|
||||||
|
console.log(props.data);
|
||||||
|
if (!isEdit) {
|
||||||
|
DataService.create(props.getAllUrl, props.data).then(() => loadItems());
|
||||||
|
} else {
|
||||||
|
DataService.update(props.url + props.data.id, props.data).then(() => loadItems());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleAdd() {
|
||||||
|
setEdit(false);
|
||||||
|
setModalHeader('Добавление элемента');
|
||||||
|
setModalConfirm('Добавить');
|
||||||
|
setModalVisible(true);
|
||||||
|
props.onAdd();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEdit() {
|
||||||
|
if (selectedItems.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
edit(selectedItems[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function edit(editedId) {
|
||||||
|
DataService.read(props.url + editedId, props.transformer)
|
||||||
|
.then(data => {
|
||||||
|
setEdit(true);
|
||||||
|
setModalHeader('Редактирование элемента');
|
||||||
|
setModalConfirm('Сохранить');
|
||||||
|
setModalVisible(true);
|
||||||
|
props.onEdit(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRemove() {
|
||||||
|
if (selectedItems.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (confirm('Удалить выбранные элементы?')) {
|
||||||
|
const promises = [];
|
||||||
|
selectedItems.forEach(item => {
|
||||||
|
promises.push(DataService.delete(props.url + item));
|
||||||
|
});
|
||||||
|
Promise.all(promises).then((results) => {
|
||||||
|
selectedItems.length = 0;
|
||||||
|
loadItems();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTableClick(tableSelectedItems) {
|
||||||
|
selectedItems = tableSelectedItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTableDblClick(tableSelectedItem) {
|
||||||
|
edit(tableSelectedItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleModalHide() {
|
||||||
|
setModalVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleModalDone() {
|
||||||
|
saveItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Toolbar
|
||||||
|
onAdd={handleAdd}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onRemove={handleRemove}/>
|
||||||
|
<Table
|
||||||
|
headers={props.headers}
|
||||||
|
items={items}
|
||||||
|
selectable={true}
|
||||||
|
onClick={handleTableClick}
|
||||||
|
onDblClick={handleTableDblClick}/>
|
||||||
|
<Modal
|
||||||
|
header={modalHeader}
|
||||||
|
confirm={modalConfirm}
|
||||||
|
visible={modalVisible}
|
||||||
|
onHide={handleModalHide}
|
||||||
|
onDone={handleModalDone}>
|
||||||
|
{props.children}
|
||||||
|
</Modal>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
53
frontend/src/components/catalogs/CatalogBuyers.jsx
Normal file
53
frontend/src/components/catalogs/CatalogBuyers.jsx
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import Catalog from './Catalog';
|
||||||
|
import Buyer from '../../models/Buyer';
|
||||||
|
import DataService from '../../services/DataService';
|
||||||
|
|
||||||
|
export default function CatalogBuyers(props) {
|
||||||
|
const getAllUrl = '/buyer';
|
||||||
|
const url = '/buyer/';
|
||||||
|
const transformer = (data) => new Buyer(data);
|
||||||
|
const catalogBuyerHeaders = [
|
||||||
|
{ name: 'buyerFirstName', label: 'Имя' },
|
||||||
|
{ name: 'buyerSecondName', label: 'Фамилия' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const [data, setData] = useState(new Buyer());
|
||||||
|
|
||||||
|
function handleOnAdd() {
|
||||||
|
console.log("create buyer: " + data.buyerFirstName + "|" + data.buyerSecondName);
|
||||||
|
setData(new Buyer());
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleOnEdit(data) {
|
||||||
|
setData(new Buyer(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleFormChange(event) {
|
||||||
|
setData({ ...data, [event.target.id]: event.target.value })
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="flex-shrink-0" style={{ backgroundColor: "white" }}>
|
||||||
|
<Catalog
|
||||||
|
headers={catalogBuyerHeaders}
|
||||||
|
getAllUrl={getAllUrl}
|
||||||
|
url={url}
|
||||||
|
transformer={transformer}
|
||||||
|
data={data}
|
||||||
|
onAdd={handleOnAdd}
|
||||||
|
onEdit={handleOnEdit}>
|
||||||
|
<div className="mb-3">
|
||||||
|
<label htmlFor="buyerFirstName" className="form-label">Имя покупателя</label>
|
||||||
|
<input type="text" id="buyerFirstName" className="form-control" required
|
||||||
|
value={data.buyerFirstName} onChange={handleFormChange}/>
|
||||||
|
</div>
|
||||||
|
<div className="mb-3">
|
||||||
|
<label htmlFor="buyerSecondName" className="form-label">Фамилия покупателя</label>
|
||||||
|
<input type="text" id="buyerSecondName" className="form-control" required
|
||||||
|
value={data.buyerSecondName} onChange={handleFormChange}/>
|
||||||
|
</div>
|
||||||
|
</Catalog>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
316
frontend/src/components/catalogs/CatalogCars.jsx
Normal file
316
frontend/src/components/catalogs/CatalogCars.jsx
Normal file
@ -0,0 +1,316 @@
|
|||||||
|
import { useState, useEffect, Component } from "react";
|
||||||
|
import ToolbarStore from "../common/ToolbarStore";
|
||||||
|
import Card from "../common/Card";
|
||||||
|
import Modal from "../common/Modal";
|
||||||
|
import DataService from "../../services/DataService";
|
||||||
|
import Table from "../common/Table";
|
||||||
|
import Toolbar from "../common/Toolbar";
|
||||||
|
|
||||||
|
export default function catalogP(props) {
|
||||||
|
const [items, setItems] = useState([]);
|
||||||
|
const [modalHeader, setModalHeader] = useState("");
|
||||||
|
const [modalConfirm, setModalConfirm] = useState("");
|
||||||
|
const [modalVisible, setModalVisible] = useState(false);
|
||||||
|
const [isEdit, setEdit] = useState(false);
|
||||||
|
const [StoreL, setStoreL] = useState({});
|
||||||
|
const [BuyerL, setBuyerL] = useState({});
|
||||||
|
useEffect(() => {
|
||||||
|
loadItems();
|
||||||
|
}, []);
|
||||||
|
function loadItems() {
|
||||||
|
DataService.readAll(props.url, props.transformer).then((data) =>{
|
||||||
|
setItems(data);
|
||||||
|
});
|
||||||
|
DataService.readAll(props.url, props.transformer).then((data) =>
|
||||||
|
setItems(data));
|
||||||
|
}
|
||||||
|
const catalogCarHeaders = [
|
||||||
|
{ name: 'carName', label: 'Название' },
|
||||||
|
// { name: 'storeDTOList.length', label: 'Количество магазинов'}
|
||||||
|
];
|
||||||
|
let selectedItems = [];
|
||||||
|
async function saveItem() {
|
||||||
|
console.log(props.data);
|
||||||
|
if (!isEdit) {
|
||||||
|
await props.set();
|
||||||
|
DataService.create(props.url, props.data).then(() => loadItems());
|
||||||
|
} else {
|
||||||
|
await props.set();
|
||||||
|
DataService.update(props.url + "/" + props.data.id, props.data).then(() =>
|
||||||
|
loadItems()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleRemoveCard(id) {
|
||||||
|
if (confirm("Удалить выбранные элементы?")) {
|
||||||
|
DataService.delete(props.url + "/" + id).then(() => {
|
||||||
|
loadItems();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleRemove() {
|
||||||
|
if (selectedItems.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (confirm('Удалить выбранные элементы?')) {
|
||||||
|
const promises = [];
|
||||||
|
selectedItems.forEach(item => {
|
||||||
|
promises.push(DataService.delete(props.url + "/" + item));
|
||||||
|
});
|
||||||
|
Promise.all(promises).then((results) => {
|
||||||
|
selectedItems.length = 0;
|
||||||
|
loadItems();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleAdd() {
|
||||||
|
setEdit(false);
|
||||||
|
setModalHeader("Добавление элемента");
|
||||||
|
setModalConfirm("Добавить");
|
||||||
|
setModalVisible(true);
|
||||||
|
props.onAdd();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEdit(id) {
|
||||||
|
if (selectedItems.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setStoreL(
|
||||||
|
props.workStore.filter((x) => x.id == selectedItems[0])[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function edit(editedId) {
|
||||||
|
DataService.read(props.url+ "/" + editedId, props.transformer).then((data) => {
|
||||||
|
for(let i = 0; i < data.storeDTOList.length; i++){
|
||||||
|
props.workStore.push(data.storeDTOList[i]);
|
||||||
|
props.setWorkStore(props.workStore)
|
||||||
|
}
|
||||||
|
for(let i = 0; i < data.buyerDTOList.length; i++){
|
||||||
|
props.workBuyer.push(data.buyerDTOList[i]);
|
||||||
|
//props.setworkBuyer(props.workBuyer)
|
||||||
|
}
|
||||||
|
setEdit(true);
|
||||||
|
setModalHeader("Редактирование элемента");
|
||||||
|
setModalConfirm("Сохранить");
|
||||||
|
setModalVisible(true);
|
||||||
|
props.onEdit(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleModalDone() {
|
||||||
|
saveItem();
|
||||||
|
}
|
||||||
|
function handleModalHide() {
|
||||||
|
setModalVisible(false);
|
||||||
|
props.setWorkStore([]);
|
||||||
|
props.setWorkBuyer([]);
|
||||||
|
}
|
||||||
|
function handleRemoveStore(id, e) {
|
||||||
|
if (selectedItems.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (confirm("Удалить выбранные элементы?")) {
|
||||||
|
const promises = [];
|
||||||
|
selectedItems.forEach((item) => {
|
||||||
|
props.deleteStores(item);
|
||||||
|
});
|
||||||
|
selectedItems.length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRemoveBuyer(id, e) {
|
||||||
|
if (selectedItems.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (confirm("Удалить выбранные элементы?")) {
|
||||||
|
const promises = [];
|
||||||
|
selectedItems.forEach((item) => {
|
||||||
|
props.deleteBuyers(item);
|
||||||
|
});
|
||||||
|
selectedItems.length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleAddStore() {
|
||||||
|
if (props.workStore.filter((x) => x.id == StoreL.id).length == 0)
|
||||||
|
{
|
||||||
|
await props.updateStores(StoreL);
|
||||||
|
setStoreL({});
|
||||||
|
props.workStore.push(StoreL);
|
||||||
|
await props.setWorkStore(props.workStore);
|
||||||
|
setStoreL({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function handleAddBuyer() {
|
||||||
|
if (props.workBuyer.filter((x) => x.id == BuyerL.id).length == 0)
|
||||||
|
{
|
||||||
|
await props.updateBuyers(BuyerL);
|
||||||
|
setBuyerL({});
|
||||||
|
props.workBuyer.push(BuyerL);
|
||||||
|
await props.setWorkBuyer(props.workBuyer);
|
||||||
|
setBuyerL({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleFormChangeStore(event) {
|
||||||
|
if (event.target.id === "storeName") {
|
||||||
|
setStoreL({
|
||||||
|
...StoreL,
|
||||||
|
["id"]: event.target.value,
|
||||||
|
["storeName"]: props.store
|
||||||
|
.filter((x) => x.id == event.target.value)
|
||||||
|
.map((x) => x.storeName)[0],
|
||||||
|
["price"]: props.store
|
||||||
|
.filter((x) => x.id == event.target.value)
|
||||||
|
.map((x) => x.price)[0],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setStoreL({
|
||||||
|
...StoreL,
|
||||||
|
[event.target.id]: Number(event.target.value),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleFormChangeBuyer(event) {
|
||||||
|
if (event.target.id === "buyerSecondName") {
|
||||||
|
setBuyerL({
|
||||||
|
...BuyerL,
|
||||||
|
["id"]: event.target.value,
|
||||||
|
["buyerFirstName"]: props.buyer
|
||||||
|
.filter((x) => x.id == event.target.value)
|
||||||
|
.map((x) => x.buyerFirstName)[0],
|
||||||
|
["buyerSecondName"]: props.buyer
|
||||||
|
.filter((x) => x.id == event.target.value)
|
||||||
|
.map((x) => x.buyerSecondName)[0],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setBuyerL({
|
||||||
|
...BuyerL,
|
||||||
|
[event.target.id]: Number(event.target.value),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
function handleTableClick(tableSelectedItems) {
|
||||||
|
selectedItems = tableSelectedItems;
|
||||||
|
}
|
||||||
|
function handleTableDblClickModalProd(tableSelectedItem) {
|
||||||
|
setStoreL(
|
||||||
|
props.workStore.filter((x) => x.id == tableSelectedItem)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
function handleTableDblClick(tableSelectedItem) {
|
||||||
|
edit(tableSelectedItem);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* <ToolbarStore onAdd={handleAdd} />
|
||||||
|
<Card items={items} onEdit={edit} onRemove={handleRemoveCard} car={props.car} setCar={props.setCar}/> */}
|
||||||
|
<Modal
|
||||||
|
header={modalHeader}
|
||||||
|
confirm={modalConfirm}
|
||||||
|
visible={modalVisible}
|
||||||
|
onHide={handleModalHide}
|
||||||
|
onDone={handleModalDone}
|
||||||
|
>
|
||||||
|
<div className="mb-3">
|
||||||
|
<label htmlFor="name" className="form-label">
|
||||||
|
Название машины
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="carName"
|
||||||
|
className="form-control"
|
||||||
|
required
|
||||||
|
value={props.data.carName}
|
||||||
|
onChange={props.handleFormChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="mb-3">
|
||||||
|
<label htmlFor="groupId" className="form-label">
|
||||||
|
Магазин
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
id="storeName"
|
||||||
|
className="form-select"
|
||||||
|
required
|
||||||
|
value={StoreL.id}
|
||||||
|
onChange={handleFormChangeStore}
|
||||||
|
>
|
||||||
|
<option disabled value="">
|
||||||
|
Укажите магазин
|
||||||
|
</option>
|
||||||
|
{props.store.map((group) => (
|
||||||
|
<option key={group.id} value={group.id}>
|
||||||
|
{group.storeName}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<Toolbar
|
||||||
|
onAdd={handleAddStore}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onRemove={handleRemoveStore}
|
||||||
|
/>
|
||||||
|
<Table
|
||||||
|
headers={props.catalogProdHeaders}
|
||||||
|
items={props.workStore}
|
||||||
|
allItems={props.store}
|
||||||
|
selectable={true}
|
||||||
|
onClick={handleTableClick}
|
||||||
|
onDblClick={handleTableDblClickModalProd}
|
||||||
|
/>
|
||||||
|
<div className="mb-3">
|
||||||
|
<label htmlFor="groupId" className="form-label">
|
||||||
|
Покупатель
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
id="buyerSecondName"
|
||||||
|
className="form-select"
|
||||||
|
required
|
||||||
|
value={BuyerL.id}
|
||||||
|
onChange={handleFormChangeBuyer}
|
||||||
|
>
|
||||||
|
<option disabled value="">
|
||||||
|
Укажите рабочего
|
||||||
|
</option>
|
||||||
|
{props.buyer.map((group) => (
|
||||||
|
<option key={group.id} value={group.id}>
|
||||||
|
{group.buyerFirstName} {group.buyerSecondName}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<Toolbar
|
||||||
|
onAdd={handleAddBuyer}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onRemove={handleRemoveBuyer}
|
||||||
|
/>
|
||||||
|
<Table
|
||||||
|
headers={props.catalogWorkHeaders}
|
||||||
|
items={props.workBuyer}
|
||||||
|
allItems={props.buyer}
|
||||||
|
selectable={true}
|
||||||
|
onClick={handleTableClick}
|
||||||
|
onDblClick={handleTableDblClickModalProd}
|
||||||
|
/>
|
||||||
|
</Modal>
|
||||||
|
<Toolbar
|
||||||
|
onAdd={handleAdd}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onRemove={handleRemove}
|
||||||
|
/>
|
||||||
|
<Table
|
||||||
|
headers={catalogCarHeaders}
|
||||||
|
items={items}
|
||||||
|
selectable={true}
|
||||||
|
onClick={handleTableClick}
|
||||||
|
onDblClick={handleTableDblClick}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
53
frontend/src/components/catalogs/CatalogStores.jsx
Normal file
53
frontend/src/components/catalogs/CatalogStores.jsx
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import Catalog from './Catalog';
|
||||||
|
import Store from '../../models/Store';
|
||||||
|
import DataService from '../../services/DataService';
|
||||||
|
|
||||||
|
export default function CatalogStores(props) {
|
||||||
|
const getAllUrl = '/store';
|
||||||
|
const url = '/store/';
|
||||||
|
const transformer = (data) => new Store(data);
|
||||||
|
const catalogStudHeaders = [
|
||||||
|
{ name: 'storeName', label: 'Название' },
|
||||||
|
{ name: 'price', label: 'Цена' }
|
||||||
|
];
|
||||||
|
|
||||||
|
const [data, setData] = useState(new Store());
|
||||||
|
|
||||||
|
function handleOnAdd() {
|
||||||
|
setData(new Store());
|
||||||
|
console.log("create store: " + data.storeName + "|" + data.price);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleOnEdit(data) {
|
||||||
|
setData(new Store(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="storeName" className="form-label">Название</label>
|
||||||
|
<input type="text" id="storeName" className="form-control" required
|
||||||
|
value={data.storeName} onChange={handleFormChange}/>
|
||||||
|
</div>
|
||||||
|
<div className="mb-3">
|
||||||
|
<label htmlFor="price" className="form-label">Цена</label>
|
||||||
|
<input type="number" id="price" className="form-control" required
|
||||||
|
value={data.price} onChange={handleFormChange}/>
|
||||||
|
</div>
|
||||||
|
</Catalog>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
102
frontend/src/components/catalogs/Find.jsx
Normal file
102
frontend/src/components/catalogs/Find.jsx
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
import DataService from "../../services/DataService";
|
||||||
|
import Buyer from "../../models/Buyer";
|
||||||
|
import Store from "../../models/Store";
|
||||||
|
import Table from "../common/Table";
|
||||||
|
import { useState, useEffect, Component } from "react";
|
||||||
|
import '../../App.css';
|
||||||
|
export default function Find(props) {
|
||||||
|
const prodUrl = "/store";
|
||||||
|
const workUrl = "/buyer"
|
||||||
|
const buyerURL = '/buyer/car/';
|
||||||
|
const storeURL ='/store/buyer/';
|
||||||
|
const [itemsW, setItemsW] = useState([]);
|
||||||
|
const [itemsP, setItemsP] = useState([]);
|
||||||
|
const [StoreL, setStoreL] = useState({});
|
||||||
|
const [store, setStore] = useState([]);
|
||||||
|
const [BuyerL, setBuyerL] = useState({});
|
||||||
|
const [buyer, setBuyer] = useState([]);
|
||||||
|
const catalogBuyerHeaders = [
|
||||||
|
{ name: 'buyerFirstName', label: 'Имя' },
|
||||||
|
{ name: 'buyerSecondName', label: 'Фамилия' },
|
||||||
|
];
|
||||||
|
const transformer = (data) => new Buyer(data);
|
||||||
|
function loadItems() {
|
||||||
|
console.log(BuyerL.id);
|
||||||
|
DataService.readAll(buyerURL + BuyerL.id, transformer)
|
||||||
|
.then(data => setItemsW(data));
|
||||||
|
DataService.readAll(storeURL + StoreL.id, transformer)
|
||||||
|
.then(data => setItemsP(data));
|
||||||
|
}
|
||||||
|
useEffect(() => {
|
||||||
|
DataService.readAll(prodUrl, (data) => new Store(data)).then(
|
||||||
|
(data) => setStore(data)
|
||||||
|
);
|
||||||
|
DataService.readAll(workUrl, (data) => new Buyer(data)).then(
|
||||||
|
(data) => setBuyer(data)
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
let selectedItems = [];
|
||||||
|
function handleTableClick(tableSelectedItems) {
|
||||||
|
selectedItems = tableSelectedItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTableDblClick(tableSelectedItem) {
|
||||||
|
confirm(tableSelectedItem);
|
||||||
|
}
|
||||||
|
function handleFormChangeStore(event) {
|
||||||
|
if (event.target.id === "storeName") {
|
||||||
|
setStoreL({
|
||||||
|
...StoreL,
|
||||||
|
["id"]: event.target.value,
|
||||||
|
["storeName"]: store
|
||||||
|
.filter((x) => x.id == event.target.value)
|
||||||
|
.map((x) => x.storeName)[0],
|
||||||
|
["price"]: store
|
||||||
|
.filter((x) => x.id == event.target.value)
|
||||||
|
.map((x) => x.price)[0],
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setStoreL({
|
||||||
|
...StoreL,
|
||||||
|
[event.target.id]: Number(event.target.value),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return(
|
||||||
|
<main className="form-group" style={{ backgroundColor: "white" }}>
|
||||||
|
<select
|
||||||
|
id="storeName"
|
||||||
|
className="form-select"
|
||||||
|
value={StoreL.id}
|
||||||
|
onChange={handleFormChangeStore}
|
||||||
|
>
|
||||||
|
<option disabled selected value="">
|
||||||
|
Укажите магазин
|
||||||
|
</option>
|
||||||
|
{store.map((group) => (
|
||||||
|
<option key={group.id} value={group.id}>
|
||||||
|
{group.storeName}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<a
|
||||||
|
class="btn btn-light d-flex justify-content-center align-items-center"
|
||||||
|
href="#"
|
||||||
|
type="button"
|
||||||
|
onClick={(e) => loadItems()}
|
||||||
|
>
|
||||||
|
Покупатели, которые пользуются выбранным магазином
|
||||||
|
</a>
|
||||||
|
<div className="d-flex ">
|
||||||
|
<Table className="table"
|
||||||
|
headers={catalogBuyerHeaders}
|
||||||
|
items={itemsP}
|
||||||
|
selectable={false}
|
||||||
|
onClick={handleTableClick}
|
||||||
|
onDblClick={handleTableDblClick}/>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
61
frontend/src/components/common/Card.jsx
Normal file
61
frontend/src/components/common/Card.jsx
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
import DataService from "../../services/DataService";
|
||||||
|
export default function Card(props) {
|
||||||
|
function edit(id) {
|
||||||
|
props.onEdit(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove(id) {
|
||||||
|
props.onRemove(id);
|
||||||
|
}
|
||||||
|
async function mess(id){
|
||||||
|
let currentStore = props.store.filter(x => x.id == id.id);
|
||||||
|
if (currentStore.length != 0){
|
||||||
|
let temp = props.store.filter(x => x.id != id.id);
|
||||||
|
currentStore[0].count++;
|
||||||
|
temp.push(currentStore[0]);
|
||||||
|
await props.setStore(temp);
|
||||||
|
return;
|
||||||
|
}else{
|
||||||
|
id.count++;
|
||||||
|
props.store.push(id);
|
||||||
|
props.setStore(props.store);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="temp row row-cols-1 row-cols-md-3 g-4" id="tbl-items">
|
||||||
|
{props.items.map((item) => (
|
||||||
|
<div className="col" key={item.id}>
|
||||||
|
<div className="card">
|
||||||
|
<div
|
||||||
|
className="container"
|
||||||
|
style={{ width: "100%", height: "100px", alignItems: "center" }}
|
||||||
|
>
|
||||||
|
<p>{item["carName"]}</p>
|
||||||
|
</div>
|
||||||
|
<div className="card-body">
|
||||||
|
<h5 className="card-title text-center fs-1">{item["price"]}</h5>
|
||||||
|
<a
|
||||||
|
href="#"
|
||||||
|
className="btn btn-outline-dark text-center d-flex justify-content-md-center mx-5"
|
||||||
|
onClick={(e) => remove(item.id, e)}
|
||||||
|
>
|
||||||
|
Удалить
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
href="#"
|
||||||
|
type="button"
|
||||||
|
className="btn btn-outline-dark text-center d-flex justify-content-md-center mx-5"
|
||||||
|
data-bs-toggle="modal"
|
||||||
|
data-bs-target="#staticBackdrop"
|
||||||
|
onClick={(e) => edit(item.id, e)}
|
||||||
|
>
|
||||||
|
Изменить
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
8
frontend/src/components/common/Footer.jsx
Normal file
8
frontend/src/components/common/Footer.jsx
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default function Footer(props) {
|
||||||
|
return (
|
||||||
|
<footer class="text-center p-4 p-3 mb-2 bg-light text-dark">
|
||||||
|
Кашин Максим ПИбд-22
|
||||||
|
</footer>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
32
frontend/src/components/common/Header.jsx
Normal file
32
frontend/src/components/common/Header.jsx
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { NavLink } from "react-router-dom";
|
||||||
|
|
||||||
|
export default function Header(props) {
|
||||||
|
return (
|
||||||
|
<nav className="navbar navbar-expand-lg navbar-light bg-light">
|
||||||
|
<div className="container-fluid">
|
||||||
|
<button
|
||||||
|
className="navbar-toggler"
|
||||||
|
type="button"
|
||||||
|
data-bs-toggle="collapse"
|
||||||
|
data-bs-target="#navbarSupportedContent"
|
||||||
|
aria-controls="navbarSupportedContent"
|
||||||
|
aria-expanded="false"
|
||||||
|
aria-label="Toggle navigation"
|
||||||
|
>
|
||||||
|
<span className="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div className="collapse navbar-collapse" id="navbarSupportedContent">
|
||||||
|
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
|
||||||
|
{props.links.map((route) => (
|
||||||
|
<li key={route.path} className="nav-item">
|
||||||
|
<NavLink className="nav-link fs-4" to={route.path}>
|
||||||
|
{route.label}
|
||||||
|
</NavLink>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
);
|
||||||
|
}
|
46
frontend/src/components/common/Modal.jsx
Normal file
46
frontend/src/components/common/Modal.jsx
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export default function Modal(props) {
|
||||||
|
const formRef = React.createRef();
|
||||||
|
|
||||||
|
function hide() {
|
||||||
|
props.onHide();
|
||||||
|
}
|
||||||
|
|
||||||
|
function done(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (formRef.current.checkValidity()) {
|
||||||
|
props.onDone();
|
||||||
|
hide();
|
||||||
|
} else {
|
||||||
|
formRef.current.reportValidity();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="modal fade show" tabIndex="-1" aria-hidden="true"
|
||||||
|
style={{ display: props.visible ? 'block' : 'none' }}>
|
||||||
|
<div className="modal-dialog">
|
||||||
|
<div className="modal-content">
|
||||||
|
<div className="modal-header">
|
||||||
|
<h1 className="modal-title fs-5" id="exampleModalLabel">{props.header}</h1>
|
||||||
|
<button className="btn-close" type="button" aria-label="Close"
|
||||||
|
onClick={hide}></button>
|
||||||
|
</div>
|
||||||
|
<div className="modal-body">
|
||||||
|
<form ref={formRef} onSubmit={done}>
|
||||||
|
{props.children}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div className="modal-footer">
|
||||||
|
<button className="btn btn-light" type="button" onClick={hide}>Закрыть</button>
|
||||||
|
<button className="btn btn-light" type="button" onClick={done}>
|
||||||
|
{props.confirm}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
73
frontend/src/components/common/Table.jsx
Normal file
73
frontend/src/components/common/Table.jsx
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import styles from './Table.module.css';
|
||||||
|
|
||||||
|
export default function Table(props) {
|
||||||
|
const [tableUpdate, setTableUpdate] = useState(false);
|
||||||
|
const [selectedItems, setSelectedItems] = useState([]);
|
||||||
|
|
||||||
|
function isSelected(id) {
|
||||||
|
if (!props.selectable) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return selectedItems.includes(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function click(id) {
|
||||||
|
if (!props.selectable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isSelected(id)) {
|
||||||
|
var index = selectedItems.indexOf(id);
|
||||||
|
if (index !== -1) {
|
||||||
|
selectedItems.splice(index, 1);
|
||||||
|
setSelectedItems(selectedItems);
|
||||||
|
setTableUpdate(!tableUpdate);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
selectedItems.push(id);
|
||||||
|
setSelectedItems(selectedItems);
|
||||||
|
setTableUpdate(!tableUpdate);
|
||||||
|
}
|
||||||
|
props.onClick(selectedItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
function dblClick(id) {
|
||||||
|
if (!props.selectable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
props.onDblClick(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<table className={`table table-hover ${styles.table} ${props.selectable ? styles.selectable : ''}`}>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">#</th>
|
||||||
|
{
|
||||||
|
props.headers.map(header =>
|
||||||
|
<th key={header.name} scope="col">
|
||||||
|
{header.label}
|
||||||
|
</th>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{
|
||||||
|
props.items?.map((item, index) =>
|
||||||
|
<tr key={item.id}
|
||||||
|
className={isSelected(item.id) ? styles.selected : ''}
|
||||||
|
onClick={(e) => click(item.id, e)} onDoubleClick={(e) => dblClick(item.id, e)}>
|
||||||
|
<th scope="row">{index + 1}</th>
|
||||||
|
{
|
||||||
|
props.headers.map(header =>
|
||||||
|
<td key={item.id + header.name}>{item[header.name]}</td>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
</tr>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
</tbody >
|
||||||
|
</table >
|
||||||
|
);
|
||||||
|
}
|
12
frontend/src/components/common/Table.module.css
Normal file
12
frontend/src/components/common/Table.module.css
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
.table tbody tr {
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.selectable tbody tr:hover {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.selected {
|
||||||
|
background-color: #808080;
|
||||||
|
opacity: 80%;
|
||||||
|
}
|
29
frontend/src/components/common/Toolbar.jsx
Normal file
29
frontend/src/components/common/Toolbar.jsx
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import styles from './Toolbar.module.css';
|
||||||
|
|
||||||
|
export default function Toolbar(props) {
|
||||||
|
function add() {
|
||||||
|
props.onAdd();
|
||||||
|
}
|
||||||
|
|
||||||
|
function edit() {
|
||||||
|
props.onEdit();
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove() {
|
||||||
|
props.onRemove();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="form-group" style={{ backgroundColor: "white" }}>
|
||||||
|
<a type="button" className="btn btn-light d-flex justify-content-center align-items-center" onClick={add}>
|
||||||
|
Добавить
|
||||||
|
</a>
|
||||||
|
<a type="button" className="btn btn-light d-flex justify-content-center align-items-center" onClick={edit}>
|
||||||
|
Изменить
|
||||||
|
</a>
|
||||||
|
<a type="button" className="btn btn-light d-flex justify-content-center align-items-center" onClick={remove}>
|
||||||
|
Удалить
|
||||||
|
</a>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
3
frontend/src/components/common/Toolbar.module.css
Normal file
3
frontend/src/components/common/Toolbar.module.css
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.btn {
|
||||||
|
min-width: 140px;
|
||||||
|
}
|
17
frontend/src/components/common/ToolbarStore.jsx
Normal file
17
frontend/src/components/common/ToolbarStore.jsx
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
export default function ToolbarStore(props) {
|
||||||
|
function add() {
|
||||||
|
props.onAdd();
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="btn-group mt-2" role="group">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`btn btn-outline-dark text-center d-flex justify-content-md-center mx-5 mb-3`}
|
||||||
|
onClick={add}
|
||||||
|
>
|
||||||
|
Добавить
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
8
frontend/src/main.jsx
Normal file
8
frontend/src/main.jsx
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import ReactDOM from 'react-dom/client'
|
||||||
|
import App from './App'
|
||||||
|
import './style.css'
|
||||||
|
|
||||||
|
ReactDOM.createRoot(document.getElementById('app')).render(
|
||||||
|
<App />
|
||||||
|
)
|
8
frontend/src/models/Buyer.js
Normal file
8
frontend/src/models/Buyer.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default class Buyer {
|
||||||
|
constructor(data) {
|
||||||
|
this.id = data?.id;
|
||||||
|
this.buyerFirstName = data?.buyerFirstName || "";
|
||||||
|
this.buyerSecondName = data?.buyerSecondName || "";
|
||||||
|
//this.car = data?.car || null;
|
||||||
|
}
|
||||||
|
}
|
8
frontend/src/models/Car.js
Normal file
8
frontend/src/models/Car.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default class Car {
|
||||||
|
constructor(data) {
|
||||||
|
this.id = data?.id;
|
||||||
|
this.carName = data?.carName || "";
|
||||||
|
this.storeDTOList = data?.storeDTOList || [];
|
||||||
|
this.buyerDTOList = data?.buyerDTOList || [];
|
||||||
|
}
|
||||||
|
}
|
8
frontend/src/models/Store.js
Normal file
8
frontend/src/models/Store.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default class Store {
|
||||||
|
constructor(data) {
|
||||||
|
this.id = data?.id;
|
||||||
|
this.storeName = data?.storeName || "";
|
||||||
|
this.price = data?.price || 0;
|
||||||
|
this.carDTOList = data?.carDTOList || [];
|
||||||
|
}
|
||||||
|
}
|
46
frontend/src/services/DataService.js
Normal file
46
frontend/src/services/DataService.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
export default class DataService {
|
||||||
|
static dataUrlPrefix = 'http://localhost:8080';
|
||||||
|
|
||||||
|
static async readAll(url, transformer) {
|
||||||
|
const response = await fetch(this.dataUrlPrefix + url);
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(data);
|
||||||
|
return data.map(item => transformer(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
static async read(url, transformer) {
|
||||||
|
const response = await axios.get(this.dataUrlPrefix + url);
|
||||||
|
return transformer(response.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async create(url, data) {
|
||||||
|
const requestParams = {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
};
|
||||||
|
console.log(JSON.stringify(data) + " " + this.dataUrlPrefix + " " + url);
|
||||||
|
const response = await fetch(this.dataUrlPrefix + url, requestParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async update(url, data) {
|
||||||
|
const requestParams = {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
};
|
||||||
|
const response = await fetch(this.dataUrlPrefix + url, requestParams);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async delete(url) {
|
||||||
|
const response = await axios.delete(this.dataUrlPrefix + url);
|
||||||
|
return response.data.id;
|
||||||
|
}
|
||||||
|
}
|
0
frontend/src/style.css
Normal file
0
frontend/src/style.css
Normal file
7
frontend/vite.config.js
Normal file
7
frontend/vite.config.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import react from '@vitejs/plugin-react'
|
||||||
|
|
||||||
|
// https://vitejs.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [react()]
|
||||||
|
})
|
@ -1 +1 @@
|
|||||||
rootProject.name = 'springip'
|
rootProject.name = 'lab1'
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
package com.example.springip;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class WebConfiguration implements WebMvcConfigurer {
|
|
||||||
@Override
|
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
|
||||||
registry.addMapping("/**").allowedMethods("*");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
package com.example.springip.lab3.controller;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.dto.BuyerDto;
|
|
||||||
import com.example.springip.lab3.dto.CarDto;
|
|
||||||
import com.example.springip.lab3.models.Car;
|
|
||||||
import com.example.springip.lab3.service.BuyerService;
|
|
||||||
import com.example.springip.lab3.service.CarService;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/buyer")
|
|
||||||
public class BuyerController {
|
|
||||||
private final BuyerService buyerService;
|
|
||||||
private final CarService carService;
|
|
||||||
|
|
||||||
public BuyerController(BuyerService buyerService, CarService carService) {
|
|
||||||
this.buyerService = buyerService;
|
|
||||||
this.carService = carService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public BuyerDto getBuyer(@PathVariable long id) {
|
|
||||||
return new BuyerDto(buyerService.findBuyer(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}/cars")
|
|
||||||
public List<CarDto> getBuyerCaes(@PathVariable long id) {
|
|
||||||
return buyerService.findBuyer(id).getCars().stream().map(CarDto::new).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public List<BuyerDto> getAllCompanies() {
|
|
||||||
return buyerService.findAllCompanies().stream()
|
|
||||||
.map(BuyerDto::new)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public BuyerDto create(@RequestParam("name") String name) {
|
|
||||||
return new BuyerDto(buyerService.addBuyer(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
|
||||||
public BuyerDto update(@PathVariable Long id,
|
|
||||||
@RequestParam("name") String name) {
|
|
||||||
return new BuyerDto(buyerService.updateBuyer(id, name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public BuyerDto delete(@PathVariable Long id) {
|
|
||||||
return new BuyerDto(buyerService.deleteBuyer(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}/hire")
|
|
||||||
public CarDto hire(@PathVariable Long id, @RequestParam Long carId) {
|
|
||||||
Car e = carService.findCar(carId);
|
|
||||||
return new CarDto(buyerService.addNewCar(id, e));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}/dismiss")
|
|
||||||
public void dismiss(@PathVariable Long id, @RequestParam Long carId) {
|
|
||||||
Car e = carService.findCar(carId);
|
|
||||||
buyerService.deleteCar(id, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,92 +0,0 @@
|
|||||||
package com.example.springip.lab3.controller;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.dto.CarDto;
|
|
||||||
import com.example.springip.lab3.models.Store;
|
|
||||||
import com.example.springip.lab3.service.CarService;
|
|
||||||
import com.example.springip.lab3.service.StoreService;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/car")
|
|
||||||
public class CarController {
|
|
||||||
private final CarService carService;
|
|
||||||
private final StoreService storeService;
|
|
||||||
|
|
||||||
public CarController(CarService carService, StoreService storeService) {
|
|
||||||
this.carService = carService;
|
|
||||||
this.storeService = storeService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public CarDto getCar(@PathVariable long id) {
|
|
||||||
return new CarDto(carService.findCar(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public List<CarDto> getAllCars() {
|
|
||||||
return carService.findAllCars().stream()
|
|
||||||
.map(CarDto::new)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public CarDto createCar(@RequestParam("name") String name,
|
|
||||||
@RequestParam("surname") String surname,
|
|
||||||
@RequestParam("price") String price) {
|
|
||||||
return new CarDto(carService.addCar(
|
|
||||||
surname,
|
|
||||||
name,
|
|
||||||
price));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
|
||||||
public CarDto updateCar(@PathVariable Long id,
|
|
||||||
@RequestParam("name") String name,
|
|
||||||
@RequestParam("surname") String surname,
|
|
||||||
@RequestParam("price") String price) {
|
|
||||||
return new CarDto(carService.updateCar(id, surname, name, price));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public CarDto deleteCar(@PathVariable Long id) {
|
|
||||||
return new CarDto(carService.deleteCar(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}/addPos")
|
|
||||||
public CarDto addStore(@PathVariable Long id,
|
|
||||||
@RequestParam("store") Long store) {
|
|
||||||
Store p = storeService.findStore(store);
|
|
||||||
if (p == null)
|
|
||||||
return null;
|
|
||||||
return new CarDto(carService.addStore(id, p));
|
|
||||||
}
|
|
||||||
@PutMapping("/{id}/delPos")
|
|
||||||
public CarDto delStore(@PathVariable Long id,
|
|
||||||
@RequestParam("store") Long store) {
|
|
||||||
Store p = storeService.findStore(store);
|
|
||||||
if (p == null)
|
|
||||||
return null;
|
|
||||||
return new CarDto(carService.deleteStore(id, p));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/free")
|
|
||||||
public List<CarDto> getAllFreeCars() {
|
|
||||||
return carService.findAllFreeCars().stream()
|
|
||||||
.map(CarDto::new)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/store")
|
|
||||||
public List<CarDto> getCarsByStore(@RequestParam Long pos) {
|
|
||||||
Store p = storeService.findStore(pos);
|
|
||||||
if (p == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return carService.getCarsByStore(p).stream()
|
|
||||||
.map(CarDto::new)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package com.example.springip.lab3.controller;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.dto.StoreDto;
|
|
||||||
import com.example.springip.lab3.service.StoreService;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/store")
|
|
||||||
public class StoreController {
|
|
||||||
private final StoreService storeService;
|
|
||||||
|
|
||||||
public StoreController(StoreService storeService) {
|
|
||||||
this.storeService = storeService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public StoreDto getBuyer(@PathVariable long id) {
|
|
||||||
return new StoreDto(storeService.findStore(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public List<StoreDto> getAllCompanies() {
|
|
||||||
return storeService.findAllStores().stream()
|
|
||||||
.map(StoreDto::new)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public StoreDto create(@RequestParam("name") String name) {
|
|
||||||
return new StoreDto(storeService.addStore(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
|
||||||
public StoreDto update(@PathVariable Long id,
|
|
||||||
@RequestParam("name") String name) {
|
|
||||||
return new StoreDto(storeService.updateStore(id, name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public StoreDto delete(@PathVariable Long id) {
|
|
||||||
return new StoreDto(storeService.deleteStore(id));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.springip.lab3.dao;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Buyer;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface BuyerRepository extends JpaRepository<Buyer, Long> {
|
|
||||||
Optional<Buyer> findById(Long id);
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.example.springip.lab3.dao;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Car;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface CarRepository extends JpaRepository<Car, Long> {
|
|
||||||
Optional<Car> findById(Long id);
|
|
||||||
List<Car> findByStores_Id(Long p_id);
|
|
||||||
List<Car> findByBuyerIsNull();
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.springip.lab3.dao;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Store;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface StoreRepository extends JpaRepository<Store, Long> {
|
|
||||||
Optional<Store> findById(Long id);
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
package com.example.springip.lab3.dto;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Buyer;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class BuyerDto {
|
|
||||||
private final Long Id;
|
|
||||||
private final String name;
|
|
||||||
private final List<CarDto> cars;
|
|
||||||
|
|
||||||
public BuyerDto(Buyer buyer) {
|
|
||||||
Id = buyer.getId();
|
|
||||||
this.name = buyer.getName();
|
|
||||||
this.cars = buyer.getCars().stream().map(CarDto::new).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<CarDto> getCars() {
|
|
||||||
return cars;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.springip.lab3.dto;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Buyer;
|
|
||||||
|
|
||||||
public class BuyerWithoutEmpDto {
|
|
||||||
private final Long Id;
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
public BuyerWithoutEmpDto(Buyer buyer) {
|
|
||||||
this.Id = buyer.getId();
|
|
||||||
this.name = buyer.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package com.example.springip.lab3.dto;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Car;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class CarDto {
|
|
||||||
private final Long Id;
|
|
||||||
private final String name;
|
|
||||||
private final String price;
|
|
||||||
private final String surname;
|
|
||||||
private final List<StoreDto> stores;
|
|
||||||
|
|
||||||
private final BuyerWithoutEmpDto buyer;
|
|
||||||
|
|
||||||
public CarDto(Car car) {
|
|
||||||
Id = car.getId();
|
|
||||||
this.name = car.getName();
|
|
||||||
this.price = car.getPrice();
|
|
||||||
this.surname = car.getSurname();
|
|
||||||
this.stores = car.getStores().stream().map(StoreDto::new).toList();
|
|
||||||
this.buyer = car.getBuyer() != null ? new BuyerWithoutEmpDto(car.getBuyer()) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPrice() {
|
|
||||||
return price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSurname() {
|
|
||||||
return surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<StoreDto> getStores() {
|
|
||||||
return stores;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BuyerWithoutEmpDto getBuyer() {
|
|
||||||
return buyer;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.springip.lab3.dto;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.models.Store;
|
|
||||||
|
|
||||||
public class StoreDto {
|
|
||||||
private final Long Id;
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
public StoreDto(Store store) {
|
|
||||||
Id = store.getId();
|
|
||||||
this.name = store.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,81 +0,0 @@
|
|||||||
package com.example.springip.lab3.models;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Buyer {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private Long Id;
|
|
||||||
@Column(unique = true)
|
|
||||||
private String name;
|
|
||||||
@OneToMany(mappedBy = "buyer", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
|
|
||||||
private Set<Car> cars = new HashSet<>();
|
|
||||||
|
|
||||||
public Buyer(String name, Set<Car> cars) {
|
|
||||||
this.name = name;
|
|
||||||
this.cars = cars;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Buyer(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Buyer() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addNewCar(Car car) {
|
|
||||||
cars.add(car);
|
|
||||||
car.setBuyer(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteCar(Car car) {
|
|
||||||
cars.remove(car);
|
|
||||||
car.deleteBuyer();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<Car> getCars() {
|
|
||||||
return cars;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCars(Set<Car> cars) {
|
|
||||||
this.cars = cars;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
Buyer buyer = (Buyer) o;
|
|
||||||
return Objects.equals(Id, buyer.Id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(Id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Buyer{" +
|
|
||||||
"Id=" + Id +
|
|
||||||
", name='" + name + '\'' +
|
|
||||||
", cars=" + cars +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,110 +0,0 @@
|
|||||||
package com.example.springip.lab3.models;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Car {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
private String price;
|
|
||||||
private String name;
|
|
||||||
private String surname;
|
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
|
||||||
private Buyer buyer;
|
|
||||||
|
|
||||||
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
|
||||||
private Set<Store> stores = new HashSet<>();
|
|
||||||
|
|
||||||
|
|
||||||
public Car(String surname, String name, String price) {
|
|
||||||
this.price = price;
|
|
||||||
this.name = name;
|
|
||||||
this.surname = surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Car() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPrice() {
|
|
||||||
return price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPrice(String price) {
|
|
||||||
this.price = price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSurname() {
|
|
||||||
return surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSurname(String surname) {
|
|
||||||
this.surname = surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Store> getStores() {
|
|
||||||
return stores.stream().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Buyer getBuyer() {
|
|
||||||
return buyer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBuyer(Buyer buyer) {
|
|
||||||
this.buyer = buyer;
|
|
||||||
if (!buyer.getCars().contains(this)) {
|
|
||||||
buyer.addNewCar(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteBuyer() {
|
|
||||||
if (buyer.getCars().contains(this)) {
|
|
||||||
buyer.deleteCar(this);
|
|
||||||
}
|
|
||||||
this.buyer = null;
|
|
||||||
this.stores.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addNewStore(Store p) {
|
|
||||||
stores.add(p);
|
|
||||||
if (!p.getCars().contains(this)) {
|
|
||||||
p.addNewCar(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeStore(Store p) {
|
|
||||||
stores.remove(p);
|
|
||||||
if (p.getCars().contains(this)) {
|
|
||||||
p.deleteCar(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
Car e = (Car) o;
|
|
||||||
return Objects.equals(id, e.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
package com.example.springip.lab3.models;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Store {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private Long Id;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
//mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс
|
|
||||||
@ManyToMany(mappedBy = "stores", cascade = {CascadeType.REMOVE}, fetch = FetchType.EAGER)
|
|
||||||
private List<Car> cars = new ArrayList<>();
|
|
||||||
|
|
||||||
public Store() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Store(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Car> getCars() {
|
|
||||||
return cars;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCars(List<Car> cars) {
|
|
||||||
this.cars = cars;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addNewCar(Car e) {
|
|
||||||
cars.add(e);
|
|
||||||
if (!e.getStores().contains(this)) {
|
|
||||||
e.addNewStore(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void deleteCar(Car e) {
|
|
||||||
cars.remove(e);
|
|
||||||
if (e.getStores().contains(this)) {
|
|
||||||
e.removeStore(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
Store student = (Store) o;
|
|
||||||
return Objects.equals(Id, student.Id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(Id);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,82 +0,0 @@
|
|||||||
package com.example.springip.lab3.service;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.dao.BuyerRepository;
|
|
||||||
import com.example.springip.lab3.models.Car;
|
|
||||||
import com.example.springip.lab3.models.Buyer;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class BuyerService {
|
|
||||||
private BuyerRepository buyerRepository;
|
|
||||||
private CarService carService;
|
|
||||||
public BuyerService(BuyerRepository buyerRepository, CarService carService) {
|
|
||||||
this.buyerRepository = buyerRepository;
|
|
||||||
this.carService = carService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Buyer addBuyer(String name) {
|
|
||||||
if (!StringUtils.hasText(name)) {
|
|
||||||
throw new IllegalArgumentException("Student name is null or empty");
|
|
||||||
}
|
|
||||||
final Buyer buyer = new Buyer(name);
|
|
||||||
buyerRepository.save(buyer);
|
|
||||||
return buyer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public Buyer findBuyer(Long id) {
|
|
||||||
final Buyer buyer = buyerRepository.findById(id).orElse(null);
|
|
||||||
if (buyer == null) {
|
|
||||||
throw new EntityNotFoundException(String.format("Buyer with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return buyer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<Buyer> findAllCompanies() {
|
|
||||||
return buyerRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Buyer updateBuyer(Long id, String name) {
|
|
||||||
if (!StringUtils.hasText(name)) {
|
|
||||||
throw new IllegalArgumentException("Buyer name is null or empty");
|
|
||||||
}
|
|
||||||
final Buyer currentBuyer = findBuyer(id);
|
|
||||||
currentBuyer.setName(name);
|
|
||||||
|
|
||||||
return buyerRepository.save(currentBuyer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Buyer deleteBuyer(Long id) {
|
|
||||||
final Buyer currentBuyer = findBuyer(id);
|
|
||||||
buyerRepository.delete(currentBuyer);
|
|
||||||
return currentBuyer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void deleteAllCompanies() {
|
|
||||||
buyerRepository.deleteAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car addNewCar(Long id, Car car) {
|
|
||||||
Buyer currentBuyer = findBuyer(id);
|
|
||||||
currentBuyer.addNewCar(car);
|
|
||||||
buyerRepository.save(currentBuyer);
|
|
||||||
return car;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void deleteCar(Long id, Car car) {
|
|
||||||
Buyer currentBuyer = findBuyer(id);
|
|
||||||
currentBuyer.deleteCar(car);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,112 +0,0 @@
|
|||||||
package com.example.springip.lab3.service;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.dao.CarRepository;
|
|
||||||
import com.example.springip.lab3.models.Car;
|
|
||||||
import com.example.springip.lab3.models.Buyer;
|
|
||||||
import com.example.springip.lab3.models.Store;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class CarService {
|
|
||||||
private CarRepository carRepository;
|
|
||||||
public CarService(CarRepository carRepository) {
|
|
||||||
this.carRepository = carRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car addCar(String surname, String name, String price) {
|
|
||||||
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(price)) {
|
|
||||||
throw new IllegalArgumentException("Car's data is null or empty");
|
|
||||||
}
|
|
||||||
final Car car = new Car(surname, name, price);
|
|
||||||
carRepository.save(car);
|
|
||||||
return car;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car findCar(Long id) {
|
|
||||||
final Car car = carRepository.findById(id).orElse(null);
|
|
||||||
if (car == null) {
|
|
||||||
throw new EntityNotFoundException(String.format("Car with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return car;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<Car> findAllCars() {
|
|
||||||
return carRepository.findAll();
|
|
||||||
}
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<Car> findAllFreeCars() {
|
|
||||||
return carRepository.findByBuyerIsNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car updateCar(Long id, String surname, String name, String price) {
|
|
||||||
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(price)) {
|
|
||||||
throw new IllegalArgumentException("Car's data is null or empty");
|
|
||||||
}
|
|
||||||
final Car currentCar = findCar(id);
|
|
||||||
currentCar.setName(name);
|
|
||||||
currentCar.setSurname(surname);
|
|
||||||
currentCar.setPrice(price);
|
|
||||||
|
|
||||||
return carRepository.save(currentCar);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car deleteCar(Long id) {
|
|
||||||
final Car car = findCar(id);
|
|
||||||
carRepository.delete(car);
|
|
||||||
return car;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void deleteAllCars() {
|
|
||||||
carRepository.deleteAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void addBuyer(Long id, Buyer c) {
|
|
||||||
final Car car = findCar(id);
|
|
||||||
car.setBuyer(c);
|
|
||||||
carRepository.save(car);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void deleteBuyer(Long id) {
|
|
||||||
final Car car = findCar(id);
|
|
||||||
car.deleteBuyer();
|
|
||||||
carRepository.save(car);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car addStore(Long id, Store p) {
|
|
||||||
Car e = findCar(id);
|
|
||||||
e.addNewStore(p);
|
|
||||||
System.out.println(e.getStores().size());
|
|
||||||
return carRepository.save(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Car deleteStore(Long id, Store p) {
|
|
||||||
Car e = findCar(id);
|
|
||||||
e.removeStore(p);
|
|
||||||
return carRepository.save(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public List<Car> getCarsByStore(Store p) {
|
|
||||||
// List<Car> cars = em.createQuery("select e from Car e INNER JOIN e.stores p WHERE p.id=:posit",
|
|
||||||
// Car.class)
|
|
||||||
// .setParameter("posit", p.getId())
|
|
||||||
// .getResultList();
|
|
||||||
List<Car> cars = carRepository.findByStores_Id(p.getId());
|
|
||||||
return cars;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package com.example.springip.lab3.service;
|
|
||||||
|
|
||||||
import com.example.springip.lab3.dao.StoreRepository;
|
|
||||||
import com.example.springip.lab3.models.Store;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class StoreService {
|
|
||||||
|
|
||||||
private StoreRepository storeRepository;
|
|
||||||
|
|
||||||
public StoreService(StoreRepository storeRepository) {
|
|
||||||
this.storeRepository = storeRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Store addStore(String name) {
|
|
||||||
if (!StringUtils.hasText(name)) {
|
|
||||||
throw new IllegalArgumentException("Car's data is null or empty");
|
|
||||||
}
|
|
||||||
final Store store = new Store(name);
|
|
||||||
storeRepository.save(store);
|
|
||||||
return store;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Store findStore(Long id) {
|
|
||||||
final Store store = storeRepository.findById(id).orElse(null);
|
|
||||||
if (store == null) {
|
|
||||||
throw new EntityNotFoundException(String.format("Store with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return store;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<Store> findAllStores() {
|
|
||||||
return storeRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Store updateStore(Long id, String name) {
|
|
||||||
if (!StringUtils.hasText(name)) {
|
|
||||||
throw new IllegalArgumentException("Car's data is null or empty");
|
|
||||||
}
|
|
||||||
final Store currentStore = findStore(id);
|
|
||||||
currentStore.setName(name);
|
|
||||||
|
|
||||||
return storeRepository.save(currentStore);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public Store deleteStore(Long id) {
|
|
||||||
final Store p = findStore(id);
|
|
||||||
storeRepository.delete(p);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void deleteAllStores() {
|
|
||||||
storeRepository.deleteAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,13 +1,16 @@
|
|||||||
package com.example.springip;
|
package ru.ulsru.is.lab1;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class SpringipApplication {
|
@RestController
|
||||||
|
public class Lab1Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SpringipApplication.class, args);
|
SpringApplication.run(Lab1Application.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
28
src/main/java/ru/ulsru/is/lab1/WebConfiguration.java
Normal file
28
src/main/java/ru/ulsru/is/lab1/WebConfiguration.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package ru.ulsru.is.lab1;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfiguration implements WebMvcConfigurer {
|
||||||
|
@Override
|
||||||
|
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"));
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.Respository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Buyer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface IBuyerRespository extends JpaRepository<Buyer, Long> {
|
||||||
|
@Query("SELECT w.buyers FROM Car w WHERE :buyer MEMBER OF w.buyers")
|
||||||
|
List<Buyer> findBuyersOnCar(@Param("buyer") Buyer buyer);
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.Respository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Car;
|
||||||
|
|
||||||
|
public interface ICarRespository extends JpaRepository<Car, Long> {
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.Respository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Store;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Buyer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface IStoreRespository extends JpaRepository<Store, Long> {
|
||||||
|
@Query("SELECT DISTINCT p.buyers FROM Car p where :store MEMBER OF p.stores")
|
||||||
|
List<Buyer> getBuyers(@Param("store") Store store);
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ulsru.is.lab1.lab3.service.BuyerService;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/buyer")
|
||||||
|
public class BuyerController {
|
||||||
|
private final BuyerService buyerService;
|
||||||
|
public BuyerController(BuyerService buyerService){
|
||||||
|
this.buyerService = buyerService;
|
||||||
|
}
|
||||||
|
@PostMapping
|
||||||
|
public BuyerDTO addBuyer(@RequestBody @Valid BuyerDTO buyerDTO) {
|
||||||
|
return new BuyerDTO(buyerService.addBuyer(buyerDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public BuyerDTO updateBuyer(@PathVariable Long id, @RequestBody @Valid BuyerDTO buyerDTO) {
|
||||||
|
return new BuyerDTO(buyerService.updateBuyer(id, buyerDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public BuyerDTO removeBuyer(@PathVariable Long id) {
|
||||||
|
return new BuyerDTO(buyerService.deleteBuyer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void removeAllBuyers() {
|
||||||
|
buyerService.deleteAllBuyers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public BuyerDTO findBuyer(@PathVariable Long id) {
|
||||||
|
return new BuyerDTO(buyerService.findBuyer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/car/{id}")
|
||||||
|
public List<BuyerDTO> findBuyersOnCar(@PathVariable Long id){
|
||||||
|
return buyerService.findBuyersOnCar(id).stream()
|
||||||
|
.map(BuyerDTO::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<BuyerDTO> findAllBuyer() {
|
||||||
|
return buyerService.findAllBuyers()
|
||||||
|
.stream()
|
||||||
|
.map(x-> new BuyerDTO(x))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/ru/ulsru/is/lab1/lab3/controller/BuyerDTO.java
Normal file
25
src/main/java/ru/ulsru/is/lab1/lab3/controller/BuyerDTO.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.controller;
|
||||||
|
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Buyer;
|
||||||
|
|
||||||
|
public class BuyerDTO {
|
||||||
|
private long id;
|
||||||
|
private String buyerFirstName;
|
||||||
|
private String buyerSecondName;
|
||||||
|
public BuyerDTO(Buyer buyer){
|
||||||
|
this.id =buyer.getId();
|
||||||
|
this.buyerFirstName = buyer.getBuyerFirstName();
|
||||||
|
this.buyerSecondName = buyer.getBuyerSecondName();
|
||||||
|
}
|
||||||
|
public BuyerDTO(){
|
||||||
|
}
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public String getBuyerFirstName(){
|
||||||
|
return buyerFirstName;
|
||||||
|
}
|
||||||
|
public String getBuyerSecondName(){
|
||||||
|
return buyerSecondName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ulsru.is.lab1.lab3.service.CarService;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/car")
|
||||||
|
public class CarController {
|
||||||
|
private final CarService carService;
|
||||||
|
public CarController(CarService carService){
|
||||||
|
this.carService = carService;
|
||||||
|
}
|
||||||
|
@PostMapping
|
||||||
|
public CarDTO addCar(@RequestBody @Valid CarDTO carDTO){
|
||||||
|
return new CarDTO(carService.addCar(carDTO));
|
||||||
|
}
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public CarDTO updateCar(@PathVariable Long id, @RequestBody @Valid CarDTO carDTO){
|
||||||
|
return new CarDTO(carService.updateCar(id, carDTO));
|
||||||
|
}
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public CarDTO removeCar(@PathVariable Long id){
|
||||||
|
return new CarDTO(carService.deleteCar(id));
|
||||||
|
}
|
||||||
|
@DeleteMapping
|
||||||
|
public void removeAllCars(){
|
||||||
|
carService.deleteAllCars();
|
||||||
|
}
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public CarDTO findCar(@PathVariable Long id){
|
||||||
|
return new CarDTO(carService.findCar(id));
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<CarDTO> findAllCars(){
|
||||||
|
return carService.findAllCars()
|
||||||
|
.stream()
|
||||||
|
.map(x -> new CarDTO(x))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
38
src/main/java/ru/ulsru/is/lab1/lab3/controller/CarDTO.java
Normal file
38
src/main/java/ru/ulsru/is/lab1/lab3/controller/CarDTO.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.controller;
|
||||||
|
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Car;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CarDTO {
|
||||||
|
private long id;
|
||||||
|
private String carName;
|
||||||
|
private List<BuyerDTO> buyerDTOList;
|
||||||
|
private List<StoreDTO> storeDTOList;
|
||||||
|
public CarDTO(Car car){
|
||||||
|
this.id = car.getId();
|
||||||
|
this.carName = car.getCarName();
|
||||||
|
this.storeDTOList = car.getStores() == null ? null : car.getStores()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getCar().contains(car))
|
||||||
|
.map(StoreDTO::new)
|
||||||
|
.toList();
|
||||||
|
this.buyerDTOList = car.getBuyers() == null ? null : car.getBuyers()
|
||||||
|
.stream()
|
||||||
|
.map(BuyerDTO::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
public CarDTO(){}
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public String getCarName(){
|
||||||
|
return carName;
|
||||||
|
}
|
||||||
|
public List<BuyerDTO> getBuyerDTOList(){
|
||||||
|
return buyerDTOList;
|
||||||
|
}
|
||||||
|
public List<StoreDTO> getStoreDTOList(){
|
||||||
|
return storeDTOList;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ulsru.is.lab1.lab3.service.StoreService;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/store")
|
||||||
|
public class StoreController {
|
||||||
|
private final StoreService storeService;
|
||||||
|
public StoreController(StoreService storeService){
|
||||||
|
this.storeService = storeService;
|
||||||
|
}
|
||||||
|
@PostMapping
|
||||||
|
public StoreDTO addStore(@RequestBody @Valid StoreDTO storeDTO) {
|
||||||
|
return new StoreDTO(storeService.addStore(storeDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public StoreDTO updateStore(@PathVariable Long id, @RequestBody @Valid StoreDTO storeDTO) {
|
||||||
|
return new StoreDTO(storeService.updateStore(id, storeDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public StoreDTO removeStore(@PathVariable Long id) {
|
||||||
|
return new StoreDTO(storeService.deleteStore(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void removeAllStores() {
|
||||||
|
storeService.deleteAllStores();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public StoreDTO findStore(@PathVariable Long id) {
|
||||||
|
return new StoreDTO(storeService.findStore(id));
|
||||||
|
}
|
||||||
|
@GetMapping("/buyer/{id}")
|
||||||
|
public List<BuyerDTO> findBuyersOnCar(@PathVariable Long id){
|
||||||
|
return storeService.findAllBuyersProducedStore(id)
|
||||||
|
.stream()
|
||||||
|
.map(BuyerDTO::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
@GetMapping
|
||||||
|
public List<StoreDTO> findAllStore() {
|
||||||
|
return storeService.findAllStores()
|
||||||
|
.stream()
|
||||||
|
.map(StoreDTO::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
38
src/main/java/ru/ulsru/is/lab1/lab3/controller/StoreDTO.java
Normal file
38
src/main/java/ru/ulsru/is/lab1/lab3/controller/StoreDTO.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.controller;
|
||||||
|
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Store;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class StoreDTO {
|
||||||
|
private long id;
|
||||||
|
private String storeName;
|
||||||
|
private int price;
|
||||||
|
private List<CarDTO> carDTOList;
|
||||||
|
public StoreDTO(Store store){
|
||||||
|
this.id = store.getId();
|
||||||
|
this.storeName = store.getStoreName();
|
||||||
|
this.price = store.getPrice();
|
||||||
|
this.carDTOList = store.getCar() == null ? null : store.getCar()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> x.getStores().contains(store.getId()))
|
||||||
|
.map(y -> new CarDTO(y))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
public StoreDTO() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public String getStoreName() {
|
||||||
|
return storeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
public List<CarDTO> getCarDTOList(){
|
||||||
|
return carDTOList;
|
||||||
|
}
|
||||||
|
}
|
59
src/main/java/ru/ulsru/is/lab1/lab3/model/Buyer.java
Normal file
59
src/main/java/ru/ulsru/is/lab1/lab3/model/Buyer.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "buyer")
|
||||||
|
public class Buyer {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private long id;
|
||||||
|
@NotNull(message = "First name can't be empty")
|
||||||
|
@Column(name = "first_name")
|
||||||
|
private String buyerFirstName;
|
||||||
|
@NotNull(message = "Second name can't be empty")
|
||||||
|
@Column(name = "second_name")
|
||||||
|
private String buyerSecondName;
|
||||||
|
public Buyer(){
|
||||||
|
}
|
||||||
|
public Buyer(String buyerFirstName, String buyerSecondName){
|
||||||
|
this.buyerFirstName = buyerFirstName;
|
||||||
|
this.buyerSecondName = buyerSecondName;
|
||||||
|
}
|
||||||
|
public Long getId(){return id;}
|
||||||
|
public String getBuyerFirstName(){
|
||||||
|
return buyerFirstName;
|
||||||
|
}
|
||||||
|
public String getBuyerSecondName(){
|
||||||
|
return buyerSecondName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBuyerFirstName(String buyerFirstName) {
|
||||||
|
this.buyerFirstName = buyerFirstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBuyerSecondName(String buyerSecondName) {
|
||||||
|
this.buyerSecondName = buyerSecondName;
|
||||||
|
}
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Buyer buyer = (Buyer) o;
|
||||||
|
return Objects.equals(id, buyer.id);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Buyer{" +
|
||||||
|
"id=" + id +
|
||||||
|
", buyerFirstName='" + buyerFirstName + '\'' +
|
||||||
|
", buyerSecondName='" + buyerSecondName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
105
src/main/java/ru/ulsru/is/lab1/lab3/model/Car.java
Normal file
105
src/main/java/ru/ulsru/is/lab1/lab3/model/Car.java
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "car")
|
||||||
|
public class Car {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private long id;
|
||||||
|
@NotNull(message = "Name can't be empty")
|
||||||
|
@Column(name = "name")
|
||||||
|
private String carName;
|
||||||
|
@OneToMany(cascade = {CascadeType.MERGE})
|
||||||
|
@JoinColumn(name = "buyers", nullable = true)
|
||||||
|
private List<Buyer> buyers;
|
||||||
|
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
|
||||||
|
@JoinTable(name = "cars_stores",
|
||||||
|
joinColumns = @JoinColumn(name = "car_fk"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "store_fk"))
|
||||||
|
private List<Store> stores;
|
||||||
|
public Car(){
|
||||||
|
}
|
||||||
|
public Car(String carName, List<Store> stores, List<Buyer> buyers){
|
||||||
|
this.carName = carName;
|
||||||
|
this.stores = stores;
|
||||||
|
this.buyers = buyers;
|
||||||
|
}
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCarName() {
|
||||||
|
return carName;
|
||||||
|
}
|
||||||
|
public void setCarName(String carName) {
|
||||||
|
this.carName = carName;
|
||||||
|
}
|
||||||
|
public List<Store> getStores() {
|
||||||
|
return stores;
|
||||||
|
}
|
||||||
|
public List<Buyer> getBuyers(){return buyers;}
|
||||||
|
public void setStores(List<Store> stores) {
|
||||||
|
this.stores = stores;
|
||||||
|
}
|
||||||
|
public void setBuyers(List<Buyer> buyers) {
|
||||||
|
this.buyers = buyers;
|
||||||
|
}
|
||||||
|
public void update(Car car){
|
||||||
|
this.carName = car.carName;
|
||||||
|
this.stores = car.getStores();
|
||||||
|
}
|
||||||
|
public void addStore(Store store){
|
||||||
|
if (stores == null){
|
||||||
|
this.stores = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!stores.contains(store)) {
|
||||||
|
this.stores.add(store);
|
||||||
|
store.addCar(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void removeStore(Store store){
|
||||||
|
if (stores.contains(store)) {
|
||||||
|
this.stores.remove(store);
|
||||||
|
store.removeCar(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void addBuyer(Buyer buyer){
|
||||||
|
if (buyers == null){
|
||||||
|
this.buyers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!buyers.contains(buyer)) {
|
||||||
|
this.buyers.add(buyer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void removeBuyer(Buyer buyer){
|
||||||
|
if (buyers.contains(buyer)) {
|
||||||
|
this.buyers.remove(buyer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Car car = (Car) o;
|
||||||
|
return Objects.equals(id, car.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Car{" +
|
||||||
|
"id=" + id +
|
||||||
|
", carName='" + carName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
92
src/main/java/ru/ulsru/is/lab1/lab3/model/Store.java
Normal file
92
src/main/java/ru/ulsru/is/lab1/lab3/model/Store.java
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Store {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
@NotNull(message = "Name can't be empty")
|
||||||
|
@Column(name = "storeName")
|
||||||
|
private String storeName;
|
||||||
|
@NotNull(message = "Price can't be null or empty")
|
||||||
|
@Column(name = "price")
|
||||||
|
private Integer price;
|
||||||
|
@ManyToMany(mappedBy = "stores",cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
|
||||||
|
private List<Car> car;
|
||||||
|
public Store() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Store(String storeName, Integer price) {
|
||||||
|
this.storeName = storeName;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStoreName() {
|
||||||
|
return storeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStoreName(String storeName) {
|
||||||
|
this.storeName = storeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(Integer price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Car> getCar() {
|
||||||
|
return car;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCar(List<Car> car) {
|
||||||
|
this.car = car;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCar(Car car) {
|
||||||
|
if (this.car == null) {
|
||||||
|
this.car = new ArrayList<>();
|
||||||
|
}
|
||||||
|
if (!this.car.contains(car))
|
||||||
|
this.car.add(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeCar(Car car) {
|
||||||
|
if (this.car.contains(car))
|
||||||
|
this.car.remove(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Store store = (Store) o;
|
||||||
|
return Objects.equals(id, store.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Store{" +
|
||||||
|
"id=" + id +
|
||||||
|
", storeName='" + storeName + '\'' +
|
||||||
|
", price='" + price + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.service;
|
||||||
|
|
||||||
|
public class BuyerNotFoundException extends RuntimeException {
|
||||||
|
public BuyerNotFoundException(Long id){
|
||||||
|
super(String.format("Buyer with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulsru.is.lab1.lab3.Respository.IBuyerRespository;
|
||||||
|
import ru.ulsru.is.lab1.lab3.controller.BuyerDTO;
|
||||||
|
import ru.ulsru.is.lab1.lab3.controller.CarDTO;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Buyer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BuyerService {
|
||||||
|
private final IBuyerRespository buyerRespository;
|
||||||
|
public BuyerService(IBuyerRespository buyerRespositroy){
|
||||||
|
this.buyerRespository = buyerRespositroy;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Buyer addBuyer(BuyerDTO buyerDTO) {
|
||||||
|
final Buyer buyer = new Buyer(buyerDTO.getBuyerFirstName(), buyerDTO.getBuyerSecondName());
|
||||||
|
buyerRespository.save(buyer);
|
||||||
|
return buyer;
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Buyer findBuyer(Long id) {
|
||||||
|
final Optional<Buyer> buyer = buyerRespository.findById(id);
|
||||||
|
return buyer.orElseThrow(()->new BuyerNotFoundException(id));
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Buyer> findAllBuyerByid(CarDTO carDTO) {
|
||||||
|
return buyerRespository
|
||||||
|
.findAllById(carDTO
|
||||||
|
.getBuyerDTOList()
|
||||||
|
.stream()
|
||||||
|
.map(x->x.getId())
|
||||||
|
.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Buyer> findAllBuyers() {
|
||||||
|
return buyerRespository
|
||||||
|
.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Buyer updateBuyer(Long id, BuyerDTO buyer) {
|
||||||
|
final Buyer currentBuyer = findBuyer(id);
|
||||||
|
currentBuyer.setBuyerFirstName(buyer.getBuyerFirstName());
|
||||||
|
currentBuyer.setBuyerSecondName(buyer.getBuyerSecondName());
|
||||||
|
buyerRespository.save(currentBuyer);
|
||||||
|
return currentBuyer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Buyer deleteBuyer(Long id) {
|
||||||
|
final Buyer currentBuyer = findBuyer(id);
|
||||||
|
buyerRespository.delete(currentBuyer);
|
||||||
|
return currentBuyer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Buyer> findBuyersOnCar(Long id){
|
||||||
|
return buyerRespository.findBuyersOnCar(findBuyer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllBuyers() {
|
||||||
|
buyerRespository.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.service;
|
||||||
|
|
||||||
|
public class CarNotFoundException extends RuntimeException{
|
||||||
|
public CarNotFoundException(Long id){
|
||||||
|
super(String.format("Car with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
}
|
84
src/main/java/ru/ulsru/is/lab1/lab3/service/CarService.java
Normal file
84
src/main/java/ru/ulsru/is/lab1/lab3/service/CarService.java
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulsru.is.lab1.lab3.Respository.ICarRespository;
|
||||||
|
import ru.ulsru.is.lab1.lab3.controller.CarDTO;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Store;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Buyer;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Car;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CarService {
|
||||||
|
private final ICarRespository carRespository;
|
||||||
|
private final StoreService storeService;
|
||||||
|
private final BuyerService buyerService;
|
||||||
|
public CarService(ICarRespository carRespository,
|
||||||
|
StoreService storeService,
|
||||||
|
BuyerService buyerService
|
||||||
|
){
|
||||||
|
this.carRespository = carRespository;
|
||||||
|
this.storeService = storeService;
|
||||||
|
this.buyerService = buyerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Car addCar(CarDTO carDTO) {
|
||||||
|
|
||||||
|
List<Store> stores = storeService.findAllStoresById(carDTO);
|
||||||
|
List<Buyer> buyers = buyerService.findAllBuyerByid(carDTO);
|
||||||
|
|
||||||
|
final Car car = new Car(
|
||||||
|
carDTO.getCarName(), stores, buyers);
|
||||||
|
carRespository.save(car);
|
||||||
|
return car;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Car addBuyer(CarDTO carDTO, List<Buyer> buyers){
|
||||||
|
final Car currentCar = findCar(carDTO.getId());
|
||||||
|
currentCar.setBuyers(buyers);
|
||||||
|
return currentCar;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Car addStore(CarDTO carDTO, List<Store> stores){
|
||||||
|
final Car currentCar = findCar(carDTO.getId());
|
||||||
|
currentCar.setStores(stores);
|
||||||
|
return currentCar;
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Car findCar(Long id) {
|
||||||
|
final Optional<Car> car = carRespository.findById(id);
|
||||||
|
return car.orElseThrow(() -> new CarNotFoundException(id));
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Car> findAllCars() {
|
||||||
|
return carRespository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Car updateCar(Long id, CarDTO carDTO) {
|
||||||
|
final Car currentCar = findCar(id);
|
||||||
|
currentCar.setCarName(carDTO.getCarName());
|
||||||
|
|
||||||
|
List<Store> newStores = storeService.findAllStoresById(carDTO);
|
||||||
|
List<Buyer> newBuyers = buyerService.findAllBuyerByid(carDTO);
|
||||||
|
|
||||||
|
currentCar.setStores(newStores);
|
||||||
|
currentCar.setBuyers(newBuyers);
|
||||||
|
carRespository.save(currentCar);
|
||||||
|
return currentCar;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Car deleteCar(Long id) {
|
||||||
|
final Car currentCar = findCar(id);
|
||||||
|
carRespository.delete(currentCar);
|
||||||
|
return currentCar;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllCars() {
|
||||||
|
carRespository.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.service;
|
||||||
|
|
||||||
|
public class StoreNotFoundException extends RuntimeException{
|
||||||
|
public StoreNotFoundException(Long id){
|
||||||
|
super(String.format("Store with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package ru.ulsru.is.lab1.lab3.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulsru.is.lab1.lab3.Respository.IStoreRespository;
|
||||||
|
import ru.ulsru.is.lab1.lab3.controller.StoreDTO;
|
||||||
|
import ru.ulsru.is.lab1.lab3.controller.CarDTO;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Store;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Buyer;
|
||||||
|
import ru.ulsru.is.lab1.lab3.model.Car;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StoreService {
|
||||||
|
private final IStoreRespository storeRespository;
|
||||||
|
public StoreService(IStoreRespository storeRespository){
|
||||||
|
this.storeRespository = storeRespository;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public Store addStore(StoreDTO storeDTO) {
|
||||||
|
Store store =new Store(storeDTO.getStoreName(), storeDTO.getPrice());
|
||||||
|
storeRespository.save(store);
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Store findStore(Long id) {
|
||||||
|
final Optional<Store> store = storeRespository.findById(id);
|
||||||
|
return store.orElseThrow(()->new StoreNotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Store> findAllStores() {
|
||||||
|
return storeRespository.findAll();
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Store> findAllStoresById(CarDTO carDTO) {
|
||||||
|
return storeRespository
|
||||||
|
.findAllById(carDTO
|
||||||
|
.getStoreDTOList()
|
||||||
|
.stream()
|
||||||
|
.map(x -> x.getId())
|
||||||
|
.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Store updateStore(Long id, StoreDTO storeDTO) {
|
||||||
|
final Store currentStore = findStore(id);
|
||||||
|
currentStore.setStoreName(storeDTO.getStoreName());
|
||||||
|
currentStore.setPrice(storeDTO.getPrice());
|
||||||
|
storeRespository.save(currentStore);
|
||||||
|
return currentStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Store deleteStore(Long id) {
|
||||||
|
final Store currentStore = findStore(id);
|
||||||
|
int size = currentStore.getCar().size();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
Car temp = currentStore.getCar().get(i);
|
||||||
|
temp.removeStore(currentStore);
|
||||||
|
currentStore.removeCar(temp);
|
||||||
|
}
|
||||||
|
storeRespository.delete(currentStore);
|
||||||
|
return currentStore;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public List<Buyer> findAllBuyersProducedStore(Long id){
|
||||||
|
return storeRespository.getBuyers(findStore(id));
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllStores() {
|
||||||
|
storeRespository.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
//package ru.ulsru.is.lab1.util.error;
|
||||||
|
//
|
||||||
|
//import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
|
//import org.springframework.http.HttpStatus;
|
||||||
|
//import org.springframework.http.ResponseEntity;
|
||||||
|
//import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
//import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
//import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
//import ru.ulsru.is.lab1.lab3.service.ProductNotFoundException;
|
||||||
|
//import ru.ulsru.is.lab1.lab3.service.WorkerNotFoundException;
|
||||||
|
//import ru.ulsru.is.lab1.lab3.service.WorkplaceNotFoundException;
|
||||||
|
//import ru.ulsru.is.lab1.util.validation.ValidationException;
|
||||||
|
//import java.util.stream.Collectors;
|
||||||
|
//
|
||||||
|
// @ControllerAdvice
|
||||||
|
// public class AdviceController {
|
||||||
|
// @ExceptionHandler({
|
||||||
|
// WorkerNotFoundException.class,
|
||||||
|
// ProductNotFoundException.class,
|
||||||
|
// WorkplaceNotFoundException.class,
|
||||||
|
// ValidationException.class
|
||||||
|
// })
|
||||||
|
// public ResponseEntity<Object> handleException(Throwable e) {
|
||||||
|
// return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
// public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||||
|
// final ValidationException validationException = new ValidationException(
|
||||||
|
// e.getBindingResult().getAllErrors().stream()
|
||||||
|
// .map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||||
|
// .collect(Collectors.toSet()));
|
||||||
|
// return handleException(validationException);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @ExceptionHandler(Exception.class)
|
||||||
|
// public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
// }
|
||||||
|
// }
|
@ -0,0 +1,9 @@
|
|||||||
|
//package ru.ulsru.is.lab1.util.validation;
|
||||||
|
//
|
||||||
|
//import java.util.Set;
|
||||||
|
//
|
||||||
|
//public class ValidationException extends RuntimeException {
|
||||||
|
// public ValidationException(Set<String> errors) {
|
||||||
|
// super(String.join("\n", errors));
|
||||||
|
// }
|
||||||
|
//}
|
@ -0,0 +1,30 @@
|
|||||||
|
//package ru.ulsru.is.lab1.util.validation;
|
||||||
|
//
|
||||||
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
|
//import javax.validation.ConstraintViolation;
|
||||||
|
//import javax.validation.Validation;
|
||||||
|
//import javax.validation.Validator;
|
||||||
|
//import javax.validation.ValidatorFactory;
|
||||||
|
//import java.util.Set;
|
||||||
|
//import java.util.stream.Collectors;
|
||||||
|
//
|
||||||
|
//@Component
|
||||||
|
//public class ValidatorUtil {
|
||||||
|
// private final Validator validator;
|
||||||
|
//
|
||||||
|
// public ValidatorUtil() {
|
||||||
|
// try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
||||||
|
// this.validator = factory.getValidator();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public <T> void validate(T object) {
|
||||||
|
// final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||||
|
// if (!errors.isEmpty()) {
|
||||||
|
// throw new ValidationException(errors.stream()
|
||||||
|
// .map(ConstraintViolation::getMessage)
|
||||||
|
// .collect(Collectors.toSet()));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
@ -1,5 +1,6 @@
|
|||||||
spring.main.banner-mode=off
|
spring.main.banner-mode=off
|
||||||
#server.port=8080
|
server.port=8080
|
||||||
|
server.tomcat.relaxed-query-chars=|,{,},[,]
|
||||||
spring.datasource.url=jdbc:h2:file:./data
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
spring.datasource.driverClassName=org.h2.Driver
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
spring.datasource.username=sa
|
spring.datasource.username=sa
|
||||||
|
Loading…
x
Reference in New Issue
Block a user