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();