From 3858a3c06ed76ab0b7dce7847cee816f742870de Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 2 Nov 2024 22:32:35 +0300 Subject: [PATCH] added new endpoint --- src/controllers/user.js | 8 ++++++++ src/routers/user.js | 2 ++ src/services/user.js | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/src/controllers/user.js b/src/controllers/user.js index ef22565..df73119 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -66,6 +66,14 @@ class UserController { return res.status(200).send(result); } + + async getById(req, res) { + const userId = req.params.userId + + let result = (await UserService.getById(userId)).username; + + return res.status(200).send(result); + } } export default new UserController() \ No newline at end of file diff --git a/src/routers/user.js b/src/routers/user.js index 8a91f39..e128183 100644 --- a/src/routers/user.js +++ b/src/routers/user.js @@ -11,5 +11,7 @@ UserRouter.get('/synchronize/:groupId', tryHandler(auth.authenticate), tryHandle UserRouter.post('/changeUsername', tryHandler(auth.authenticate), tryHandler(auth.requireUsername), tryHandler(UserController.changeUsername)); UserRouter.post('/changePassword', tryHandler(auth.authenticate), tryHandler(auth.requirePassword), tryHandler(UserController.changePassword)); UserRouter.get('/myGroups', tryHandler(auth.authenticate), tryHandler(UserController.getAllGroupsForUser)); +//Maybe I should add a middleware like "auth.usersShareGroups" +UserRouter.get('/byId/:userId', tryHandler(auth.authenticate), tryHandler(UserController.getById)) export default UserRouter; \ No newline at end of file diff --git a/src/services/user.js b/src/services/user.js index 545178a..f559832 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -43,6 +43,12 @@ class UserService { async getAllGroupsForUser(userId) { return (await db.query("SELECT groups FROM users WHERE id = $1", [userId])).rows[0].groups } + + async getById(userId) { + let res = (await db.query("SELECT * FROM users WHERE id = $1", [userId])) + if (res.rowCount == 0) throw new customError(`getById no user found`, responseCodes.responses.user.not_found) + return res.rows[0] + } } export default new UserService(); \ No newline at end of file