Skip to content

Commit

Permalink
Merge pull request #25 from Arquisoft/santiago
Browse files Browse the repository at this point in the history
Meter nuevas preguntas si no hay en bd
  • Loading branch information
Santiago21112001 authored Mar 29, 2024
2 parents f37909c + ee4ec60 commit ba04c68
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,24 @@ async function generateQuestions() {
app.get('/getquestions', async (req, res) => {
try {

// Realizar consulta de agregación para obtener una pregunta aleatoria
const randomQuestions = await Question.aggregate([
{ $sample: { size: 5 } } // Selecciona una pregunta aleatoria
// Selecciona 'size' preguntas aleatorias
let randomQuestions = await Question.aggregate([
{ $sample: { size: 5 } }
]);

//const questionAndAnswer = await getQuestionTemplate(); // Obtenemos el json de pregunta y sus respuestas
console.log(randomQuestions); // Imprime questionAndAnswer en la consola
if (randomQuestions.length === 0) {
console.log("Not enough questions in database. Adding new ones...");
await generateQuestions();
randomQuestions = await Question.aggregate([
{ $sample: { size: 5 } }
]);
}

res.json(randomQuestions); //Devolvemos a la gateway el json
console.log(randomQuestions);
res.status(200).json(randomQuestions);
} catch (error) {
console.log("Error getting question from database: " + error);
console.log("Adding new questions to database...");
await generateQuestions();
console.log("Questions added. Getting random question...");
const randomQuestions = await Question.aggregate([
{ $sample: { size: 5 } } // Selecciona una pregunta aleatoria
]);
console.log("Random questions selected: " + randomQuestions); // Imprime questionAndAnswer en la consola
res.json(randomQuestions); //Devolvemos a la gateway el json
res.status(500).json({ error: 'Internal Server Error' });
}

});
Expand Down

0 comments on commit ba04c68

Please sign in to comment.