Skip to content

Commit 0ecce13

Browse files
committed
Fetch Qn by category and complexity
1 parent 99598fe commit 0ecce13

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

Backend/MatchingService/rabbitmq/subscriber.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function matchUsers(channel, msg, userId, difficulty, category) {
7171
const matchedUsers = waitingUsers[criteriaKey].splice(0, 2);
7272
removeMatchedUsersFromOtherLists(matchedUsers, criteriaKey);
7373
console.log("waitingusers after strict matching: ", waitingUsers)
74-
notifyMatch(channel, matchedUsers, category);
74+
notifyMatch(channel, matchedUsers, category, difficulty);
7575
return true;
7676
}
7777

@@ -83,7 +83,7 @@ function matchUsers(channel, msg, userId, difficulty, category) {
8383
const matchedUsers = waitingUsers[categoryKey].splice(0, 2);
8484
removeMatchedUsersFromOtherLists(matchedUsers, categoryKey);
8585
console.log("waitingusers after lenient matching: ", waitingUsers)
86-
notifyMatch(channel, matchedUsers, category);
86+
notifyMatch(channel, matchedUsers, category, difficulty);
8787
return true;
8888
}
8989
}
@@ -102,16 +102,18 @@ function removeMatchedUsersFromOtherLists(matchedUsers, keyToSkip) {
102102
console.log("waiting users after removing: ", waitingUsers);
103103
}
104104

105-
async function notifyMatch(channel, matchedUsers, category) {
105+
async function notifyMatch(channel, matchedUsers, category, complexity) {
106106
const roomId = uuidv4();
107107

108108
try {
109109
// Fetch a question from QuestionService based on category
110-
const response = await axios.get(`${questionAPIUrl}/by-category`, {
111-
params: { category: category }
110+
console.log(`fetching question by ${category} ${complexity}`)
111+
const response = await axios.get(`${questionAPIUrl}/by-category-and-complexity`, {
112+
params: { category: category, complexity: complexity }
112113
});
113114

114115
const questions = response.data;
116+
console.log("questions fetch from db: ", questions)
115117
const randomIndex = Math.floor(Math.random() * questions.length);
116118

117119
// Notify matched users with the roomId, question, and other details

Backend/QuestionService/controllers/questions.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
const questionsRouter = require('express').Router()
22
const QuestionModel = require('../models/Questions')
33

4-
questionsRouter.get("/by-category", async (req, res) => {
5-
const { category } = req.query;
4+
questionsRouter.get("/by-category-and-complexity", async (req, res) => {
5+
let { category, complexity } = req.query;
66
try {
7-
const query = category ? { category: { $in: [new RegExp(`^${category}$`, "i")] } } : {};
7+
const query = {}
8+
category = category.replace(/-/g, " ");
9+
query.category = { $in: [new RegExp(`^${category}$`, "i")] };
10+
query.complexity = { $in: [new RegExp(`^${complexity}$`, "i")] };
11+
812
const questions = await QuestionModel.find(query);
913
res.status(200).json(questions);
1014
} catch (error) {
11-
console.error("Error fetching questions by category:", error);
15+
console.error("Error fetching questions by category & complexity:", error);
1216
res.status(500).json({ error: error.message });
1317
}
1418
});

0 commit comments

Comments
 (0)