Skip to content

Commit

Permalink
Problem with concurrent users with questions solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFN2 committed Apr 5, 2024
1 parent 8d0561d commit caea032
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
3 changes: 2 additions & 1 deletion gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
26 changes: 16 additions & 10 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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{
/**
Expand All @@ -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
Expand Down Expand Up @@ -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]}`]
}

Expand Down Expand Up @@ -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)}`
})
}
});
Expand Down
10 changes: 6 additions & 4 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
});

Expand All @@ -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))
});
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -74,7 +74,7 @@ const Question = (props) => {
{question.images.map(image => (
<button className="transition-transform transform-gpu hover:scale-105 rounded-xl mx-8 my-8 max-h-52 max-w-80">
<img src={image} alt='Loading image...' className="rounded-lg object-contain shadow-md"
onClick={() => answerQuestion(image)}></img>
onClick={() => answerQuestion(image,question.question)}></img>
</button>
))}
</div>
Expand Down

0 comments on commit caea032

Please sign in to comment.