From 041a71d43989d25cdd667735a0246178dea631dc Mon Sep 17 00:00:00 2001 From: carlospelazas Date: Wed, 13 Mar 2024 20:43:30 +0100 Subject: [PATCH] leave group gateway --- gatewayservice/gateway-service.js | 13 +++++++++++++ users/userservice/UserController.js | 11 ++++++++++- users/userservice/user-service.js | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index b2b86ef..3d101fc 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -138,7 +138,20 @@ app.post('/joinGroup', async (req, res) => { res.status(500).json({ error: error.message }); } }) + // leave group +app.post('/leaveGroup', async (req, res) => { + try{ + const groupResponse = await axios.post(groupServiceUrl+'/leaveGroup', req.body); + console.log("---- GROUP LEFT SUCCESFULLY ----") + console.log(groupResponse.data) + const userResponse = await axios.delete(userServiceUrl+'/leaveGroup/'+req.body.expelledUUID); + res.json(groupResponse.data); + } catch(error){ + res.status(500).json({ error: error.message }); + } +}) + // get group by id app.get('/getGroup/:uuid', async (req, res) => { try{ diff --git a/users/userservice/UserController.js b/users/userservice/UserController.js index 8bdea1e..bcc83d3 100644 --- a/users/userservice/UserController.js +++ b/users/userservice/UserController.js @@ -143,8 +143,17 @@ let UserController = { users.push(user); } res.json(users); + }, + leaveGroup: async (req, res) => { + const id = req.params.id; + console.log(id) + const user = await User.findOne({uuid: id}); + if(user){ + user.groupId = null; + } + const response = await user.save(); + res.json(response); } - } module.exports = UserController; \ No newline at end of file diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 5c5b2f1..05e88d0 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -30,6 +30,7 @@ app.get('/getUser/:username', UserController.getUserByUsername); app.post('/getUsersByIds', UserController.getUsersByIds); app.get('/getUserById/:id', UserController.getUserById) app.put('/addGroup/:userUUID', UserController.addGroupToUser) +app.delete('/leaveGroup/:id', UserController.leaveGroup) const server = app.listen(port, () => { console.log(`User Service listening at http://localhost:${port}`);