From caea0326f82d4a3a10ad4154db873617ee946382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Fri, 5 Apr 2024 19:38:03 +0200 Subject: [PATCH] Problem with concurrent users with questions solved --- gatewayservice/gateway-service.js | 3 ++- questionservice/question-service.js | 26 +++++++++++++++--------- questionservice/question-service.test.js | 10 +++++---- webapp/src/components/Question.jsx | 6 +++--- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 41540bf..ed1845d 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -101,8 +101,9 @@ app.get('/imgs/foods/question', async (req, res) => { app.post('/imgs/answer', async (req, res) => { try { const answer = req.body.answer; + const question = req.body.question; // Forward the request to the question service - const questionResponse = await axios.post(questionServiceUrl+'/imgs/answer', answer, { headers: {'Content-Type': 'text/plain'} }); + const questionResponse = await axios.post(questionServiceUrl+'/imgs/answer', {answer:answer, question:question}, { headers: {'Content-Type': 'application/json'} }); res.json(questionResponse.data); } catch (error) { res.status(error.response.status).json({ error: error.response.data.error }); diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 16e0c98..c239a1b 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -8,10 +8,10 @@ const app = express(); const port = 8010; app.use(express.static('public')); -app.use(express.text()); +app.use(express.json()); -var correctImg var imgToAssociatedMap = new Map() +var answerToQuestionMap = new Map() class WIQ_API{ /** @@ -27,8 +27,6 @@ class WIQ_API{ * @returns - A JSON with the question (question) and the images (images) */ async getQuestionAndImages(query, imgTypeName, relation) { - //Reset the map for the new question - imgToAssociatedMap = new Map() //Num of fetched items const itemsNum = 200 @@ -83,10 +81,13 @@ class WIQ_API{ //Choose a random item of the chosen to make the question const chosenNum = this.#getRandomNumNotInSetAndUpdate(numOfChosen,chosenNums) const chosenAssociate = associates[chosenNum] - correctImg = imgs[chosenNum] + let correctImg = imgs[chosenNum] + + const question = `Which of the following ${imgTypeName} ${relation} ${chosenAssociate}?` + answerToQuestionMap.set(correctImg,question) const questionAndImages = { - question: `Which of the following ${imgTypeName} ${relation} ${chosenAssociate}?`, + question: question, images: [`${imgs[0]}`,`${imgs[1]}`,`${imgs[2]}`,`${imgs[3]}`] } @@ -198,22 +199,27 @@ app.get('/imgs/foods/question', async (req, res) => { /** * Gets a response indicating if the chosen img was correct or not - * @param {string} req - img url selected by the player + * @param {Object} req - img url selected by the player and the question he is answering * @param {Object} res - JSON containing whether the answer was correct "true" * or not "false". In case it was incorrect, the chosen * associate will be returned as well */ app.post('/imgs/answer', (req, res) => { - const answer = req.body; + const obj = req.body; + + //console.log(obj) - if(correctImg==answer){ + if(obj.question==answerToQuestionMap.get(obj.answer)){ + //console.log("Correct") res.status(200).json({ correct: "true" }) } else { + //console.log("Incorrect") + //console.log(imgToAssociatedMap.get(obj.answer)) res.status(200).json({ correct: "false", - associate: `${imgToAssociatedMap.get(answer)}` + associate: `${imgToAssociatedMap.get(obj.answer)}` }) } }); diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js index 61c035c..079f4f4 100644 --- a/questionservice/question-service.test.js +++ b/questionservice/question-service.test.js @@ -90,10 +90,11 @@ describe('Question Service', () => { } counter++ } + question = response.body.question const responseAnswer = await request(app) .post("/imgs/answer") - .set('Content-Type', 'text/plain') - .send(correctImage) + .set('Content-Type', 'application/json') + .send({answer:correctImage, question:question}) expect(responseAnswer.body.correct).toBe("true") }); @@ -114,10 +115,11 @@ describe('Question Service', () => { } counter++ } + question = response.body.question const responseAnswer = await request(app) .post("/imgs/answer") - .set('Content-Type', 'text/plain') - .send(incorrectImageAnswer) + .set('Content-Type', 'application/json') + .send({answer:incorrectImageAnswer, question:question}) expect(responseAnswer.body.correct).toBe("false") expect(responseAnswer.body.associate).toBe(imgsToAssociatesMap.get(incorrectImageAnswer)) }); diff --git a/webapp/src/components/Question.jsx b/webapp/src/components/Question.jsx index 930be01..6f87d7a 100644 --- a/webapp/src/components/Question.jsx +++ b/webapp/src/components/Question.jsx @@ -37,10 +37,10 @@ const Question = (props) => { } }; - const answerQuestion = async (answer) => { + const answerQuestion = async (answer, question) => { try { setLoading(true); - const result = await axios.post(`${apiEndpoint}/${props.type}/answer`, { answer }); + const result = await axios.post(`${apiEndpoint}/${props.type}/answer`, { answer:answer, question:question }, { headers: {'Content-Type': 'application/json'}}); const res = await axios.get(`${apiEndpoint}/${props.type}/${props.category}/question`); setQuestion(res.data); setLoading(false); @@ -74,7 +74,7 @@ const Question = (props) => { {question.images.map(image => ( ))}