Skip to content

Commit

Permalink
Now the game saves on the users database (no sé si funciona)
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanfdezz committed Feb 22, 2024
1 parent 928d645 commit 8828bd1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ app.post('/login', async (req, res) => {
const token = jwt.sign({ userId: user._id }, 'your-secret-key', { expiresIn: '1h' });
//Almacenamos el token del usuario para su autentificación
sessionStorage.setItem('token', token);

sessionStorage.setItem('username', username);

// Respond with the token and user information
res.json({ token: token, username: username, createdAt: user.createdAt });
} else {
Expand Down
4 changes: 4 additions & 0 deletions users/userservice/game-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const gameSchema = new mongoose.Schema({
type: Number,
default: 0
},
avgTime: {
type: Number,
default: 0
}
});

const Game = mongoose.model('Game', gameSchema);
Expand Down
42 changes: 42 additions & 0 deletions webapp/src/pages/Clasico/Clasico.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,48 @@ const JuegoPreguntas = () => {
setTiempoMedio(tiempoTotal/(preguntasCorrectas+preguntasFalladas));
}

//Now we store the game in the user's DB
const username = sessionStorage.getItem('username');

const newGame = new Game({
correctAnswers: preguntasCorrectas,
incorrectAnswers: preguntasFalladas,
points: puntuacion,
avgTime: tiempoMedio,
});

//SAVING THE GAME ON THE USERS DATABASE

// Encontrar el usuario en la base de datos
User.findOne({ username }, (err, user) => {
if (err) {
console.error('Error al encontrar el usuario:', err);
// TODO : hacer la UI para el manejo de errores
} else {

newGame.save((err, game) => {

if (err) {

console.error('Error al guardar el juego:', err);
// TODO : hacer la UI para el manejo de errores
}
else {

user.games.push(game._id);

user.save((err) => {
if (err) {
console.error('Error al guardar el usuario actualizado:', err);
// TODO : hacer la UI para el manejo de errores
}
});
}
});

}
});

setJuegoTerminado(true);
}
};
Expand Down

0 comments on commit 8828bd1

Please sign in to comment.