210 lines
6.2 KiB
TypeScript
210 lines
6.2 KiB
TypeScript
|
import { QueryTypes, type Order } from "sequelize";
|
|||
|
import Section from "../../models/section.model";
|
|||
|
import { ApiErrorHandler } from "../../error/api-error.handler";
|
|||
|
import { sequelize } from "../../db";
|
|||
|
import type { ISectionDto } from "./section.dto";
|
|||
|
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));
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
getFillteredList = async (req: Request, res: Response, next: NextFunction) => {
|
|||
|
try {
|
|||
|
// Сюда приходит id текущего товара при редактировании,
|
|||
|
// чтобы не возвращать его в списке
|
|||
|
// или же флаг onlyEndSections
|
|||
|
// const filter: { onlyEndSections: boolean } | { id: number } = req.query.filter
|
|||
|
// ? JSON.parse(req.query.filter.toString())
|
|||
|
// : {};
|
|||
|
|
|||
|
const currentSectionId: number = req.query.id
|
|||
|
? JSON.parse(req.query.id.toString())
|
|||
|
: -1;
|
|||
|
|
|||
|
const onlyEndSections: boolean = req.query.onlyEndSections
|
|||
|
? JSON.parse(req.query.onlyEndSections.toString())
|
|||
|
: false;
|
|||
|
|
|||
|
// 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"];
|
|||
|
|
|||
|
if (onlyEndSections === true) {
|
|||
|
const endSections = await sequelize.query(
|
|||
|
`
|
|||
|
SELECT * FROM sections
|
|||
|
WHERE id NOT IN (SELECT DISTINCT root_section_id FROM sections
|
|||
|
WHERE root_section_id IS NOT NULL);
|
|||
|
`,
|
|||
|
{ type: QueryTypes.SELECT }
|
|||
|
);
|
|||
|
return res.json(endSections);
|
|||
|
}
|
|||
|
|
|||
|
// const sections = await Section.findAndCountAll({
|
|||
|
// offset: range[0],
|
|||
|
// limit: range[1] - range[0] + 1,
|
|||
|
// order: [sort] as Order,
|
|||
|
// include: {
|
|||
|
// model: Thread,
|
|||
|
// required: false,
|
|||
|
// where: { id: null }
|
|||
|
// },
|
|||
|
// where: {
|
|||
|
// id: {
|
|||
|
// [Op.ne]: filter && "id" in filter ? filter.id : null
|
|||
|
// }
|
|||
|
// }
|
|||
|
// });
|
|||
|
|
|||
|
const sections = await sequelize.query(
|
|||
|
`SELECT * FROM sections WHERE id != ${currentSectionId} AND id NOT IN
|
|||
|
(SELECT DISTINCT section_id FROM threads) AND
|
|||
|
(root_section_id IS NULL OR root_section_id != ${currentSectionId});`,
|
|||
|
{ type: QueryTypes.SELECT }
|
|||
|
);
|
|||
|
|
|||
|
return res.json(sections);
|
|||
|
} 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<object, object, ISectionDto>,
|
|||
|
res: Response<Section>,
|
|||
|
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<Section>,
|
|||
|
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<Section>,
|
|||
|
next: NextFunction
|
|||
|
) => {
|
|||
|
try {
|
|||
|
const { id } = req.params;
|
|||
|
const { name, root_section_id } = req.body;
|
|||
|
|
|||
|
const section = await Section.findByPk(id);
|
|||
|
if (!section) return next(ApiErrorHandler.internal("Такой секции не найдено"));
|
|||
|
|
|||
|
section.name = name;
|
|||
|
section.root_section_id = root_section_id;
|
|||
|
await section.save();
|
|||
|
|
|||
|
return res.json(section);
|
|||
|
} catch (error) {
|
|||
|
next(ApiErrorHandler.internal((error as Error).message));
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
delete = async (
|
|||
|
req: Request<{ id: number }>,
|
|||
|
res: Response<Section>,
|
|||
|
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();
|