Skip to content

Commit

Permalink
Inicio obtencion base pregunta aleatoria
Browse files Browse the repository at this point in the history
  • Loading branch information
uo283055 committed Feb 14, 2024
1 parent 97aade1 commit d97c133
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions questions/createservice/create-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ app.post('/addQuestion', async (req, res) => {
}
});

//obtiene una pregunta de forma aleatoria
app.get('/getQuestionBody', async (req, res) => {
try {

//modelo mongo
const Question = mongoose.model('Question');
//saco una pregunta de forma aleatoria
const rQuestion = await Question.aggregate([{ $sample: { size: 1 } }]);

if (rQuestion && rQuestion.length > 0) {

//obtengo la pregunta
const rQuestionResult = rQuestion[0];

res.json({ pregunta: rQuestionResult });

} else {
console.log("No hay preguntas");
}

} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

// Start the server
const server = app.listen(port, () => {
console.log(`Auth Service listening at http://localhost:${port}`);
Expand Down

0 comments on commit d97c133

Please sign in to comment.