From e0823e1879692037990cca42e108b95c6fa23fd2 Mon Sep 17 00:00:00 2001 From: Pablo Date: Mon, 8 Apr 2024 21:48:56 +0200 Subject: [PATCH] Implemented agreed thresholds for questions --- .../__tests/routes/question-routes.test.js | 2 +- questions/routes/question-routes.js | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/questions/__tests/routes/question-routes.test.js b/questions/__tests/routes/question-routes.test.js index 24814e26..f997d0b4 100644 --- a/questions/__tests/routes/question-routes.test.js +++ b/questions/__tests/routes/question-routes.test.js @@ -57,7 +57,7 @@ beforeEach(async () => { //Load database with initial conditions //await mongoose.connection.dropDatabase(); await Question.deleteMany({}); - for(var i = 0; i < 5; i++) { + for(var i = 0; i < 100; i++) { await addingQuestion(questionData1); await addingQuestion(questionData2); } diff --git a/questions/routes/question-routes.js b/questions/routes/question-routes.js index 05954f0a..565c54b3 100644 --- a/questions/routes/question-routes.js +++ b/questions/routes/question-routes.js @@ -6,16 +6,35 @@ const router = express.Router(); //Get random question from db and deleting it, adding questions if there are less than 10: http://localhost:8010/questions router.get('/', async (req, res) => { - if (await dbService.getQuestionCount() < 10) { - //Must generate 2 questions + const questionCount = await dbService.getQuestionCount(); + + // 0: Await till it creates 2, creates 50 async and do not delete + if (questionCount == 0) { await generateQuestionsService.generateQuestions(2); + generateQuestionsService.generateQuestions(50); + const question = await dbService.getQuestion(); + res.json(question); + + // < 50: async creates 10 and do not delete + } else if (questionCount < 50) { //Do not wait to generate the others - generateQuestionsService.generateQuestions(8); - } - const question = await dbService.getQuestion(); - res.json(question); + generateQuestionsService.generateQuestions(10); + const question = await dbService.getQuestion(); + res.json(question); - dbService.deleteQuestionById(question._id); + // < 100: async creates 5 and delete + } else if (questionCount < 100) { + generateQuestionsService.generateQuestions(10); + const question = await dbService.getQuestion(); + res.json(question); + dbService.deleteQuestionById(question._id); + + // >= 100: do not create and delete + } else { + const question = await dbService.getQuestion(); + res.json(question); + dbService.deleteQuestionById(question._id); + } }); //Get random questions from db: http://localhost:8010/questions/getQuestionsFromDb/3 @@ -63,4 +82,4 @@ router.get('/getQuestionsFromDb/:n/:category', async(_req, res) => { res.json(questions); }); -module.exports = router; +module.exports = router; \ No newline at end of file