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

108 lines
3.5 KiB
TypeScript

import { NextFunction, Request, Response } from "express";
import Role from "../../models/role.model";
import { ApiErrorHandler } from "../../error/api-error.handler";
import { IRoleDto } from "./role.dto";
import { Order } from "sequelize";
type RangeQueryParamType = [number, number];
type SortQueryParamType = [string, string] | Array<[string, string]>;
type FilterQueryParamType = {
[key: string]: unknown
}
class RoleController {
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 roles = await Role.findAndCountAll({
offset: range[0],
limit: range[1] - range[0] + 1,
order: [sort] as Order
});
return res.json({ data: roles.rows, total: roles.count });
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
}
// getMany = async (req, res, next) => {
// try {
// const filter = JSON.parse(req.query.filter);
// const users = await User.findAll({ attributes: { exclude: ["slug"] }, where: { id: filter.ids } });
// return res.json({ data: users });
// } catch (error) {
// next(ApiErrorHandler.internal(error.message));
// }
// }
create = async (req: Request<{}, {}, IRoleDto>, res: Response<Role>, next: NextFunction) => {
try {
const { name } = req.body;
const role = await Role.create({ name: name.toUpperCase() });
return res.json(role);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
}
getById = async (req: Request<{ id: number }>, res: Response<Role>, next: NextFunction) => {
try {
const { id } = req.params;
const role = await Role.findByPk(id);
if (!role) return next(ApiErrorHandler.notFound("Такой роли не найдено"));
return res.json(role);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
}
update = async (req: Request<{ id: number }, {}, IRoleDto>, res: Response<Role>, next: NextFunction) => {
try {
const { id } = req.params;
const { name } = req.body;
const role = await Role.findByPk(id);
if (!role) return next(ApiErrorHandler.internal("Такой роли не найдено"));
role.name = name;
await role.save();
return res.json(role);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
}
delete = async (req: Request<{ id: number }>, res: Response<Role>, next: NextFunction) => {
try {
const { id } = req.params;
const role = await Role.findByPk(id);
if (!role) return next(ApiErrorHandler.internal("Такой роли не найдено"));
await role.destroy();
return res.json(role);
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
}
deleteMany = async (req: Request, res: Response<{data: Role[]}>, next: NextFunction) => {
try {
const filter: FilterQueryParamType = req.query.filter ? JSON.parse(req.query.filter.toString()) : {};
const roles = await Role.findAll({ where: filter });
Role.destroy({ where: filter });
return res.json({ data: roles });
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
}
}
export default new RoleController();