Skip to content

Commit

Permalink
update lastGame for each user that plays
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Mar 7, 2024
1 parent 07a1f94 commit d626b57
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
28 changes: 28 additions & 0 deletions users/userservice/UserController.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
Expand Down

0 comments on commit d626b57

Please sign in to comment.