From 14a7fdbf45dccba3bdc3726508d9a805620c41bf Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Mon, 25 Mar 2024 22:18:41 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=83=20=D0=BD=D0=B0=D0=B4=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D0=BE=D0=BC=20?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20Section?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/App.tsx | 9 ++ client/src/components/Section/Create.tsx | 16 ++ client/src/components/Section/Edit.tsx | 22 +++ client/src/components/Section/List.tsx | 16 ++ .../controllers/section/section.controller.ts | 142 ++++++++++++++++++ server/controllers/section/section.dto.ts | 4 + server/db.ts | 5 +- server/models/models.constants.js | 8 - server/models/models.js | 0 server/models/section.model.ts | 51 +++++++ server/routes/routes.ts | 2 + server/routes/routes/message.router.js | 0 server/routes/routes/role.router.ts | 2 +- server/routes/routes/section.router.js | 0 server/routes/routes/section.router.ts | 14 ++ server/routes/routes/thread.router.js | 0 16 files changed, 280 insertions(+), 11 deletions(-) create mode 100644 client/src/components/Section/Create.tsx create mode 100644 client/src/components/Section/Edit.tsx create mode 100644 client/src/components/Section/List.tsx create mode 100644 server/controllers/section/section.controller.ts create mode 100644 server/controllers/section/section.dto.ts delete mode 100644 server/models/models.constants.js delete mode 100644 server/models/models.js create mode 100644 server/models/section.model.ts delete mode 100644 server/routes/routes/message.router.js delete mode 100644 server/routes/routes/section.router.js create mode 100644 server/routes/routes/section.router.ts delete mode 100644 server/routes/routes/thread.router.js diff --git a/client/src/App.tsx b/client/src/App.tsx index a4d3f4d..6ac705f 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -6,6 +6,9 @@ import RoleEdit from "./components/Role/Edit"; import UsersList from "./components/User/List"; import UserCreate from "./components/User/Create"; import UserEdit from "./components/User/Edit"; +import SectionsList from "./components/Section/List"; +import SectionCreate from "./components/Section/Create"; +import SectionEdit from "./components/Section/Edit"; function App() { return ( @@ -22,6 +25,12 @@ function App() { create={} edit={} /> + } + create={} + edit={} + /> ); } diff --git a/client/src/components/Section/Create.tsx b/client/src/components/Section/Create.tsx new file mode 100644 index 0000000..f536431 --- /dev/null +++ b/client/src/components/Section/Create.tsx @@ -0,0 +1,16 @@ +import { Create, ReferenceInput, SelectInput, SimpleForm, TextInput } from "react-admin"; + +function SectionCreate() { + return ( + + + + + + + + + ); +}; + +export default SectionCreate; \ No newline at end of file diff --git a/client/src/components/Section/Edit.tsx b/client/src/components/Section/Edit.tsx new file mode 100644 index 0000000..8ea06dd --- /dev/null +++ b/client/src/components/Section/Edit.tsx @@ -0,0 +1,22 @@ +import { Edit, ReferenceInput, SelectInput, SimpleForm, TextInput, useGetList, useRecordContext } from "react-admin"; +import Title from "../Title"; + +function SectionEdit() { + // const record = useRecordContext(); + // if (!record) return null; + + // const { data } = useGetList("sections", { id: record.id }); + + return ( + } actions={false}> + + + {/* */} + {/* */} + {/* */} + + + ); +} + +export default SectionEdit; diff --git a/client/src/components/Section/List.tsx b/client/src/components/Section/List.tsx new file mode 100644 index 0000000..7849ca0 --- /dev/null +++ b/client/src/components/Section/List.tsx @@ -0,0 +1,16 @@ +import { List, Datagrid, TextField, EditButton } from "react-admin"; + +function SectionsList() { + return ( + + + + + + + + + ); +}; + +export default SectionsList; \ No newline at end of file diff --git a/server/controllers/section/section.controller.ts b/server/controllers/section/section.controller.ts new file mode 100644 index 0000000..123567c --- /dev/null +++ b/server/controllers/section/section.controller.ts @@ -0,0 +1,142 @@ +import Section from "../../models/section.model"; +import { ApiErrorHandler } from "../../error/api-error.handler"; +import type { ISectionDto } from "./section.dto"; +import type { Order } from "sequelize"; +import type { NextFunction, Request, Response } from "express"; +import type { + FilterQueryParamType, + RangeQueryParamType, + SortQueryParamType +} from "../query-param.types"; + +class SectionController { + getAll = async (req: Request, res: Response, next: NextFunction) => { + try { + const range: RangeQueryParamType = req.query.range + ? JSON.parse(req.query.range.toString()) + : [0, 10]; + const sort: SortQueryParamType = req.query.sort + ? JSON.parse(req.query.sort.toString()) + : ["id", "ASC"]; + + const sections = await Section.findAndCountAll({ + offset: range[0], + limit: range[1] - range[0] + 1, + order: [sort] as Order + }); + + return res.json({ data: sections.rows, total: sections.count }); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; + + getMany = async ( + req: Request, + res: Response<{ data: Section[] }>, + next: NextFunction + ) => { + try { + const filter: FilterQueryParamType = req.query.filter + ? JSON.parse(req.query.filter.toString()) + : { ids: [] }; + + const sections = await Section.findAll({ + where: { id: filter.ids as number[] } + }); + return res.json({ data: sections }); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; + + create = async ( + req: Request, + res: Response
, + next: NextFunction + ) => { + try { + const { name, root_section_id } = req.body; + const section = await Section.create({ name, root_section_id }); + return res.json(section); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; + + getById = async ( + req: Request<{ id: number }>, + res: Response
, + next: NextFunction + ) => { + try { + const { id } = req.params; + const section = await Section.findByPk(id); + if (!section) return next(ApiErrorHandler.notFound("Такой роли не найдено")); + + return res.json(section); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; + + update = async ( + req: Request<{ id: number }, object, ISectionDto>, + res: Response
, + next: NextFunction + ) => { + try { + const { id } = req.params; + const { name } = req.body; + + const section = await Section.findByPk(id); + if (!section) return next(ApiErrorHandler.internal("Такой роли не найдено")); + + section.name = name; + await section.save(); + + return res.json(section); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; + + delete = async ( + req: Request<{ id: number }>, + res: Response
, + next: NextFunction + ) => { + try { + const { id } = req.params; + const section = await Section.findByPk(id); + if (!section) return next(ApiErrorHandler.internal("Такой роли не найдено")); + + await section.destroy(); + + return res.json(section); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; + + deleteMany = async ( + req: Request, + res: Response<{ data: Section[] }>, + next: NextFunction + ) => { + try { + const filter: FilterQueryParamType = req.query.filter + ? JSON.parse(req.query.filter.toString()) + : {}; + const sections = await Section.findAll({ where: filter }); + + Section.destroy({ where: filter }); + + return res.json({ data: sections }); + } catch (error) { + next(ApiErrorHandler.internal((error as Error).message)); + } + }; +} + +export default new SectionController(); diff --git a/server/controllers/section/section.dto.ts b/server/controllers/section/section.dto.ts new file mode 100644 index 0000000..dcd14c6 --- /dev/null +++ b/server/controllers/section/section.dto.ts @@ -0,0 +1,4 @@ +export interface ISectionDto { + name: string; + root_section_id: number | null; +} diff --git a/server/db.ts b/server/db.ts index 7e20526..e5617f5 100644 --- a/server/db.ts +++ b/server/db.ts @@ -2,6 +2,7 @@ import dotenv from "dotenv"; import { Sequelize } from "sequelize-typescript"; import Role from "./models/role.model"; import User from "./models/user.model"; +import Section from "./models/section.model"; dotenv.config(); @@ -12,5 +13,5 @@ export const sequelize = new Sequelize({ password: process.env.DB_PASSWORD, host: process.env.DB_HOST, port: process.env.DB_PORT, - models: [Role, User] -}); \ No newline at end of file + models: [Role, User, Section] +}); diff --git a/server/models/models.constants.js b/server/models/models.constants.js deleted file mode 100644 index c0ec85a..0000000 --- a/server/models/models.constants.js +++ /dev/null @@ -1,8 +0,0 @@ -import { DataTypes } from "sequelize"; - -export const customCreatedAndUpdatedFields = { - createdAt: "created_at", - updatedAt: "updated_at" -}; - -export const idSettingsObject = { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }; \ No newline at end of file diff --git a/server/models/models.js b/server/models/models.js deleted file mode 100644 index e69de29..0000000 diff --git a/server/models/section.model.ts b/server/models/section.model.ts new file mode 100644 index 0000000..eb62880 --- /dev/null +++ b/server/models/section.model.ts @@ -0,0 +1,51 @@ +import { + Table, + Column, + Model, + DataType, + Length, + UpdatedAt, + CreatedAt, + ForeignKey, + BelongsTo, + HasMany +} from "sequelize-typescript"; + +@Table({ + tableName: "sections", + modelName: "Section" +}) +export default class Section extends Model { + @Column({ + primaryKey: true, + autoIncrement: true, + type: DataType.INTEGER + }) + declare id: number; + + @Length({ min: 1 }) + @Column({ + allowNull: false, + defaultValue: "Unknown", + type: DataType.STRING + }) + declare name: string; + + @CreatedAt + declare created_at: Date; + + @UpdatedAt + declare updated_at: Date; + + @ForeignKey(() => Section) + @Column({ + type: DataType.INTEGER + }) + declare root_section_id: number; + + @BelongsTo(() => Section, "root_section_id") + declare root_section: Section; + + @HasMany(() => Section, "root_section_id") + declare inner_sections: Section[]; +} diff --git a/server/routes/routes.ts b/server/routes/routes.ts index 7059f55..fe79be3 100644 --- a/server/routes/routes.ts +++ b/server/routes/routes.ts @@ -1,8 +1,10 @@ import { Router } from "express"; import roleRouter from "./routes/role.router"; import userRouter from "./routes/user.router"; +import sectionRouter from "./routes/section.router"; export const router = Router(); router.use("/roles", roleRouter); router.use("/users", userRouter); +router.use("/sections", sectionRouter); diff --git a/server/routes/routes/message.router.js b/server/routes/routes/message.router.js deleted file mode 100644 index e69de29..0000000 diff --git a/server/routes/routes/role.router.ts b/server/routes/routes/role.router.ts index 6529cc5..f1c871d 100644 --- a/server/routes/routes/role.router.ts +++ b/server/routes/routes/role.router.ts @@ -11,4 +11,4 @@ roleRouter.put("/:id", RoleController.update); roleRouter.delete("/delete-many", RoleController.deleteMany); roleRouter.delete("/:id", RoleController.delete); -export default roleRouter; \ No newline at end of file +export default roleRouter; diff --git a/server/routes/routes/section.router.js b/server/routes/routes/section.router.js deleted file mode 100644 index e69de29..0000000 diff --git a/server/routes/routes/section.router.ts b/server/routes/routes/section.router.ts new file mode 100644 index 0000000..7aa4f73 --- /dev/null +++ b/server/routes/routes/section.router.ts @@ -0,0 +1,14 @@ +import { Router } from "express"; +import SectionController from "../../controllers/section/section.controller"; + +const sectionRouter = Router(); + +sectionRouter.post("/", SectionController.create); +sectionRouter.get("/", SectionController.getAll); +sectionRouter.get("/get-many", SectionController.getMany); +sectionRouter.get("/:id", SectionController.getById); +sectionRouter.put("/:id", SectionController.update); +sectionRouter.delete("/delete-many", SectionController.deleteMany); +sectionRouter.delete("/:id", SectionController.delete); + +export default sectionRouter; diff --git a/server/routes/routes/thread.router.js b/server/routes/routes/thread.router.js deleted file mode 100644 index e69de29..0000000