150 lines
4.1 KiB
TypeScript
150 lines
4.1 KiB
TypeScript
import Role from "../../models/role.model";
|
|
import { ApiErrorHandler } from "../../error/api-error.handler";
|
|
import User from "../../models/user.model";
|
|
import type { IUserDto } from "./user.dto";
|
|
import type { Order } from "sequelize";
|
|
import type {
|
|
FilterQueryParamType,
|
|
RangeQueryParamType,
|
|
SortQueryParamType
|
|
} from "../query-param.types";
|
|
import type { NextFunction, Request, Response } from "express";
|
|
|
|
class UserController {
|
|
getAll = async (
|
|
req: Request,
|
|
res: Response<{ data: User[]; total: number }>,
|
|
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 users = await User.findAndCountAll({
|
|
attributes: { exclude: ["password"] },
|
|
include: Role,
|
|
offset: range[0],
|
|
limit: range[1] - range[0] + 1,
|
|
order: [sort] as Order
|
|
});
|
|
|
|
return res.json({ data: users.rows, total: users.count });
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
|
|
getMany = async (req: Request, res: Response<{ data: User[] }>, next: NextFunction) => {
|
|
try {
|
|
const filter: FilterQueryParamType = req.query.filter
|
|
? JSON.parse(req.query.filter.toString())
|
|
: { ids: [] };
|
|
|
|
const users = await User.findAll({
|
|
where: { id: filter.ids as number[] }
|
|
});
|
|
return res.json({ data: users });
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
|
|
create = async (
|
|
req: Request<object, object, IUserDto>,
|
|
res: Response<User>,
|
|
next: NextFunction
|
|
) => {
|
|
try {
|
|
const { nickname, avatar, email, password, role_id } = req.body;
|
|
const user = await User.create({ nickname, avatar, email, password, role_id });
|
|
return res.json(user);
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
|
|
getById = async (
|
|
req: Request<{ id: number }>,
|
|
res: Response<User>,
|
|
next: NextFunction
|
|
) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const user = await User.findByPk(id);
|
|
if (!user) return next(ApiErrorHandler.notFound("Такого пользователя не найдено"));
|
|
|
|
return res.json(user);
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
|
|
update = async (
|
|
req: Request<{ id: number }, object, IUserDto>,
|
|
res: Response<User>,
|
|
next: NextFunction
|
|
) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { nickname, avatar, email, password, role_id } = req.body;
|
|
|
|
const user = await User.findByPk(id);
|
|
if (!user) return next(ApiErrorHandler.internal("Такого пользователя не найдено"));
|
|
|
|
user.nickname = nickname;
|
|
user.avatar = avatar;
|
|
user.email = email;
|
|
user.password = password;
|
|
user.role_id = role_id;
|
|
await user.save();
|
|
|
|
return res.json(user);
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
|
|
delete = async (
|
|
req: Request<{ id: number }>,
|
|
res: Response<User>,
|
|
next: NextFunction
|
|
) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const user = await User.findByPk(id);
|
|
if (!user) return next(ApiErrorHandler.internal("Такого пользователя не найдено"));
|
|
|
|
await user.destroy();
|
|
|
|
return res.json(user);
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
|
|
deleteMany = async (
|
|
req: Request,
|
|
res: Response<{ data: User[] }>,
|
|
next: NextFunction
|
|
) => {
|
|
try {
|
|
const filter: FilterQueryParamType = req.query.filter
|
|
? JSON.parse(req.query.filter.toString())
|
|
: {};
|
|
const users = await User.findAll({ where: filter });
|
|
|
|
User.destroy({ where: filter });
|
|
|
|
return res.json({ data: users });
|
|
} catch (error) {
|
|
next(ApiErrorHandler.internal((error as Error).message));
|
|
}
|
|
};
|
|
}
|
|
|
|
export default new UserController();
|