From 947dc720a5204f8aeed2470b9b09bdc17ae63c05 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 18 Nov 2024 12:32:11 +0300 Subject: [PATCH] added rename endpoint for groups --- messages/en-US/msgs.json | 1 + messages/ru-RU/msgs.json | 1 + src/controllers/group.js | 12 ++++++++++++ src/response/responseCodes.js | 5 ++++- src/routers/group.js | 1 + src/services/group.js | 4 ++++ 6 files changed, 23 insertions(+), 1 deletion(-) diff --git a/messages/en-US/msgs.json b/messages/en-US/msgs.json index a1a79ae..df4060a 100644 --- a/messages/en-US/msgs.json +++ b/messages/en-US/msgs.json @@ -15,6 +15,7 @@ "group id not found": "Group with such ID not found!", "group not an owner": "You are not an owner of this group!", "group not a member": "You are not a member of this group!", + "name not specified": "New name of a group is not specified!", "abstract product not found": "Such abstract product is not found!", "abstract product invalid syntax": "Invalid syntax in one of user's parameters!", "barcode not found": "Such barcode not found!", diff --git a/messages/ru-RU/msgs.json b/messages/ru-RU/msgs.json index 28ba16f..371d466 100644 --- a/messages/ru-RU/msgs.json +++ b/messages/ru-RU/msgs.json @@ -15,6 +15,7 @@ "group id not found": "Группы с таким ID не существует!", "group not an owner": "Вы не владелец этой группы!", "group not a member": "Вы не участник этой группы!", + "name not specified": "Не указано новое имя группы!", "abstract product not found": "Такой абстрактный продукт не найден!", "abstract product invalid syntax": "Неправильный синткасис в одном из параметров абстрактного продукта!", "barcode not found": "Такой штрихкод не найден!", diff --git a/src/controllers/group.js b/src/controllers/group.js index c0d2b45..5d14179 100644 --- a/src/controllers/group.js +++ b/src/controllers/group.js @@ -5,6 +5,7 @@ import config from '../../config.json' with {type: "json"}; import log from '../utils/log.js'; import translate from '../utils/translate.js'; import responseCodes from '../response/responseCodes.js'; +import customError from '../response/customError.js'; const TAG = "/controllers/group.js"; @@ -75,6 +76,17 @@ class GroupController { return res.status(200).send(result.toString()) } + + async rename(req, res) { + const groupId = req.params.groupId; + const name = req.body.name; + + if (!name) throw new customError(`New group name is not specified`, responseCodes.responses.groups.name_not_specified); + + await GroupService.rename(groupId, name); + + return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); + } } export default new GroupController(); diff --git a/src/response/responseCodes.js b/src/response/responseCodes.js index 2f8e530..d2f8258 100644 --- a/src/response/responseCodes.js +++ b/src/response/responseCodes.js @@ -23,7 +23,8 @@ const responses = { name_not_found: "group name not found", id_not_found: "group id not found", not_an_owner: "group not an owner", - not_a_member: "group not a member" + not_a_member: "group not a member", + name_not_specified: "name not specified" }, abstractproducts: { not_found: "abstract product not found", @@ -141,6 +142,8 @@ const getHTTPCode = (type) => { return 403 case responses.groups.not_a_member: return 403 + case responses.groups.name_not_specified: + return 400 case responses.abstractproducts.not_found: return 404 diff --git a/src/routers/group.js b/src/routers/group.js index 9fb0150..3074df7 100644 --- a/src/routers/group.js +++ b/src/routers/group.js @@ -13,5 +13,6 @@ GroupRouter.get('/byName/:groupName', tryHandler(auth.authenticate), tryHandler( GroupRouter.get('/byId/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getById)) GroupRouter.get('/getUsers/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getUsersInGroup)) GroupRouter.get('/adminId/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(GroupController.getAdminId)) +GroupRouter.post('/rename/:groupId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.authorizeGroupOwner), tryHandler(GroupController.rename)); export default GroupRouter; \ No newline at end of file diff --git a/src/services/group.js b/src/services/group.js index 3a4536d..27d79f8 100644 --- a/src/services/group.js +++ b/src/services/group.js @@ -37,6 +37,10 @@ class GroupService { async getUsersInGroup(groupId) { return (await db.query("SELECT id FROM users WHERE $1 = ANY(groups)", [groupId])).rows.map((group) => group.id) } + + async rename(groupId, newName) { + await db.query("UPDATE groups SET name = $1 WHERE id = $2", [newName, groupId]).catch((e) => errorHandler(e, "groups"));; + } }; export default new GroupService(); \ No newline at end of file