import { ApiErrorHandler } from "../../error/api-error.handler"; import Message from "../../models/message.model"; import type { IMessageDto } from "./message.dto"; import type { Order } from "sequelize"; import type { NextFunction, Request, Response } from "express"; import type { FilterQueryParamType, RangeQueryParamType, SortQueryParamType } from "../query-param.types"; class MessageController { 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 messages = await Message.findAndCountAll({ offset: range[0], limit: range[1] - range[0] + 1, order: [sort] as Order }); return res.json({ data: messages.rows, total: messages.count }); } catch (error) { next(ApiErrorHandler.internal((error as Error).message)); } }; getMany = async ( req: Request, res: Response<{ data: Message[] }>, next: NextFunction ) => { try { const filter: FilterQueryParamType = req.query.filter ? JSON.parse(req.query.filter.toString()) : { ids: [] }; const messages = await Message.findAll({ where: { id: filter.ids as number[] } }); return res.json({ data: messages }); } catch (error) { next(ApiErrorHandler.internal((error as Error).message)); } }; create = async ( req: Request, res: Response, next: NextFunction ) => { try { const { text, user_id, thread_id } = req.body; const message = await Message.create({ text, user_id, thread_id }); return res.json(message); } 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 message = await Message.findByPk(id); if (!message) return next(ApiErrorHandler.notFound("Такой роли не найдено")); return res.json(message); } catch (error) { next(ApiErrorHandler.internal((error as Error).message)); } }; update = async ( req: Request<{ id: number }, object, IMessageDto>, res: Response, next: NextFunction ) => { try { const { id } = req.params; const { text, user_id, thread_id } = req.body; const message = await Message.findByPk(id); if (!message) return next(ApiErrorHandler.internal("Такой роли не найдено")); message.text = text; message.user_id = user_id; message.thread_id = thread_id; await message.save(); return res.json(message); } 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 message = await Message.findByPk(id); if (!message) return next(ApiErrorHandler.internal("Такой роли не найдено")); await message.destroy(); return res.json(message); } catch (error) { next(ApiErrorHandler.internal((error as Error).message)); } }; deleteMany = async ( req: Request, res: Response<{ data: Message[] }>, next: NextFunction ) => { try { const filter: FilterQueryParamType = req.query.filter ? JSON.parse(req.query.filter.toString()) : {}; const messages = await Message.findAll({ where: filter }); Message.destroy({ where: filter }); return res.json({ data: messages }); } catch (error) { next(ApiErrorHandler.internal((error as Error).message)); } }; } export default new MessageController();