Skip to content

Commit

Permalink
Merge pull request #156 from Arquisoft/users_info_api
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287747 authored Apr 6, 2024
2 parents e5c1f30 + af0fc67 commit 3633f4f
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 38 deletions.
8 changes: 8 additions & 0 deletions gameservice/game-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const gameSchema = new mongoose.Schema({
type: Number,
required: true,
},
date: {
type: Date,
default: Date.now,
},
gameMode:{
type: String,
required: true,
}
});
module.exports = (connection) => {
return connection.model('Game', gameSchema);
Expand Down
27 changes: 17 additions & 10 deletions gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ const validateRequiredFields = (req, fields) => {
// Ruta para agregar un nuevo juego
app.post('/addgame', async (req, res) => {
try {
validateRequiredFields(req, ['user', 'pAcertadas', 'pFalladas', 'totalTime']);
validateRequiredFields(req, ['user', 'pAcertadas', 'pFalladas', 'totalTime', 'gameMode']);

const { user, pAcertadas, pFalladas, totalTime } = req.body;
const { user, pAcertadas, pFalladas, totalTime, gameMode } = req.body;

// Crea una nueva instancia del modelo de juegos
const newGame = new Game({
user: user,
pAcertadas: pAcertadas,
pFalladas: pFalladas,
totalTime: totalTime
totalTime: totalTime,
gameMode: gameMode
});

// Guarda el nuevo juego en la base de datos
Expand All @@ -51,12 +52,23 @@ app.get('/api/info/games', async (req, res) => {
const {user} = req.query;
let query = {};

if (user !== undefined) query.user = user === '' ? null : user;
if (user !== undefined) {//filtra por el id del usuario
query.user = user === '' ? null : user;
}

const game = await Game.find(query);
if (!game) {
return res.status(404).json({ error: 'No information for games found' });
}
res.status(200).json(game);
const modifiedGame = game.map(g => {
const { pAcertadas, pFalladas, ...rest } = g._doc;
return {
...rest,
correctAnswers: pAcertadas,
incorrectAnswers: pFalladas
};
});
res.status(200).json(modifiedGame);
} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down Expand Up @@ -123,17 +135,14 @@ app.get('/api/info/users', async (req, res) => {
app.get('/getParticipation/:userId', async (req, res) => {
try {
const userId = req.params.userId;
console.log('User ID:', userId);

if (!userId) {
// Si no se encuentra el usuario, responder con un error
console.log('User not found');
res.status(404).json({ error: 'User not found' });
return;
}

// Consulta para obtener los datos de participación del usuario
console.log('Querying participation data...');
const participationData = await Game.aggregate([
{ $match: { user: userId } },
{
Expand All @@ -149,12 +158,10 @@ app.get('/getParticipation/:userId', async (req, res) => {

if (participationData.length === 0) {
// No se encontraron datos para el usuario
console.log('No participation data found for the user.');
res.status(404).json({ error: 'No participation data found for the user.' });
return;
}

console.log('Sending participation data:', participationData[0]);
res.status(200).json(participationData[0]);
} catch (error) {
console.error('Error al obtener datos de participación:', error);
Expand Down
4 changes: 4 additions & 0 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ beforeEach(async () => {
pAcertadas: 5,
pFalladas: 3,
totalTime: 1200,
gameMode: 'normal'
});

});
Expand All @@ -54,6 +55,7 @@ describe('Game Service', () => {
pAcertadas: 5,
pFalladas: 3,
totalTime: 1200,
gameMode: 'normal'
};

const response = await request(app).post('/addgame').send(newGame);
Expand All @@ -64,6 +66,8 @@ describe('Game Service', () => {
expect(data).toHaveProperty('pAcertadas', newGame.pAcertadas);
expect(data).toHaveProperty('pFalladas', newGame.pFalladas);
expect(data).toHaveProperty('totalTime', newGame.totalTime);
expect(data).toHaveProperty('gameMode', newGame.gameMode);
expect(data).toHaveProperty('date');
});


Expand Down
Loading

0 comments on commit 3633f4f

Please sign in to comment.