From d0e04152247a690e416d9ebfd1a52c194fdb7d06 Mon Sep 17 00:00:00 2001 From: RaquelSuarezSanchez Date: Wed, 5 Mar 2025 21:43:02 +0100 Subject: [PATCH] Added method to get incorrect answers for each question, as well as a method to get one question (that hasn't already been shown) that will be called from front-end. --- questionservice/question-model.js | 3 ++- questionservice/question-service.js | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/questionservice/question-model.js b/questionservice/question-model.js index eac62de8..0400d77d 100644 --- a/questionservice/question-model.js +++ b/questionservice/question-model.js @@ -5,7 +5,8 @@ const questionSchema = new mongoose.Schema({ imageUrl: { type: String, required: true }, options: { type: [String], required: true }, // Opciones de respuesta correctAnswer: { type: String, required: true }, - createdAt: { type: Date, default: Date.now } + createdAt: { type: Date, default: Date.now }, + alreadyShown: { type: Boolean, default:false } }); const Question = mongoose.model('Question', questionSchema); diff --git a/questionservice/question-service.js b/questionservice/question-service.js index b890f861..578e2312 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -1,6 +1,7 @@ const axios = require('axios'); const mongoose = require('mongoose'); const Question = require('./question-model'); +const LLMService = require('../llmservice/llm-service'); // Define the connection URL const mongoURI = 'mongodb://localhost:27017/mongo-db-wichat_en2a'; @@ -34,10 +35,11 @@ async function fetchFlagData(){ } }); + const results = response.data.results.bindings.map(entry => ({ type: "flag", imageUrl: entry.flag.value, - options: [entry.countryLabel.value], + options: getIncorrectAnswers(entry.countryLabel.value), // Insert incorrect answers using LLM. correctAnswer: entry.countryLabel.value })); @@ -52,6 +54,11 @@ async function fetchFlagData(){ } } +/// Returns array including correct and incorrect answers. +function getIncorrectAnswers(correctAnswer) { + return [correctAnswer, ...LLMService.getIncorrectAnswers(correctAnswer)]; +} + async function saveQuestionsToDB(questions){ try{ await Question.insertMany(questions) //insert questions to db @@ -62,6 +69,24 @@ async function saveQuestionsToDB(questions){ } } +// Should be called from front-end +async function getAndUpdateQuestion() { + try { + // Find one question where alreadyShown is false + const question = await Question.findOneAndUpdate( + { alreadyShown: false }, // Find condition + { alreadyShown: true }, // Update to true + { new: true } // Return the updated document + ); + + return question; // Return the extracted question + } catch (error) { + console.error("Error fetching question:", error); + return null; + } +} + + //Just for testing purposes fetchFlagData();