PIbd-23_Dyakonov_R_R_SUBD_P.../server/controllers/section/section.controller.ts

155 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Op, type Order } from "sequelize";
import Section from "../../models/section.model";
import { ApiErrorHandler } from "../../error/api-error.handler";
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 {
// Сюда приходит id текущего товара при редактировании,
// чтобы не возвращать его в списке
const filter: FilterQueryParamType = req.query.filter
? JSON.parse(req.query.filter.toString())
: { id: -1 };
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,
where: {
id: {
[Op.ne]: filter && filter.id ? filter.id : null
}
}
});
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<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();