Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloGOP committed Apr 8, 2024
2 parents fa69919 + 36ad181 commit 34d773a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion questions/__tests/routes/question-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
35 changes: 27 additions & 8 deletions questions/routes/question-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,4 +82,4 @@ router.get('/getQuestionsFromDb/:n/:category', async(_req, res) => {
res.json(questions);
});

module.exports = router;
module.exports = router;

0 comments on commit 34d773a

Please sign in to comment.