Subd/server/controllers/role/role.controller.ts

139 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-04-23 12:08:46 +04:00
import Role from "../../models/role.model";
import { ApiErrorHandler } from "../../error/api-error.handler";
import type { Order } from "sequelize";
import type { IRoleDto } from "./role.dto";
import type { NextFunction, Request, Response } from "express";
import type {
FilterQueryParamType,
RangeQueryParamType,
SortQueryParamType
} from "../query-param.types";
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: Request, res: Response<{ data: Role[] }>, next: NextFunction) => {
try {
const filter: FilterQueryParamType = req.query.filter
? JSON.parse(req.query.filter.toString())
: { ids: [] };
const roles = await Role.findAll({
where: { id: filter.ids as number[] }
});
return res.json({ data: roles });
} catch (error) {
next(ApiErrorHandler.internal((error as Error).message));
}
};
create = async (
req: Request<object, object, 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 }, object, 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();