diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index c1eca2a..8f83f89 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -61,8 +61,10 @@ app.post('/createGame', async (req, res) => { const { players } = req.body; const createGameResponse = await axios.get(qgServiceUrl+'/game'); const questions = createGameResponse.data; - const updateUserResponse = await axios.post(gameServiceUrl+'/createGame', {players, questions}); - + const gameResponse = await axios.post(gameServiceUrl+'/createGame', {players, questions}); + const game = gameResponse.data; + const gameUUID = game.uuid; + const updateLastGameResponse = await axios.post(userServiceUrl+'/updateLastGame', {gameUUID, players}); res.json(questions); } catch (error) { res.status(500).json({ error: 'Internal server error' }); diff --git a/users/userservice/UserController.js b/users/userservice/UserController.js new file mode 100644 index 0000000..592dc54 --- /dev/null +++ b/users/userservice/UserController.js @@ -0,0 +1,28 @@ +const User = require('./user-model') + +let UserController = { + updateLastGame: async (req, res) => { + const { gameUUID, players } = req.body; + + for (const p of players) { + try { + const user = await User.findOne({ uuid: p.uuid }); + + if (user) { + user.lastGameId = gameUUID; + await user.save(); + } else { + console.error(`User with UUID ${p.uuid} not found.`); + } + } catch (error) { + console.error(`Error updating last game for user with UUID ${p.uuid}: ${error.message}`); + } + } + + const nPlayers = players.length; + res.json({ "message": `Last game updated for ${nPlayers} users.` }); + } + +} + +module.exports = UserController; \ No newline at end of file diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 12b4212..2df0861 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -5,6 +5,7 @@ const bcrypt = require('bcrypt'); const bodyParser = require('body-parser'); const User = require('./user-model') const uuid = require('uuid'); +const UserController = require('./UserController'); const app = express(); const port = 8001; @@ -52,6 +53,7 @@ app.post('/adduser', async (req, res) => { 3. Obtener estadisticas por usuario (puntos, partidas jugadas, preguntas acertadas/falladas) 4. Checkear si existe usuario con username */ +app.post('/updateLastGame', UserController.updateLastGame); const server = app.listen(port, () => { console.log(`User Service listening at http://localhost:${port}`);