diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 8587970..343fb31 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -102,9 +102,10 @@ app.post('/imgs/answer', async (req, res) => { try { const answer = req.body.answer; const username = req.body.username; + const category = req.body.category; // Forward the request to the question service - const questionResponse = await axios.post(questionServiceUrl+'/imgs/answer', {answer, username }, { headers: {'Content-Type': 'text/plain'} }); + const questionResponse = await axios.post(questionServiceUrl+'/imgs/answer', {answer, username, category }, { headers: {'Content-Type': 'text/plain'} }); res.json(questionResponse.data); } catch (error) { diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 454e58d..594ac54 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -234,14 +234,17 @@ function validateRequiredFields(req, requiredFields) { */ app.post('/imgs/answer', async (req, res) => { - const { answer, username } =JSON.parse(req.body); + const { answer, username, category } =JSON.parse(req.body); if(correctImg==answer){ - await axios.post(userServiceUrl+'/addpoints', {username: username } ); + await axios.post(userServiceUrl+'/addpoints', + {username: username, category: category, correct: "true" } ); res.status(200).json({ correct: "true", }) } else { + await axios.post(userServiceUrl+'/addpoints', + {username: username, category: category, correct: "false" } ); res.status(200).json({ correct: "false", country: `${imgToAssociatedMap.get(answer)}` diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 7166cc7..7cdf1c3 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -69,15 +69,37 @@ app.get('/rankings/:filter', async (req, res) => { app.post("/addpoints", async (req, res) => { - try { - validateRequiredFields(req, ['username']); + const username = req.body.username; + const category = req.body.category; + const correct = req.body.correct; + + + try { + validateRequiredFields(req, ['username','category','correct']); const user = await User.findOne({ username: req.body.username }); if (!user) { throw new Error('User not found'); } - user.points += 1; + + // updates global and category questions + user.ranking.global.questions += 1; + user.ranking[category].questions += 1; + + + // Answer is correct + if ( correct === "true"){ + user.ranking[category].points += 1; + user.ranking[category].correct += 1; + user.ranking.global.points += 1; + user.ranking.global.correct += 1; + } + else{ // Answer is wrong + user.ranking[category].wrong += 1; + user.ranking.global.wrong += 1; + } + await user.save(); res.status(200).json(user); } catch (error) { diff --git a/webapp/src/components/Question.jsx b/webapp/src/components/Question.jsx index fa3b617..1c02425 100644 --- a/webapp/src/components/Question.jsx +++ b/webapp/src/components/Question.jsx @@ -43,7 +43,7 @@ const Question = (props) => { const answerQuestion = async (answer) => { try { setLoading(true); - const result = await axios.post(`${apiEndpoint}/${props.type}/answer`, { answer: answer, username: auth.username }); + const result = await axios.post(`${apiEndpoint}/${props.type}/answer`, { answer: answer, username: auth.username, category: props.category }); const res = await axios.get(`${apiEndpoint}/${props.type}/${props.category}/question`); if ( result.data.correct === "true" ) { setScore(score +1);