Subd/server/controllers/thread/thread.controller.ts

146 lines
4.0 KiB
TypeScript
Raw Permalink Normal View History

2024-04-23 12:08:46 +04:00
import { type Order } from "sequelize";
import Section from "../../models/section.model";
import { ApiErrorHandler } from "../../error/api-error.handler";
import Thread from "../../models/thread.model";
import type { IThreadDto } from "./thread.dto";
import type { NextFunction, Request, Response } from "express";
import type {
FilterQueryParamType,
RangeQueryParamType,
SortQueryParamType
} from "../query-param.types";
class ThreadController {
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 threads = await Thread.findAndCountAll({
offset: range[0],
limit: range[1] - range[0] + 1,
order: [sort] as Order
});
return res.json({ data: threads.rows, total: threads.count });
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
};
getMany = async (
req: Request,
res: Response<{ data: Thread[] }>,
next: NextFunction
) => {
try {
const filter: FilterQueryParamType = req.query.filter
? JSON.parse(req.query.filter.toString())
: { ids: [] };
const threads = await Thread.findAll({
where: { id: filter.ids as number[] }
});
return res.json({ data: threads });
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
};
create = async (
req: Request<object, object, IThreadDto>,
res: Response<Thread>,
next: NextFunction
) => {
try {
const { name, section_id, creator_id } = req.body;
const thread = await Thread.create({ name, section_id, creator_id });
return res.json(thread);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
};
getById = async (
req: Request<{ id: number }>,
res: Response<Thread>,
next: NextFunction
) => {
try {
const { id } = req.params;
const thread = await Thread.findByPk(id);
if (!thread) return next(ApiErrorHandler.notFound("Такой роли не найдено"));
return res.json(thread);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
};
update = async (
req: Request<{ id: number }, object, IThreadDto>,
res: Response<Thread>,
next: NextFunction
) => {
try {
const { id } = req.params;
const { name, section_id, creator_id } = req.body;
const thread = await Thread.findByPk(id);
if (!thread) return next(ApiErrorHandler.internal("Такой роли не найдено"));
thread.name = name;
thread.section_id = section_id;
thread.creator_id = creator_id;
await thread.save();
return res.json(thread);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
};
delete = async (
req: Request<{ id: number }>,
res: Response<Thread>,
next: NextFunction
) => {
try {
const { id } = req.params;
const thread = await Thread.findByPk(id);
if (!thread) return next(ApiErrorHandler.internal("Такой роли не найдено"));
await thread.destroy();
return res.json(thread);
} 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 ThreadController();