From ee4ec605a0a0450770b05ee57bc430b964134d96 Mon Sep 17 00:00:00 2001 From: Santiago21112001 Date: Fri, 29 Mar 2024 18:10:12 +0100 Subject: [PATCH] Meter nuevas preguntas si no hay en bd --- questionservice/question-service.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 8cf4e32..70a94dd 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -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' }); } });