Skip to content

Commit

Permalink
no participation data
Browse files Browse the repository at this point in the history
  • Loading branch information
uo276976 committed Apr 8, 2024
1 parent 7a34cff commit d94b5cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ app.get('/getParticipation/:userId', async (req, res) => {
},
]);

if (participationData.length === 0) {
if (participationData.length === 0 || (participationData.length > 0 && participationData[0].totalGames === 0)) {
// No se encontraron datos para el usuario
res.status(404).json({ error: 'No participation data found for the user.' });
res.status(204).send();
return;
}

Expand Down
16 changes: 13 additions & 3 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ beforeEach(async () => {
totalTime: 1200,
gameMode: 'normal'
});

});

afterEach(async () => {
Expand Down Expand Up @@ -82,7 +81,6 @@ describe('Game Service', () => {
totalTime: 1200,
};


const response = await request(app).get(`/getParticipation/${userId}`);

expect(response.status).toBe(200);
Expand All @@ -95,7 +93,19 @@ describe('Game Service', () => {
const response = await request(app).get(`/getParticipation/${nonExistentUserId}`);

expect(response.status).toBe(404);
expect(response.body).toEqual({ error: 'No participation data found for the user.' });
expect(response.body).toEqual({ error: 'User not found.' });
});
it('should return 204 when getting participation data for user with totalGames equal to 0', async () => {
const userNoGames = await User.create({
username: 'noGames',
profileImage: 'defaultProfileImg',
password: 'password123'
});
userNGId = userNoGames._id;

const response = await request(app).get(`/getParticipation/${userNGId}`);

expect(response.status).toBe(204);
});
});
describe('Api info users', () => {
Expand Down
29 changes: 20 additions & 9 deletions webapp/src/components/Participation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ const gatewayUrl = process.env.REACT_APP_API_ENDPOINT || "http://localhost:8000"
export const Participation = ({ goTo }) => {
const { sessionData } = useContext(SessionContext); // Obtener datos de sesión del contexto
const [participationData, setParticipationData] = useState(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
// Realizar la solicitud al servidor para obtener los datos de participación
const fetchData = async () => {
try {
const response = await axios.get(`${gatewayUrl}/getParticipation/${sessionData.userId}`);
setParticipationData(response.data);
if (response.status === 204) {
setLoading(false);
} else {
setParticipationData(response.data);
setLoading(false);
}
} catch (error) {
console.error('Error al obtener los datos de participación:', error);
setLoading(false);
}
};

Expand All @@ -28,15 +35,19 @@ export const Participation = ({ goTo }) => {
<main>
<div>
<h1>Participación</h1>
{participationData !== null ? (
<div>
<p>Número de partidas jugadas: {participationData.totalGames}</p>
<p>Preguntas acertadas: {participationData.correctAnswers}</p>
<p>Preguntas falladas: {participationData.incorrectAnswers}</p>
<p>Tiempo total jugando: {participationData.totalTime} segundos</p>
</div>
{loading ? (
<p>{participationData ? 'Cargando datos de participación...' : 'No hay datos de participación'}</p>
) : (
<p>Cargando datos de participación...</p>
participationData !== null ? (
<div>
<p>Número de partidas jugadas: {participationData.totalGames}</p>
<p>Preguntas acertadas: {participationData.correctAnswers}</p>
<p>Preguntas falladas: {participationData.incorrectAnswers}</p>
<p>Tiempo total jugando: {participationData.totalTime} segundos</p>
</div>
) : (
<p>No hay datos de participación</p>
)
)}
</div>
</main>
Expand Down

0 comments on commit d94b5cf

Please sign in to comment.