Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
630ac3e0bf | |||
0b8d64ab7e | |||
24dc4857e3 | |||
f153c0f108 | |||
73ed6a8278 | |||
ec634ba472 | |||
f9c5202f6b | |||
f024215f82 | |||
2105cff6f9 | |||
9730c77ed5 | |||
eb03fcc404 | |||
2683ba9907 | |||
92e9b29a1e | |||
d90389cfbe | |||
70ec81dc5c | |||
eab61da355 | |||
6dfb0655bb | |||
f0fec374ff | |||
a6d16481da | |||
815967146e |
10
build.gradle
10
build.gradle
@ -1,10 +1,10 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'org.springframework.boot' version '2.7.8'
|
id 'org.springframework.boot' version '3.0.2'
|
||||||
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
|
id 'io.spring.dependency-management' version '1.1.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'ru.ulstu.is'
|
group = 'com.example'
|
||||||
version = '0.0.1-SNAPSHOT'
|
version = '0.0.1-SNAPSHOT'
|
||||||
sourceCompatibility = '17'
|
sourceCompatibility = '17'
|
||||||
|
|
||||||
@ -14,6 +14,10 @@ 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 'com.h2database:h2:2.1.210'
|
||||||
|
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31166
front/package-lock.json
generated
Normal file
31166
front/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
48
front/package.json
Normal file
48
front/package.json
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
{
|
||||||
|
"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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
14
front/public/index.html
Normal file
14
front/public/index.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<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>
|
47
front/src/App.js
Normal file
47
front/src/App.js
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { useRoutes, Outlet, BrowserRouter } from 'react-router-dom';
|
||||||
|
import Students from './components/Students.jsx';
|
||||||
|
import Header from "./components/commons/Header.jsx"
|
||||||
|
import 'bootstrap/dist/css/bootstrap.min.css';
|
||||||
|
import DrivingSchools from './components/DrivingSchools.jsx';
|
||||||
|
import Categories from './components/Categories.jsx';
|
||||||
|
import OneDrivingSchool from './components/OneDrivingSchool.jsx';
|
||||||
|
import CountStudInCategory from './components/CountStudInCategory.jsx';
|
||||||
|
|
||||||
|
function Router(props) {
|
||||||
|
return useRoutes(props.rootRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const routes = [
|
||||||
|
{ index: true, element: <DrivingSchools /> },
|
||||||
|
{ path: '/', element: <DrivingSchools />, label: 'Сеть Автошкол' },
|
||||||
|
{ path: '/drivingSchools', element: <DrivingSchools />, label: 'Автошколы' },
|
||||||
|
{ path: '/students', element: <Students />, label: 'Студенты' },
|
||||||
|
{ path: '/categories', element: <Categories />, label: 'Категории' },
|
||||||
|
{ path: '/studcategory', element: <CountStudInCategory />, label: 'Количество студентов в категории' },
|
||||||
|
{ path: '/drivingSchool/:id', element: <OneDrivingSchool />},
|
||||||
|
];
|
||||||
|
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;
|
56
front/src/components/Catalog.jsx
Normal file
56
front/src/components/Catalog.jsx
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
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 handleChoose(itemId) {
|
||||||
|
setModalTitle("Выбрать");
|
||||||
|
props.onChoose(itemId);
|
||||||
|
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="success" onClick={handleAdd}>Добавить</Button>
|
||||||
|
<Table
|
||||||
|
headers={props.headers}
|
||||||
|
items={props.items}
|
||||||
|
onChoose={handleChoose}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onDelete={handleRemove}
|
||||||
|
/>
|
||||||
|
<ModalForm
|
||||||
|
show={show}
|
||||||
|
onClose={handleClose}
|
||||||
|
modalTitle={modalTitle}
|
||||||
|
// onSubmit={submitForm}
|
||||||
|
onChange={changeData}
|
||||||
|
form={props.form}
|
||||||
|
/></>
|
||||||
|
|
||||||
|
}
|
12
front/src/components/CatalogForGroup.jsx
Normal file
12
front/src/components/CatalogForGroup.jsx
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import TableForGroup from "./commons/TableForGroup.jsx";
|
||||||
|
// это абстрактный компонент для всех справочников
|
||||||
|
export default function CatalogForGroup(props) {
|
||||||
|
|
||||||
|
return <>
|
||||||
|
<div>{props.name}</div>
|
||||||
|
<TableForGroup
|
||||||
|
headers={props.headers}
|
||||||
|
items={props.items}
|
||||||
|
/></>
|
||||||
|
|
||||||
|
}
|
49
front/src/components/CatalogSC.jsx
Normal file
49
front/src/components/CatalogSC.jsx
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import Button from 'react-bootstrap/Button';
|
||||||
|
import TableSC from "./commons/TableSC.jsx";
|
||||||
|
import { useState} from 'react';
|
||||||
|
import ModalForm from './commons/ModalForm.jsx';
|
||||||
|
// это абстрактный компонент для всех справочников
|
||||||
|
export default function CatalogSC(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="success" onClick={handleAdd}>Добавить</Button>
|
||||||
|
<TableSC
|
||||||
|
headers={props.headers}
|
||||||
|
items={props.items}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onDelete={handleRemove}
|
||||||
|
/>
|
||||||
|
<ModalForm
|
||||||
|
show={show}
|
||||||
|
onClose={handleClose}
|
||||||
|
modalTitle={modalTitle}
|
||||||
|
// onSubmit={submitForm}
|
||||||
|
onChange={changeData}
|
||||||
|
form={props.form}
|
||||||
|
/></>
|
||||||
|
|
||||||
|
}
|
108
front/src/components/Categories.jsx
Normal file
108
front/src/components/Categories.jsx
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
import Category from "../models/Category";
|
||||||
|
import Form from 'react-bootstrap/Form';
|
||||||
|
import Button from 'react-bootstrap/Button';
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import DataService from '../services/DataService';
|
||||||
|
import CatalogSC from "./CatalogSC.jsx";
|
||||||
|
|
||||||
|
|
||||||
|
export default function Categories(props) {
|
||||||
|
const headers = [
|
||||||
|
{name: 'name', label: "Название"},
|
||||||
|
];
|
||||||
|
|
||||||
|
const nameCatalog = "Категории";
|
||||||
|
const url = '/category';
|
||||||
|
const requestParams = '?name=nameData';
|
||||||
|
|
||||||
|
const [items, setItems] = useState([]);
|
||||||
|
const [data, setData] = useState(new Category());
|
||||||
|
|
||||||
|
const [isEditing, setEditing] = useState(false);
|
||||||
|
useEffect(() => {
|
||||||
|
loadItems();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
function loadItems() {
|
||||||
|
DataService.readAll(url, (data) => new Category(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 Category(e))
|
||||||
|
.then(data => {
|
||||||
|
setData(new Category(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 Category());
|
||||||
|
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="primary" type="submit">
|
||||||
|
Отправить
|
||||||
|
</Button>
|
||||||
|
</Form>
|
||||||
|
|
||||||
|
return <div className="container-lg pt-5 min-vh-100">
|
||||||
|
<CatalogSC name={nameCatalog}
|
||||||
|
headers={headers}
|
||||||
|
items={items}
|
||||||
|
onAdd={handleAdd}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onDelete={handleDelete}
|
||||||
|
onClose={reset}
|
||||||
|
onBtnAdd={reset}
|
||||||
|
form={form}>
|
||||||
|
</CatalogSC>
|
||||||
|
</div>
|
||||||
|
}
|
37
front/src/components/CountStudInCategory.jsx
Normal file
37
front/src/components/CountStudInCategory.jsx
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import DataService from '../services/DataService';
|
||||||
|
import CatalogForGroup from "./CatalogForGroup.jsx";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import GroupedStudAndCategoryDto from "../models/GroupedStudAndCategoryDto";
|
||||||
|
|
||||||
|
export default function CountStudInCategory(props) {
|
||||||
|
|
||||||
|
const headers = [
|
||||||
|
{name: 'categoryName', label: "Название"},
|
||||||
|
{name: 'studentsCount', label: "Студенты"},
|
||||||
|
];
|
||||||
|
|
||||||
|
const nameCatalog = "Количество студентов в категории";
|
||||||
|
|
||||||
|
const url = '/categoryStudent/groupbycategory';
|
||||||
|
|
||||||
|
const [items, setItems] = useState([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadItems();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function loadItems() {
|
||||||
|
DataService.readAll(url, (data) => new GroupedStudAndCategoryDto(data))
|
||||||
|
.then(data => setItems(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
return <div className="container-lg pt-5 min-vh-100">
|
||||||
|
<CatalogForGroup name={nameCatalog}
|
||||||
|
headers={headers}
|
||||||
|
items={items}>
|
||||||
|
</CatalogForGroup>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
}
|
154
front/src/components/DrivingSchools.jsx
Normal file
154
front/src/components/DrivingSchools.jsx
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
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 DrivingSchool from "../models/DrivingSchool";
|
||||||
|
import ModalForm from './commons/ModalForm';
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
|
|
||||||
|
export default function DrivingSchools(props) {
|
||||||
|
const headers = [
|
||||||
|
{name: 'name', label: "Название"},
|
||||||
|
{name: 'countStudents', label: "Студенты"},
|
||||||
|
];
|
||||||
|
const nameCatalog = "Автошколы";
|
||||||
|
|
||||||
|
const url = '/drivingSchool';
|
||||||
|
const requestParams = '?name=nameData';
|
||||||
|
|
||||||
|
const [items, setItems] = useState([]);
|
||||||
|
|
||||||
|
|
||||||
|
const [data, setData] = useState(new DrivingSchool());
|
||||||
|
const [isEditing, setEditing] = useState(false);
|
||||||
|
useEffect(() => {
|
||||||
|
loadItems();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const [selectedId, setSelectedId] = useState(null);
|
||||||
|
|
||||||
|
function loadItems() {
|
||||||
|
DataService.readAll(url, (data) => new DrivingSchool(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 DrivingSchool(e))
|
||||||
|
.then(data => {
|
||||||
|
setData(new DrivingSchool(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 DrivingSchool());
|
||||||
|
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="primary" type="submit">
|
||||||
|
Отправить
|
||||||
|
</Button>
|
||||||
|
</Form>;
|
||||||
|
|
||||||
|
|
||||||
|
const [showModalForm, setShowChoosing] = useState(false);
|
||||||
|
const formChooseDrivingSchool = <Form onSubmit={redirectToDrivingSchool}>
|
||||||
|
<Form.Group className="mb-3" controlId="name">
|
||||||
|
<Form.Label>Автошкола</Form.Label>
|
||||||
|
<Form.Select name="name_select" type="input" onChange={(e) => {setChosenDrivingSchool(e.target.value)}} required>
|
||||||
|
<option selected disabled>Выберите автошколу</option>
|
||||||
|
{
|
||||||
|
items.map((drivingSchool) => <option key={`drivingSchool_${drivingSchool.id}`} value={`${drivingSchool.id}`}>{`${drivingSchool.name}`}</option>)
|
||||||
|
}
|
||||||
|
</Form.Select>
|
||||||
|
</Form.Group>
|
||||||
|
<Button variant="primary" type="submit">
|
||||||
|
Перейти
|
||||||
|
</Button>
|
||||||
|
</Form>;
|
||||||
|
|
||||||
|
function showModalFormChoosing() {
|
||||||
|
setShowChoosing(true);
|
||||||
|
}
|
||||||
|
function unshowModalFormChoosing() {
|
||||||
|
setShowChoosing(false);
|
||||||
|
}
|
||||||
|
const [chosenDrivingSchool, setChosenDrivingSchool] = useState(0);
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
function redirectToDrivingSchool(item) {
|
||||||
|
setSelectedId(item);
|
||||||
|
navigate(`/drivingSchool/${item}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return <div className="container-lg pt-5 min-vh-100">
|
||||||
|
<ModalForm show={showModalForm} onClose={unshowModalFormChoosing} modalTitle={"Выбор автошколы"} form={formChooseDrivingSchool}></ModalForm>
|
||||||
|
<Catalog name={nameCatalog}
|
||||||
|
headers={headers}
|
||||||
|
items={items}
|
||||||
|
onAdd={handleAdd}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onDelete={handleDelete}
|
||||||
|
onChoose={redirectToDrivingSchool}
|
||||||
|
onClose={reset}
|
||||||
|
onBtnAdd={reset}
|
||||||
|
form={form}>
|
||||||
|
</Catalog>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
}
|
256
front/src/components/OneDrivingSchool.jsx
Normal file
256
front/src/components/OneDrivingSchool.jsx
Normal file
@ -0,0 +1,256 @@
|
|||||||
|
import DrivingSchool from "../models/DrivingSchool";
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import { Link, useParams } from "react-router-dom";
|
||||||
|
import DataService from '../services/DataService';
|
||||||
|
import Student from '../models/Student';
|
||||||
|
import Category from '../models/Category';
|
||||||
|
import Form from 'react-bootstrap/Form';
|
||||||
|
import Button from 'react-bootstrap/Button';
|
||||||
|
import ModalForm from './commons/ModalForm';
|
||||||
|
import styles from "./OneDrivingSchool.module.css";
|
||||||
|
|
||||||
|
export default function OneDrivingSchool(props) {
|
||||||
|
const { id } = useParams();
|
||||||
|
|
||||||
|
const url = '/drivingSchool/id';
|
||||||
|
const urlDrivingSchoolStud = '/drivingSchool/id/students';
|
||||||
|
const urlStud = '/student/free';
|
||||||
|
const urlCat = '/category';
|
||||||
|
const urlHire = '/drivingSchool/id/hire';
|
||||||
|
const urlDismiss = '/drivingSchool/id/dismiss';
|
||||||
|
const urlAddCat = '/student/id/addCat';
|
||||||
|
const urlDelCat = '/student/id/delCat';
|
||||||
|
const requestParamsCategory = '?category=catId';
|
||||||
|
const requestParamsHire = '?studentId=studId';
|
||||||
|
|
||||||
|
const [drivingSchool, setDrivingSchool] = useState(new DrivingSchool());
|
||||||
|
const [itemsStudFree, setItemsStudFree] = useState([]);
|
||||||
|
const [itemsStudDrivingSchool, setItemsStudDrivingSchool] = useState([]);
|
||||||
|
const [itemsCat, setItemsCat] = useState([]);
|
||||||
|
|
||||||
|
const headersStud = [
|
||||||
|
{name: 'surname', label: "Фамилия"},
|
||||||
|
{name: 'name', label: "Имя"},
|
||||||
|
{name: 'categoriesString', label: 'Категории'},
|
||||||
|
{name: 'phoneNumber', label: "Номер телефона"}
|
||||||
|
];
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadDrivingSchool();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function loadDrivingSchool() {
|
||||||
|
DataService.read(url.replace("id", id), (data) => new DrivingSchool(data)).then((data) => setDrivingSchool(new DrivingSchool(data)));
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadItemsStudents() {
|
||||||
|
DataService.readAll(urlStud, (data) => new Student(data))
|
||||||
|
.then(data => setItemsStudFree(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadItemsCategories() {
|
||||||
|
DataService.readAll(urlCat, (data) => new Category(data))
|
||||||
|
.then(data => setItemsCat(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadItemsStudentsDrivingSchool() {
|
||||||
|
DataService.readAll(urlDrivingSchoolStud.replace("id", drivingSchool.id), (data) => new Student(data))
|
||||||
|
.then(data => setItemsStudDrivingSchool(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// логика для найма или увольнения
|
||||||
|
const [isShowHire, setShowHire] = useState(false);
|
||||||
|
|
||||||
|
function showModalFormHire() {
|
||||||
|
loadItemsStudents();
|
||||||
|
setShowHire(true);
|
||||||
|
}
|
||||||
|
function unshowModalFormHire() {
|
||||||
|
setShowHire(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [isShowDismiss, setShowDismiss] = useState(false);
|
||||||
|
|
||||||
|
function showModalFormDismiss(e) {
|
||||||
|
loadItemsStudentsDrivingSchool();
|
||||||
|
setShowDismiss(true);
|
||||||
|
}
|
||||||
|
function unshowModalFormDismiss() {
|
||||||
|
setShowDismiss(false);
|
||||||
|
}
|
||||||
|
const [student, setStudent] = useState(new Student());
|
||||||
|
|
||||||
|
function studentChosenFree(e) {
|
||||||
|
setStudent(itemsStudFree.filter((stud) => stud.id == e.target.value)[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function studentChosenBusy(e) {
|
||||||
|
setStudent(Array.from(drivingSchool.students).filter((stud) => stud.id == e.target.value)[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function formHireSubmit() {
|
||||||
|
DataService.update(urlHire.replace("id",drivingSchool.id) + requestParamsHire
|
||||||
|
.replace("studId", student.id))
|
||||||
|
.then(() => loadDrivingSchool());
|
||||||
|
|
||||||
|
|
||||||
|
setStudent(new Student());
|
||||||
|
}
|
||||||
|
|
||||||
|
function formDismissSubmit() {
|
||||||
|
DataService.update(urlDismiss.replace("id",drivingSchool.id) + requestParamsHire
|
||||||
|
.replace("studId", student.id))
|
||||||
|
.then(() => loadDrivingSchool());
|
||||||
|
|
||||||
|
setStudent(new Student());
|
||||||
|
}
|
||||||
|
|
||||||
|
const [isShowChooseCategory, setShowChooseCategory] = useState(false);
|
||||||
|
|
||||||
|
function showModalFormChooseCategory() {
|
||||||
|
loadItemsCategories();
|
||||||
|
loadItemsStudentsDrivingSchool();
|
||||||
|
setShowChooseCategory(true);
|
||||||
|
}
|
||||||
|
function unshowModalFormChooseCategory() {
|
||||||
|
setShowChooseCategory(false);
|
||||||
|
setCategoryStud([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [categoriesChosen, setCategoryStud] = useState([]);
|
||||||
|
function checkBoxChanged(e) {
|
||||||
|
// если чекбокс был выбран, то добавляем в массив категорию
|
||||||
|
if (e.target.checked)
|
||||||
|
{
|
||||||
|
categoriesChosen.push(e.target.value);
|
||||||
|
} // если чекбокс был убран, то исключаем его значение из массива
|
||||||
|
else
|
||||||
|
{
|
||||||
|
let i = categoriesChosen.indexOf(e.target.value);
|
||||||
|
if(i >= 0) {
|
||||||
|
categoriesChosen.splice(i,1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formChooseCategoriesSubmit() {
|
||||||
|
for (let i = 0; i < categoriesChosen.length; i++) {
|
||||||
|
DataService.update(urlAddCat.replace("id",student.id) + requestParamsCategory
|
||||||
|
.replace("catId", categoriesChosen[i]))
|
||||||
|
.then(() => loadDrivingSchool());
|
||||||
|
}
|
||||||
|
|
||||||
|
let categoriesStud = student.categories;
|
||||||
|
|
||||||
|
for (let i = 0; i < categoriesStud.length; i++) {
|
||||||
|
if (categoriesChosen.indexOf(''+categoriesStud[i].id) == -1) {
|
||||||
|
// удаление категории
|
||||||
|
DataService.update(urlDelCat.replace("id",student.id) + requestParamsCategory
|
||||||
|
.replace("catId", categoriesStud[i].id))
|
||||||
|
.then(() => loadDrivingSchool());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const formHire = <Form onSubmit={formHireSubmit}>
|
||||||
|
<Form.Group className="mb-3" controlId="student">
|
||||||
|
<Form.Label>Студент</Form.Label>
|
||||||
|
<Form.Select name="student_select" type="input" onChange={studentChosenFree} required>
|
||||||
|
<option selected disabled>Выберите студента</option>
|
||||||
|
{
|
||||||
|
itemsStudFree.map((e) => <option key={`stud_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
|
||||||
|
}
|
||||||
|
</Form.Select>
|
||||||
|
</Form.Group>
|
||||||
|
<Button variant="primary" type="submit">
|
||||||
|
Отправить
|
||||||
|
</Button>
|
||||||
|
</Form>;
|
||||||
|
|
||||||
|
|
||||||
|
const formDismiss = <Form onSubmit={formDismissSubmit}>
|
||||||
|
<Form.Group className="mb-3" controlId="student">
|
||||||
|
<Form.Label>Студент</Form.Label>
|
||||||
|
<Form.Select name="student_select" type="input" onChange={studentChosenBusy} required>
|
||||||
|
<option selected disabled>Выберите студента</option>
|
||||||
|
{
|
||||||
|
itemsStudDrivingSchool.map((e) => <option key={`stud_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
|
||||||
|
}
|
||||||
|
</Form.Select>
|
||||||
|
</Form.Group>
|
||||||
|
<Button variant="primary" type="submit">
|
||||||
|
Отправить
|
||||||
|
</Button>
|
||||||
|
</Form>;
|
||||||
|
|
||||||
|
const formCheckBoxesCategory = <Form onSubmit={formChooseCategoriesSubmit}>
|
||||||
|
<Form.Group className="mb-3" controlId="student">
|
||||||
|
<Form.Label>Студент</Form.Label>
|
||||||
|
<Form.Select name="student_select" type="input" onChange={studentChosenBusy} required>
|
||||||
|
<option selected disabled>Выберите студента</option>
|
||||||
|
{
|
||||||
|
itemsStudDrivingSchool.map((e) => <option key={`stud_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
|
||||||
|
}
|
||||||
|
</Form.Select>
|
||||||
|
</Form.Group>
|
||||||
|
<Form.Group className="mb-3" controlId="category">
|
||||||
|
<Form.Label>Категории</Form.Label>
|
||||||
|
<div className={`${styles.prokrutka}`}>
|
||||||
|
{
|
||||||
|
itemsCat.map((p) => <div style={{width: `150 px`}}>
|
||||||
|
<input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>
|
||||||
|
{/* {!studentHasCategory(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>}
|
||||||
|
{studentHasCategory(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} checked onChange={checkBoxChanged}/>} */}
|
||||||
|
{`${p.name}`}
|
||||||
|
</div>)
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</Form.Group>
|
||||||
|
<Button variant="primary" type="submit">
|
||||||
|
Отправить
|
||||||
|
</Button>
|
||||||
|
</Form>
|
||||||
|
|
||||||
|
if (!drivingSchool) {
|
||||||
|
return <div><h1>Автошкола не найдена</h1>
|
||||||
|
<Link to='/drivingSchools'>
|
||||||
|
<Button variant="info">Назад</Button>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
return <div className="container-lg pt-5 min-vh-100">
|
||||||
|
<Link to='/drivingSchools'>
|
||||||
|
<Button variant="info">Назад</Button>
|
||||||
|
</Link>
|
||||||
|
<h1>Название: {drivingSchool.name}</h1>
|
||||||
|
<h2>Количество студентов: {drivingSchool.countStudents}</h2>
|
||||||
|
<Button name="Зачисление" onClick={showModalFormHire} variant="btn btn-outline-success">Зачислить студента</Button>
|
||||||
|
<Button name='Отчисление' onClick={showModalFormDismiss} variant="btn btn-outline-danger">Отчислить студента</Button>
|
||||||
|
<Button name='Выбор категории' onClick={showModalFormChooseCategory} variant="btn btn-outline-primary">Выбор категории</Button>
|
||||||
|
<div >
|
||||||
|
<table className={`table table-hover`}>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{
|
||||||
|
headersStud.map((header) => <th key={header.name}>{header.label}</th>)
|
||||||
|
}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{
|
||||||
|
Array.from(drivingSchool.students).map((item, index) => <tr key={item.id}>
|
||||||
|
{
|
||||||
|
headersStud.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={isShowChooseCategory} onClose={unshowModalFormChooseCategory} modalTitle={"Управление категориями"} form={formCheckBoxesCategory}></ModalForm>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
}
|
7
front/src/components/OneDrivingSchool.module.css
Normal file
7
front/src/components/OneDrivingSchool.module.css
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.prokrutka {
|
||||||
|
background: #fff; /* цвет фона, белый */
|
||||||
|
border: 1px solid #C1C1C1; /* размер и цвет границы блока */
|
||||||
|
overflow: auto;
|
||||||
|
width:200px;
|
||||||
|
height:100px;
|
||||||
|
}
|
115
front/src/components/Students.jsx
Normal file
115
front/src/components/Students.jsx
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
import Student from "../models/Student";
|
||||||
|
import Form from 'react-bootstrap/Form';
|
||||||
|
import Button from 'react-bootstrap/Button';
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import DataService from '../services/DataService';
|
||||||
|
import CatalogSC from "./CatalogSC.jsx";
|
||||||
|
export default function Students(props) {
|
||||||
|
const headers = [
|
||||||
|
{name: 'surname', label: "Фамилия"},
|
||||||
|
{name: 'name', label: "Имя"},
|
||||||
|
{name: 'phoneNumber', label: "Номер телефона"},
|
||||||
|
{name: 'categoriesString', label: 'Категории'}
|
||||||
|
];
|
||||||
|
const nameCatalog = "Студенты";
|
||||||
|
const [items, setItems] = useState([]);
|
||||||
|
const url = '/student';
|
||||||
|
const requestParams = '?name=nameData&surname=surnameData&phoneNumber=phoneNameData';
|
||||||
|
const [data, setData] = useState(new Student());
|
||||||
|
const [isEditing, setEditing] = useState(false);
|
||||||
|
useEffect(() => {
|
||||||
|
loadItems();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
function loadItems() {
|
||||||
|
DataService.readAll(url, (data) => new Student(data))
|
||||||
|
.then(data => setItems(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleAdd() {
|
||||||
|
DataService.create(url +requestParams
|
||||||
|
.replace("nameData", data.name)
|
||||||
|
.replace("surnameData", data.surname)
|
||||||
|
.replace("phoneNameData", data.phoneNumber))
|
||||||
|
.then(() => loadItems());
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEdit(editedId) {
|
||||||
|
DataService.read(url + "/" + editedId, (e) => new Student(e))
|
||||||
|
.then(data => {
|
||||||
|
setData(new Student(data));
|
||||||
|
});
|
||||||
|
setEditing(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
function handleEditIsDone() {
|
||||||
|
|
||||||
|
DataService.update(url + "/" + data.id + requestParams
|
||||||
|
.replace("nameData", data.name)
|
||||||
|
.replace("surnameData", data.surname)
|
||||||
|
.replace("phoneNameData", data.phoneNumber)).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 Student());
|
||||||
|
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="phoneNumber" value={data.phoneNumber} type="input" placeholder="Enter text" onChange={handleFormChange} required/>
|
||||||
|
</Form.Group>
|
||||||
|
<Button variant="primary" type="submit">
|
||||||
|
Отправить
|
||||||
|
</Button>
|
||||||
|
</Form>
|
||||||
|
|
||||||
|
return <div className="container-lg pt-5 min-vh-100">
|
||||||
|
<CatalogSC name={nameCatalog}
|
||||||
|
headers={headers}
|
||||||
|
items={items}
|
||||||
|
onAdd={handleAdd}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onDelete={handleDelete}
|
||||||
|
onClose={reset}
|
||||||
|
onBtnAdd={reset}
|
||||||
|
form={form}>
|
||||||
|
</CatalogSC>
|
||||||
|
</div>
|
||||||
|
}
|
25
front/src/components/commons/Header.jsx
Normal file
25
front/src/components/commons/Header.jsx
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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="primary" variant="primary">
|
||||||
|
<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>
|
||||||
|
);
|
||||||
|
}
|
22
front/src/components/commons/ItemTable.jsx
Normal file
22
front/src/components/commons/ItemTable.jsx
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
function chooseDrivingSchool() {
|
||||||
|
props.onChoose(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="btn btn-outline-warning" onClick={chooseDrivingSchool}>Выбрать</Button>
|
||||||
|
<Button variant="btn btn-outline-primary" onClick={edit}>Редактировать</Button>
|
||||||
|
<Button variant="btn btn-outline-danger" onClick={remove}>Удалить</Button></td>}
|
||||||
|
</tr>
|
||||||
|
}
|
8
front/src/components/commons/ItemTableForGroup.jsx
Normal file
8
front/src/components/commons/ItemTableForGroup.jsx
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default function ItemTableForGroup(props) {
|
||||||
|
|
||||||
|
return <tr key={props.item.id}>
|
||||||
|
{
|
||||||
|
props.headers.map((header) => <td key={`${header.name}_${props.item.id}`}>{props.item[header.name]}</td>)
|
||||||
|
}
|
||||||
|
</tr>
|
||||||
|
}
|
18
front/src/components/commons/ItemTableSC.jsx
Normal file
18
front/src/components/commons/ItemTableSC.jsx
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import Button from 'react-bootstrap/Button';
|
||||||
|
export default function ItemTableSC(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="btn btn-outline-primary" onClick={edit}>Редактировать</Button>
|
||||||
|
<Button variant="btn btn-outline-danger" onClick={remove}>Удалить</Button></td>}
|
||||||
|
</tr>
|
||||||
|
}
|
14
front/src/components/commons/ModalForm.jsx
Normal file
14
front/src/components/commons/ModalForm.jsx
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
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>
|
||||||
|
}
|
39
front/src/components/commons/Table.jsx
Normal file
39
front/src/components/commons/Table.jsx
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import ItemTable from './ItemTable';
|
||||||
|
export default function Table(props) {
|
||||||
|
function edit(itemId) {
|
||||||
|
props.onEdit(itemId)
|
||||||
|
}
|
||||||
|
function remove(itemId) {
|
||||||
|
props.onDelete(itemId);
|
||||||
|
}
|
||||||
|
function chooseDrivingSchool(itemId) {
|
||||||
|
props.onChoose(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}
|
||||||
|
onChoose={chooseDrivingSchool}
|
||||||
|
isOnlyView={props.isOnlyView}/>)
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
}
|
26
front/src/components/commons/TableForGroup.jsx
Normal file
26
front/src/components/commons/TableForGroup.jsx
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import ItemTableForGroup from './ItemTableForGroup';
|
||||||
|
|
||||||
|
export default function TableForGroup(props) {
|
||||||
|
|
||||||
|
return <div >
|
||||||
|
<table className={`table table-hover`}>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{
|
||||||
|
props.headers.map((header) => <th key={header.name}>{header.label}</th>)
|
||||||
|
}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{
|
||||||
|
props.items.map((item, index) =>
|
||||||
|
<ItemTableForGroup
|
||||||
|
key={index}
|
||||||
|
headers={props.headers}
|
||||||
|
item={item}
|
||||||
|
isOnlyView={props.isOnlyView}/>)
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
}
|
35
front/src/components/commons/TableSC.jsx
Normal file
35
front/src/components/commons/TableSC.jsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import ItemTableSC from './ItemTableSC';
|
||||||
|
export default function TableSC(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) =>
|
||||||
|
<ItemTableSC
|
||||||
|
key={index}
|
||||||
|
headers={props.headers}
|
||||||
|
item={item}
|
||||||
|
onDelete={remove}
|
||||||
|
onEdit={edit}
|
||||||
|
isOnlyView={props.isOnlyView}/>)
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
}
|
8
front/src/index.js
Normal file
8
front/src/index.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import ReactDOM from 'react-dom/client';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
|
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||||
|
root.render(
|
||||||
|
<App />
|
||||||
|
);
|
6
front/src/models/Category.js
Normal file
6
front/src/models/Category.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default class Category {
|
||||||
|
constructor(data) {
|
||||||
|
this.id = data?.id;
|
||||||
|
this.name = data?.name || '';
|
||||||
|
}
|
||||||
|
}
|
13
front/src/models/DrivingSchool.js
Normal file
13
front/src/models/DrivingSchool.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import Student from "./Student";
|
||||||
|
|
||||||
|
export default class DrivingSchool {
|
||||||
|
constructor(data) {
|
||||||
|
this.id = data?.id;
|
||||||
|
this.name = data?.name || '';
|
||||||
|
this.students = data?.students.map((e) => new Student(e)) || '';
|
||||||
|
this.countStudents = '';
|
||||||
|
if (this.students !== '') {
|
||||||
|
this.countStudents = this.students.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
front/src/models/GroupedStudAndCategoryDto.js
Normal file
7
front/src/models/GroupedStudAndCategoryDto.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export default class GroupedStudAndCategoryDto {
|
||||||
|
constructor(data) {
|
||||||
|
this.Id = data?.Id;
|
||||||
|
this.categoryName = data?.categoryName;
|
||||||
|
this.studentsCount = data?.studentsCount;
|
||||||
|
}
|
||||||
|
}
|
25
front/src/models/Student.js
Normal file
25
front/src/models/Student.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import Category from "./Category";
|
||||||
|
|
||||||
|
export default class Student {
|
||||||
|
constructor(data) {
|
||||||
|
this.id = data?.id;
|
||||||
|
this.surname = data?.surname || '';
|
||||||
|
this.name = data?.name || '';
|
||||||
|
this.phoneNumber = data?.phoneNumber || '';
|
||||||
|
this.categories = data?.categories.map((p) => new Category(p)) || '';
|
||||||
|
this.categoriesString = '';
|
||||||
|
if (this.categories !== '') {
|
||||||
|
this.categories.forEach((p) => {this.categoriesString += p.name + " "});
|
||||||
|
}
|
||||||
|
this.drivingSchool = data?.drivingSchool || '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Student.prototype.equals = function (obj){
|
||||||
|
if(typeof obj != typeof this)
|
||||||
|
return false;
|
||||||
|
if (this.id === obj.id) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
42
front/src/services/DataService.js
Normal file
42
front/src/services/DataService.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class CbappApplication {
|
public class CbappApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(CbappApplication.class, args);
|
SpringApplication.run(CbappApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
12
src/main/java/ru/ulstu/is/cbapp/WebConfiguration.java
Normal file
12
src/main/java/ru/ulstu/is/cbapp/WebConfiguration.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package ru.ulstu.is.cbapp;
|
||||||
|
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("*");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package ru.ulstu.is.cbapp.controller;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.dto.CategoryDto;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryService;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/category")
|
||||||
|
public class CategoryController {
|
||||||
|
private final CategoryService categoryService;
|
||||||
|
|
||||||
|
public CategoryController(CategoryService categoryService) {
|
||||||
|
this.categoryService = categoryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public CategoryDto getDrivingSchool(@PathVariable long id) {
|
||||||
|
return new CategoryDto(categoryService.findCategory(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<CategoryDto> getAllDrivingSchools() {
|
||||||
|
return categoryService.findAllCategories().stream()
|
||||||
|
.map(CategoryDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public CategoryDto create(@RequestParam("name") String name) {
|
||||||
|
return new CategoryDto(categoryService.addCategory(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public CategoryDto update(@PathVariable Long id,
|
||||||
|
@RequestParam("name") String name) {
|
||||||
|
return new CategoryDto(categoryService.updateCategory(id, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public CategoryDto delete(@PathVariable Long id) {
|
||||||
|
return new CategoryDto(categoryService.deleteCategory(id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package ru.ulstu.is.cbapp.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import ru.ulstu.is.cbapp.dto.GroupedStudAndCategoryDto;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryService;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryStudentService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/categoryStudent")
|
||||||
|
public class CategoryStudentController {
|
||||||
|
|
||||||
|
private CategoryStudentService categoryStudentService;
|
||||||
|
|
||||||
|
public CategoryStudentController(CategoryStudentService categoryStudentService) {
|
||||||
|
this.categoryStudentService = categoryStudentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/groupbycategory")
|
||||||
|
public List<GroupedStudAndCategoryDto> getGroupedStudAndCategory(){
|
||||||
|
return categoryStudentService.getGroupedStudAndCategory();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package ru.ulstu.is.cbapp.controller;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.dto.DrivingSchoolDto;
|
||||||
|
import ru.ulstu.is.cbapp.dto.StudentDto;
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
||||||
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/drivingSchool")
|
||||||
|
public class DrivingSchoolController {
|
||||||
|
private final DrivingSchoolService drivingSchoolService;
|
||||||
|
private final StudentService studentService;
|
||||||
|
|
||||||
|
public DrivingSchoolController(DrivingSchoolService drivingSchoolService, StudentService studentService) {
|
||||||
|
this.drivingSchoolService = drivingSchoolService;
|
||||||
|
this.studentService = studentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public DrivingSchoolDto getDrivingSchool(@PathVariable long id) {
|
||||||
|
return new DrivingSchoolDto(drivingSchoolService.findDrivingSchool(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}/students")
|
||||||
|
public List<StudentDto> getDrivingSchoolStudents(@PathVariable long id) {
|
||||||
|
return drivingSchoolService.findDrivingSchool(id).getStudents().stream().map(StudentDto::new).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<DrivingSchoolDto> getAllDrivingSchools() {
|
||||||
|
return drivingSchoolService.findAllDrivingSchools().stream()
|
||||||
|
.map(DrivingSchoolDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public DrivingSchoolDto create(@RequestParam("name") String name) {
|
||||||
|
return new DrivingSchoolDto(drivingSchoolService.addDrivingSchool(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public DrivingSchoolDto update(@PathVariable Long id,
|
||||||
|
@RequestParam("name") String name) {
|
||||||
|
return new DrivingSchoolDto(drivingSchoolService.updateDrivingSchool(id, name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public DrivingSchoolDto delete(@PathVariable Long id) {
|
||||||
|
return new DrivingSchoolDto(drivingSchoolService.deleteDrivingSchool(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}/hire")
|
||||||
|
public StudentDto hire(@PathVariable Long id, @RequestParam Long studentId) {
|
||||||
|
Student e = studentService.findStudent(studentId);
|
||||||
|
return new StudentDto(drivingSchoolService.addNewStudent(id, e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}/dismiss")
|
||||||
|
public void dismiss(@PathVariable Long id, @RequestParam Long studentId) {
|
||||||
|
Student e = studentService.findStudent(studentId);
|
||||||
|
drivingSchoolService.deleteStudent(id, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
package ru.ulstu.is.cbapp.controller;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.dto.StudentDto;
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryService;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/student")
|
||||||
|
public class StudentController {
|
||||||
|
private final StudentService studentService;
|
||||||
|
private final CategoryService categoryService;
|
||||||
|
|
||||||
|
public StudentController(StudentService studentService, CategoryService categoryService) {
|
||||||
|
this.studentService = studentService;
|
||||||
|
this.categoryService = categoryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public StudentDto getStudent(@PathVariable long id) {
|
||||||
|
return new StudentDto(studentService.findStudent(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<StudentDto> getAllStudents() {
|
||||||
|
return studentService.findAllStudents().stream()
|
||||||
|
.map(StudentDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public StudentDto createStudent(@RequestParam("name") String name,
|
||||||
|
@RequestParam("surname") String surname,
|
||||||
|
@RequestParam("phoneNumber") String phoneNumber) {
|
||||||
|
return new StudentDto(studentService.addStudent(
|
||||||
|
surname,
|
||||||
|
name,
|
||||||
|
phoneNumber));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public StudentDto updateStudent(@PathVariable Long id,
|
||||||
|
@RequestParam("name") String name,
|
||||||
|
@RequestParam("surname") String surname,
|
||||||
|
@RequestParam("phoneNumber") String phoneNumber) {
|
||||||
|
return new StudentDto(studentService.updateStudent(id, surname, name, phoneNumber));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public StudentDto deleteStudent(@PathVariable Long id) {
|
||||||
|
return new StudentDto(studentService.deleteStudent(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}/addCat")
|
||||||
|
public StudentDto addCategory(@PathVariable Long id,
|
||||||
|
@RequestParam("category") Long category) {
|
||||||
|
Category p = categoryService.findCategory(category);
|
||||||
|
if (p == null)
|
||||||
|
return null;
|
||||||
|
return new StudentDto(studentService.addCategory(id, p));
|
||||||
|
}
|
||||||
|
@PutMapping("/{id}/delCat")
|
||||||
|
public StudentDto delCategory(@PathVariable Long id,
|
||||||
|
@RequestParam("category") Long category) {
|
||||||
|
Category p = categoryService.findCategory(category);
|
||||||
|
if (p == null)
|
||||||
|
return null;
|
||||||
|
return new StudentDto(studentService.deleteCategory(id, p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/free")
|
||||||
|
public List<StudentDto> getAllFreeStudents() {
|
||||||
|
return studentService.findAllFreeStudents().stream()
|
||||||
|
.map(StudentDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/category")
|
||||||
|
public List<StudentDto> getStudentsByCategory(@RequestParam Long cat) {
|
||||||
|
Category p = categoryService.findCategory(cat);
|
||||||
|
if (p == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return studentService.getStudentsByCategory(p).stream()
|
||||||
|
.map(StudentDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
13
src/main/java/ru/ulstu/is/cbapp/dao/CategoryRepository.java
Normal file
13
src/main/java/ru/ulstu/is/cbapp/dao/CategoryRepository.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dao;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import ru.ulstu.is.cbapp.dto.GroupedStudAndCategoryDto;
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface CategoryRepository extends JpaRepository<Category, Long> {
|
||||||
|
Optional<Category> findById(Long id);
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dao;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface DrivingSchoolRepository extends JpaRepository<DrivingSchool, Long> {
|
||||||
|
Optional<DrivingSchool> findById(Long id);
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dao;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import ru.ulstu.is.cbapp.dto.GroupedStudAndCategoryDto;
|
||||||
|
import ru.ulstu.is.cbapp.models.StudentCategories;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface StudentCategoriesRepository extends JpaRepository<StudentCategories, Long> {
|
||||||
|
|
||||||
|
Optional<StudentCategories> findById(Long id);
|
||||||
|
|
||||||
|
@Query("SELECT new ru.ulstu.is.cbapp.dto.GroupedStudAndCategoryDto(cs.category.name,COUNT(cs.student.id))"+
|
||||||
|
"FROM StudentCategories cs "+
|
||||||
|
"group by cs.category.name")
|
||||||
|
List<GroupedStudAndCategoryDto> getGroupedStudAndCategory();
|
||||||
|
}
|
13
src/main/java/ru/ulstu/is/cbapp/dao/StudentRepository.java
Normal file
13
src/main/java/ru/ulstu/is/cbapp/dao/StudentRepository.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dao;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface StudentRepository extends JpaRepository<Student, Long> {
|
||||||
|
Optional<Student> findById(Long id);
|
||||||
|
List<Student> findByCategories_Id(Long p_id);
|
||||||
|
List<Student> findByDrivingSchoolIsNull();
|
||||||
|
}
|
21
src/main/java/ru/ulstu/is/cbapp/dto/CategoryDto.java
Normal file
21
src/main/java/ru/ulstu/is/cbapp/dto/CategoryDto.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dto;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
|
||||||
|
public class CategoryDto {
|
||||||
|
private final Long Id;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public CategoryDto(Category category) {
|
||||||
|
Id = category.getId();
|
||||||
|
this.name = category.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
29
src/main/java/ru/ulstu/is/cbapp/dto/DrivingSchoolDto.java
Normal file
29
src/main/java/ru/ulstu/is/cbapp/dto/DrivingSchoolDto.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dto;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DrivingSchoolDto {
|
||||||
|
private final Long Id;
|
||||||
|
private final String name;
|
||||||
|
private final List<StudentDto> students;
|
||||||
|
|
||||||
|
public DrivingSchoolDto(DrivingSchool drivingSchool) {
|
||||||
|
Id = drivingSchool.getId();
|
||||||
|
this.name = drivingSchool.getName();
|
||||||
|
this.students = drivingSchool.getStudents().stream().map(StudentDto::new).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<StudentDto> getStudents() {
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dto;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
|
||||||
|
public class DrivingSchoolWithoutStudDto {
|
||||||
|
private final Long Id;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public DrivingSchoolWithoutStudDto(DrivingSchool drivingSchool) {
|
||||||
|
this.Id = drivingSchool.getId();
|
||||||
|
this.name = drivingSchool.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dto;
|
||||||
|
|
||||||
|
public class GroupedStudAndCategoryDto {
|
||||||
|
private String categoryName;
|
||||||
|
private Long StudentsCount;
|
||||||
|
|
||||||
|
public GroupedStudAndCategoryDto(String categoryName, Long StudentsCount){
|
||||||
|
this.categoryName=categoryName;
|
||||||
|
this.StudentsCount=StudentsCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCategoryName() {
|
||||||
|
return categoryName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCategoryId(String categoryName) {
|
||||||
|
this.categoryName = categoryName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getStudentsCount() {
|
||||||
|
return StudentsCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudentsCount(Long studentsCount) {
|
||||||
|
StudentsCount = studentsCount;
|
||||||
|
}
|
||||||
|
}
|
48
src/main/java/ru/ulstu/is/cbapp/dto/StudentDto.java
Normal file
48
src/main/java/ru/ulstu/is/cbapp/dto/StudentDto.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package ru.ulstu.is.cbapp.dto;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class StudentDto {
|
||||||
|
private final Long Id;
|
||||||
|
private final String name;
|
||||||
|
private final String phoneNumber;
|
||||||
|
private final String surname;
|
||||||
|
private final List<CategoryDto> categories;
|
||||||
|
|
||||||
|
private final DrivingSchoolWithoutStudDto drivingSchool;
|
||||||
|
|
||||||
|
public StudentDto(Student student) {
|
||||||
|
Id = student.getId();
|
||||||
|
this.name = student.getName();
|
||||||
|
this.phoneNumber = student.getPhoneNumber();
|
||||||
|
this.surname = student.getSurname();
|
||||||
|
this.categories = student.getCategories().stream().map(CategoryDto::new).toList();
|
||||||
|
this.drivingSchool = student.getDrivingSchool() != null ? new DrivingSchoolWithoutStudDto(student.getDrivingSchool()) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPhoneNumber() {
|
||||||
|
return phoneNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CategoryDto> getCategories() {
|
||||||
|
return categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrivingSchoolWithoutStudDto getDrivingSchool() {
|
||||||
|
return drivingSchool;
|
||||||
|
}
|
||||||
|
}
|
71
src/main/java/ru/ulstu/is/cbapp/models/Category.java
Normal file
71
src/main/java/ru/ulstu/is/cbapp/models/Category.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package ru.ulstu.is.cbapp.models;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Category {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long Id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
//mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс
|
||||||
|
@ManyToMany(mappedBy = "categories", cascade = {CascadeType.REMOVE}, fetch = FetchType.EAGER)
|
||||||
|
private Set<Student> students = new HashSet<>();
|
||||||
|
|
||||||
|
public Category() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Category(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Student> getStudents() {
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudents(Set<Student> students) {
|
||||||
|
this.students = students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addNewStudent(Student e) {
|
||||||
|
students.add(e);
|
||||||
|
if (!e.getCategories().contains(this)) {
|
||||||
|
e.addNewCategory(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void deleteStudent(Student e) {
|
||||||
|
students.remove(e);
|
||||||
|
if (e.getCategories().contains(this)) {
|
||||||
|
e.removeCategory(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Category student = (Category) o;
|
||||||
|
return Objects.equals(Id, student.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(Id);
|
||||||
|
}
|
||||||
|
}
|
79
src/main/java/ru/ulstu/is/cbapp/models/DrivingSchool.java
Normal file
79
src/main/java/ru/ulstu/is/cbapp/models/DrivingSchool.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package ru.ulstu.is.cbapp.models;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class DrivingSchool {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long Id;
|
||||||
|
@Column(unique = true)
|
||||||
|
private String name;
|
||||||
|
@OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
|
||||||
|
private Set<Student> students = new HashSet<>();
|
||||||
|
|
||||||
|
public DrivingSchool(String name, Set<Student> students) {
|
||||||
|
this.name = name;
|
||||||
|
this.students = students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrivingSchool(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrivingSchool() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addNewStudent(Student student) {
|
||||||
|
students.add(student);
|
||||||
|
student.setDrivingSchool(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteStudent(Student student) {
|
||||||
|
students.remove(student);
|
||||||
|
student.deleteDrivingSchool();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Student> getStudents() {
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudents(Set<Student> students) {
|
||||||
|
this.students = students;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
DrivingSchool drivingSchool = (DrivingSchool) o;
|
||||||
|
return Objects.equals(Id, drivingSchool.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "DrivingSchool{" +
|
||||||
|
"Id=" + Id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", students=" + students +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
113
src/main/java/ru/ulstu/is/cbapp/models/Student.java
Normal file
113
src/main/java/ru/ulstu/is/cbapp/models/Student.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package ru.ulstu.is.cbapp.models;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Student {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
private String phoneNumber;
|
||||||
|
private String name;
|
||||||
|
private String surname;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
private DrivingSchool drivingSchool;
|
||||||
|
|
||||||
|
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
|
//@JoinTable(name="CATEGORY_STUDENT",
|
||||||
|
//joinColumns = {@JoinColumn(name="student_id")},
|
||||||
|
//inverseJoinColumns = {@JoinColumn(name="category_id")})
|
||||||
|
private Set<Category> categories = new HashSet<>();
|
||||||
|
|
||||||
|
|
||||||
|
public Student(String surname, String name, String phoneNumber) {
|
||||||
|
this.phoneNumber = phoneNumber;
|
||||||
|
this.name = name;
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPhoneNumber() {
|
||||||
|
return phoneNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPhoneNumber(String phoneNumber) {
|
||||||
|
this.phoneNumber = phoneNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Category> getCategories() {
|
||||||
|
return categories.stream().toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrivingSchool getDrivingSchool() {
|
||||||
|
return drivingSchool;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDrivingSchool(DrivingSchool drivingSchool) {
|
||||||
|
this.drivingSchool = drivingSchool;
|
||||||
|
if (!drivingSchool.getStudents().contains(this)) {
|
||||||
|
drivingSchool.addNewStudent(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteDrivingSchool() {
|
||||||
|
if (drivingSchool.getStudents().contains(this)) {
|
||||||
|
drivingSchool.deleteStudent(this);
|
||||||
|
}
|
||||||
|
this.drivingSchool = null;
|
||||||
|
this.categories.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addNewCategory(Category p) {
|
||||||
|
categories.add(p);
|
||||||
|
if (!p.getStudents().contains(this)) {
|
||||||
|
p.addNewStudent(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeCategory(Category p) {
|
||||||
|
categories.remove(p);
|
||||||
|
if (p.getStudents().contains(this)) {
|
||||||
|
p.deleteStudent(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Student e = (Student) o;
|
||||||
|
return Objects.equals(id, e.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package ru.ulstu.is.cbapp.models;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="student_categories")
|
||||||
|
@IdClass(StudentCategoriesId.class)
|
||||||
|
public class StudentCategories {
|
||||||
|
|
||||||
|
public Student getStudent() {
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudent(Student student) {
|
||||||
|
this.student = student;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Category getCategory() {
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCategory(Category category) {
|
||||||
|
this.category = category;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name="students_id")
|
||||||
|
private Student student;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name="categories_id")
|
||||||
|
private Category category;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package ru.ulstu.is.cbapp.models;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class StudentCategoriesId implements Serializable {
|
||||||
|
private Long student;
|
||||||
|
private Long category;
|
||||||
|
|
||||||
|
public Long getStudent() {
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudent(Long student) {
|
||||||
|
this.student = student;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCategory() {
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCategory(Long category) {
|
||||||
|
this.category = category;
|
||||||
|
}
|
||||||
|
}
|
69
src/main/java/ru/ulstu/is/cbapp/service/CategoryService.java
Normal file
69
src/main/java/ru/ulstu/is/cbapp/service/CategoryService.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package ru.ulstu.is.cbapp.service;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.dao.CategoryRepository;
|
||||||
|
import ru.ulstu.is.cbapp.dto.GroupedStudAndCategoryDto;
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
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 CategoryService {
|
||||||
|
|
||||||
|
private CategoryRepository categoryRepository;
|
||||||
|
|
||||||
|
public CategoryService(CategoryRepository categoryRepository) {
|
||||||
|
this.categoryRepository = categoryRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Category addCategory(String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Student's data is null or empty");
|
||||||
|
}
|
||||||
|
final Category category = new Category(name);
|
||||||
|
categoryRepository.save(category);
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Category findCategory(Long id) {
|
||||||
|
final Category category = categoryRepository.findById(id).orElse(null);
|
||||||
|
if (category == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Category with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Category> findAllCategories() {
|
||||||
|
return categoryRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Category updateCategory(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Student's data is null or empty");
|
||||||
|
}
|
||||||
|
final Category currentCategory = findCategory(id);
|
||||||
|
currentCategory.setName(name);
|
||||||
|
|
||||||
|
return categoryRepository.save(currentCategory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Category deleteCategory(Long id) {
|
||||||
|
final Category p = findCategory(id);
|
||||||
|
categoryRepository.delete(p);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllCategories() {
|
||||||
|
categoryRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package ru.ulstu.is.cbapp.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulstu.is.cbapp.dao.StudentCategoriesRepository;
|
||||||
|
import ru.ulstu.is.cbapp.dto.GroupedStudAndCategoryDto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CategoryStudentService {
|
||||||
|
|
||||||
|
private StudentCategoriesRepository studentCategoriesRepository;
|
||||||
|
|
||||||
|
public CategoryStudentService(StudentCategoriesRepository studentCategoriesRepository) {
|
||||||
|
this.studentCategoriesRepository = studentCategoriesRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<GroupedStudAndCategoryDto> getGroupedStudAndCategory() {
|
||||||
|
return studentCategoriesRepository.getGroupedStudAndCategory();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package ru.ulstu.is.cbapp.service;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.dao.DrivingSchoolRepository;
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
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 DrivingSchoolService {
|
||||||
|
|
||||||
|
private DrivingSchoolRepository drivingSchoolRepository;
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
|
public DrivingSchoolService(DrivingSchoolRepository drivingSchoolRepository, StudentService studentService) {
|
||||||
|
this.drivingSchoolRepository = drivingSchoolRepository;
|
||||||
|
this.studentService = studentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public DrivingSchool addDrivingSchool(String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Student name is null or empty");
|
||||||
|
}
|
||||||
|
final DrivingSchool drivingSchool = new DrivingSchool(name);
|
||||||
|
drivingSchoolRepository.save(drivingSchool);
|
||||||
|
return drivingSchool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public DrivingSchool findDrivingSchool(Long id) {
|
||||||
|
final DrivingSchool drivingSchool = drivingSchoolRepository.findById(id).orElse(null);
|
||||||
|
if (drivingSchool == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("DrivingSchool with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return drivingSchool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<DrivingSchool> findAllDrivingSchools() {
|
||||||
|
return drivingSchoolRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public DrivingSchool updateDrivingSchool(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("DrivingSchool name is null or empty");
|
||||||
|
}
|
||||||
|
final DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
currentDrivingSchool.setName(name);
|
||||||
|
|
||||||
|
return drivingSchoolRepository.save(currentDrivingSchool);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public DrivingSchool deleteDrivingSchool(Long id) {
|
||||||
|
final DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
drivingSchoolRepository.delete(currentDrivingSchool);
|
||||||
|
return currentDrivingSchool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllDrivingSchools() {
|
||||||
|
drivingSchoolRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student addNewStudent(Long id, Student student) {
|
||||||
|
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
currentDrivingSchool.addNewStudent(student);
|
||||||
|
drivingSchoolRepository.save(currentDrivingSchool);
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteStudent(Long id, Student student) {
|
||||||
|
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
currentDrivingSchool.deleteStudent(student);
|
||||||
|
}
|
||||||
|
}
|
111
src/main/java/ru/ulstu/is/cbapp/service/StudentService.java
Normal file
111
src/main/java/ru/ulstu/is/cbapp/service/StudentService.java
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
package ru.ulstu.is.cbapp.service;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.dao.StudentRepository;
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
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 StudentService {
|
||||||
|
|
||||||
|
private StudentRepository studentRepository;
|
||||||
|
|
||||||
|
public StudentService(StudentRepository studentRepository) {
|
||||||
|
this.studentRepository = studentRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student addStudent(String surname, String name, String phoneNumber) {
|
||||||
|
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
|
||||||
|
throw new IllegalArgumentException("Student's data is null or empty");
|
||||||
|
}
|
||||||
|
final Student student = new Student(surname, name, phoneNumber);
|
||||||
|
studentRepository.save(student);
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student findStudent(Long id) {
|
||||||
|
final Student student = studentRepository.findById(id).orElse(null);
|
||||||
|
if (student == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Student> findAllStudents() {
|
||||||
|
return studentRepository.findAll();
|
||||||
|
}
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Student> findAllFreeStudents() {
|
||||||
|
return studentRepository.findByDrivingSchoolIsNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student updateStudent(Long id, String surname, String name, String phoneNumber) {
|
||||||
|
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
|
||||||
|
throw new IllegalArgumentException("Student's data is null or empty");
|
||||||
|
}
|
||||||
|
final Student currentStudent = findStudent(id);
|
||||||
|
currentStudent.setName(name);
|
||||||
|
currentStudent.setSurname(surname);
|
||||||
|
currentStudent.setPhoneNumber(phoneNumber);
|
||||||
|
|
||||||
|
return studentRepository.save(currentStudent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student deleteStudent(Long id) {
|
||||||
|
final Student student = findStudent(id);
|
||||||
|
studentRepository.delete(student);
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllStudents() {
|
||||||
|
studentRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void addDrivingSchool(Long id, DrivingSchool c) {
|
||||||
|
final Student student = findStudent(id);
|
||||||
|
student.setDrivingSchool(c);
|
||||||
|
studentRepository.save(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteDrivingSchool(Long id) {
|
||||||
|
final Student student = findStudent(id);
|
||||||
|
student.deleteDrivingSchool();
|
||||||
|
studentRepository.save(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student addCategory(Long id, Category p) {
|
||||||
|
Student e = findStudent(id);
|
||||||
|
e.addNewCategory(p);
|
||||||
|
System.out.println(e.getCategories().size());
|
||||||
|
return studentRepository.save(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Student deleteCategory(Long id, Category p) {
|
||||||
|
Student e = findStudent(id);
|
||||||
|
e.removeCategory(p);
|
||||||
|
return studentRepository.save(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Student> getStudentsByCategory(Category p) {
|
||||||
|
List<Student> students = studentRepository.findByCategories_Id(p.getId());
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1 +1,11 @@
|
|||||||
|
spring.main.banner-mode=off
|
||||||
|
#server.port=8080
|
||||||
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.h2.console.enabled=true
|
||||||
|
spring.h2.console.settings.trace=false
|
||||||
|
spring.h2.console.settings.web-allow-others=false
|
||||||
|
55
src/test/java/ru/ulstu/is/cbapp/CategoryServiceTests.java
Normal file
55
src/test/java/ru/ulstu/is/cbapp/CategoryServiceTests.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package ru.ulstu.is.cbapp;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryService;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class CategoryServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private CategoryService categoryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
|
Category p = categoryService.addCategory("Category");
|
||||||
|
Student e = studentService.addStudent("1", "2", "33");
|
||||||
|
|
||||||
|
studentService.addCategory(e.getId(), p);
|
||||||
|
Assertions.assertTrue(studentService.findStudent(e.getId()).getCategories().contains(p));
|
||||||
|
Assertions.assertTrue(categoryService.findCategory(p.getId()).getStudents().contains(e));
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
|
Category p = categoryService.addCategory("Category");
|
||||||
|
Student e = studentService.addStudent("1", "2", "33");
|
||||||
|
|
||||||
|
studentService.addCategory(e.getId(), p);
|
||||||
|
studentService.deleteCategory(e.getId(), p);
|
||||||
|
Assertions.assertFalse(studentService.findStudent(e.getId()).getCategories().contains(p));
|
||||||
|
Assertions.assertFalse(categoryService.findCategory(p.getId()).getStudents().contains(e));
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package ru.ulstu.is.cbapp;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class CbappApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
116
src/test/java/ru/ulstu/is/cbapp/DrivingSchoolServiceTests.java
Normal file
116
src/test/java/ru/ulstu/is/cbapp/DrivingSchoolServiceTests.java
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package ru.ulstu.is.cbapp;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
||||||
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class DrivingSchoolServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private DrivingSchoolService drivingSchoolService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAllDrivingSchools() {
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
int n = 3;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
String name = "DrivingSchool" + i;
|
||||||
|
drivingSchoolService.addDrivingSchool(name);
|
||||||
|
}
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
Assertions.assertEquals(drivingSchoolService.findAllDrivingSchools().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteDrivingSchool() {
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
String name = "DrivingSchool1";
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool(name);
|
||||||
|
drivingSchoolService.deleteDrivingSchool(c.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> {
|
||||||
|
drivingSchoolService.findDrivingSchool(c.getId());});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddDrivingSchool() {
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
String name = "DrivingSchool1";
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool(name);
|
||||||
|
Assertions.assertNotNull(drivingSchoolService.findDrivingSchool(c.getId()));
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateDrivingSchool() {
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
String name = "DrivingSchool1";
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool(name);
|
||||||
|
String name2 = "DrivingSchool2";
|
||||||
|
drivingSchoolService.updateDrivingSchool(c.getId(), name2);
|
||||||
|
Assertions.assertNotNull(drivingSchoolService.findDrivingSchool(c.getId()));
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAllDrivingSchools() {
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
int n = 3;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
String name = "DrivingSchool" + i;
|
||||||
|
drivingSchoolService.addDrivingSchool(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assertions.assertEquals(drivingSchoolService.findAllDrivingSchools().size(), n);
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddStudent() {
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
|
||||||
|
final String name = "DrivingSchool";
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool(name);
|
||||||
|
Student newStudent = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
drivingSchoolService.addNewStudent(c.getId(), newStudent);
|
||||||
|
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(c.getId()).getStudents().contains(newStudent));
|
||||||
|
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
|
||||||
|
final String name = "DrivingSchool";
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool(name);
|
||||||
|
Student newStudent = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
|
||||||
|
drivingSchoolService.addNewStudent(c.getId(), newStudent);
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(c.getId()).getStudents().contains(newStudent));
|
||||||
|
|
||||||
|
drivingSchoolService.deleteStudent(c.getId(), newStudent);
|
||||||
|
Assertions.assertFalse(drivingSchoolService.findDrivingSchool(c.getId()).getStudents().contains(newStudent));
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
123
src/test/java/ru/ulstu/is/cbapp/StudentServiceTests.java
Normal file
123
src/test/java/ru/ulstu/is/cbapp/StudentServiceTests.java
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
package ru.ulstu.is.cbapp;
|
||||||
|
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
||||||
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryService;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class StudentServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DrivingSchoolService drivingSchoolService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CategoryService categoryService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
|
||||||
|
studentService.addStudent("name", "surname", "111");
|
||||||
|
Assertions.assertEquals(1, studentService.findAllStudents().size());
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
final String newName = "name";
|
||||||
|
|
||||||
|
Student e = studentService.addStudent("surname", "name", "111");
|
||||||
|
studentService.updateStudent(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
|
||||||
|
Assertions.assertEquals(newName, studentService.findStudent(e.getId()).getName());
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
|
||||||
|
Student e = studentService.addStudent("name", "surname", "111");
|
||||||
|
studentService.deleteStudent(e.getId());
|
||||||
|
Assertions.assertEquals(0, studentService.findAllStudents().size());
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAllStudent() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
int n = 3;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
studentService.addStudent("name", "surname", "111");
|
||||||
|
}
|
||||||
|
Assertions.assertEquals(n, studentService.findAllStudents().size());
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddDrivingSchool() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
Student e = studentService.addStudent("name", "surname", "111");
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool("Comp");
|
||||||
|
drivingSchoolService.addNewStudent(c.getId(), e);
|
||||||
|
Assertions.assertEquals(c, studentService.findStudent(e.getId()).getDrivingSchool());
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteFromDrivingSchool() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
Student e = studentService.addStudent("name", "surname", "111");
|
||||||
|
DrivingSchool c = drivingSchoolService.addDrivingSchool("Comp");
|
||||||
|
|
||||||
|
studentService.addDrivingSchool(e.getId(), c);
|
||||||
|
Assertions.assertEquals(c, studentService.findStudent(e.getId()).getDrivingSchool());
|
||||||
|
|
||||||
|
studentService.deleteDrivingSchool(e.getId());
|
||||||
|
Assertions.assertNull(studentService.findStudent(e.getId()).getDrivingSchool());
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByCategory() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
|
Category p = categoryService.addCategory("Category 1");
|
||||||
|
Category p2 = categoryService.addCategory("Category 2");
|
||||||
|
|
||||||
|
final int n = 10;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
Student e = studentService.addStudent("1111", "name" + i, "surname");
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
studentService.addCategory(e.getId(), p);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
studentService.addCategory(e.getId(), p2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assertions.assertEquals(n / 2, studentService.getStudentsByCategory(p).size());
|
||||||
|
Assertions.assertEquals(n / 2, studentService.getStudentsByCategory(p2).size());
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
}
|
||||||
|
}
|
BIN
Отчеты/1-Жимолостнова Анна Васильевна.docx
Normal file
BIN
Отчеты/1-Жимолостнова Анна Васильевна.docx
Normal file
Binary file not shown.
BIN
Отчеты/2 - Жимолостнова Анна Васильевна.docx
Normal file
BIN
Отчеты/2 - Жимолостнова Анна Васильевна.docx
Normal file
Binary file not shown.
BIN
Отчеты/3 - Жимолостнова Анна Васильевна.docx
Normal file
BIN
Отчеты/3 - Жимолостнова Анна Васильевна.docx
Normal file
Binary file not shown.
BIN
Отчеты/4 - Жимолостнова Анна Васильевна.docx
Normal file
BIN
Отчеты/4 - Жимолостнова Анна Васильевна.docx
Normal file
Binary file not shown.
BIN
Отчеты/5 - Жимолостнова Анна Васильевна.docx
Normal file
BIN
Отчеты/5 - Жимолостнова Анна Васильевна.docx
Normal file
Binary file not shown.
BIN
Отчеты/6 - Жимолостнова Анна Васильевна.docx
Normal file
BIN
Отчеты/6 - Жимолостнова Анна Васильевна.docx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user