Download src/modules/users/users.controller.js from moustafa7/gymco: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/spaces/moustafa7/gymco/resolve/main/src/modules/users/users.controller.js
- Command line
-
hf download hf://spaces/moustafa7/gymco/src/modules/users/users.controller.js
-
curl -L -o users.controller.js https://huggingface.co/spaces/moustafa7/gymco/resolve/main/src/modules/users/users.controller.js
1.56 kB
| import { ApiError } from '../../common/errors/ApiError.js'; | |
| import { asyncHandler } from '../../common/middleware/asyncHandler.js'; | |
| import { getPagination, paginated } from '../../common/utils/pagination.js'; | |
| import { hashPassword } from '../../common/utils/password.js'; | |
| import { created, ok } from '../../common/utils/response.js'; | |
| import { usersRepository } from './users.repository.js'; | |
| export const usersController = { | |
| list: asyncHandler(async (req, res) => { | |
| const pagination = getPagination(req.query); | |
| const [items, total] = await usersRepository.list({ gymId: req.gymId, search: req.query.search, role: req.query.role, ...pagination }); | |
| ok(res, paginated(items, total, pagination.page, pagination.limit)); | |
| }), | |
| get: asyncHandler(async (req, res) => { | |
| const user = await usersRepository.findById(req.gymId, req.params.id); | |
| if (!user) throw ApiError.notFound('User not found'); | |
| ok(res, user); | |
| }), | |
| create: asyncHandler(async (req, res) => { | |
| const password = await hashPassword(req.body.password); | |
| created(res, await usersRepository.create({ ...req.body, password, gymId: req.gymId }), 'User created'); | |
| }), | |
| update: asyncHandler(async (req, res) => { | |
| const data = { ...req.body }; | |
| if (data.password) data.password = await hashPassword(data.password); | |
| ok(res, await usersRepository.update(req.gymId, req.params.id, data), 'User updated'); | |
| }), | |
| remove: asyncHandler(async (req, res) => { | |
| await usersRepository.softDelete(req.gymId, req.params.id); | |
| ok(res, null, 'User deleted'); | |
| }), | |
| }; | |