Skip to content

Commit ba96215

Browse files
committed
Add read endpoints
-implement endpoint to read questions for question list (with filter and search functionality) -implement endpoint to read question using specific question id
1 parent 2176d08 commit ba96215

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

backend/question-service/src/controllers/questionController.ts

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
66
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
77
QN_DESC_CHAR_LIMIT,
8+
QN_CREATED,
9+
QN_NOT_FOUND,
10+
SERVER_ERROR,
11+
QN_RETRIEVED,
812
} from "../utils/constants.ts";
913

1014
export const createQuestion = async (
@@ -39,11 +43,11 @@ export const createQuestion = async (
3943
await newQuestion.save();
4044

4145
res.status(201).json({
42-
message: "Question created successfully",
46+
message: QN_CREATED,
4347
question: newQuestion,
4448
});
4549
} catch (error) {
46-
res.status(500).json({ message: "Server error", error });
50+
res.status(500).json({ message: SERVER_ERROR, error });
4751
}
4852
};
4953

@@ -57,7 +61,7 @@ export const updateQuestion = async (
5761

5862
const currentQuestion = await Question.findById(id);
5963
if (!currentQuestion) {
60-
res.status(404).json({ message: "Question not found" });
64+
res.status(404).json({ message: QN_NOT_FOUND });
6165
return;
6266
}
6367

@@ -85,6 +89,92 @@ export const updateQuestion = async (
8589
question: updatedQuestion,
8690
});
8791
} catch (error) {
88-
res.status(500).json({ message: "Server error", error });
92+
res.status(500).json({ message: SERVER_ERROR, error });
93+
}
94+
};
95+
96+
export const readQuestionsList = async (
97+
req: Request,
98+
res: Response,
99+
): Promise<void> => {
100+
try {
101+
const qnLimit = 10;
102+
103+
const { currPage } = req.params;
104+
const currPageInt = parseInt(currPage);
105+
106+
if (!req.body || Object.keys(req.body).length == 0) {
107+
const totalQuestions = await Question.countDocuments();
108+
if (totalQuestions == 0) {
109+
res.status(404).json({ message: QN_NOT_FOUND });
110+
return;
111+
}
112+
const totalPages = Math.ceil(totalQuestions / qnLimit);
113+
114+
const currPageQuestions = await Question.find()
115+
.skip((currPageInt - 1) * qnLimit)
116+
.limit(qnLimit);
117+
118+
res.status(200).json({
119+
message: QN_RETRIEVED,
120+
pages: totalPages,
121+
questions: currPageQuestions,
122+
});
123+
} else {
124+
const { title, complexities, categories } = req.body;
125+
const query: any = {};
126+
127+
if (title) {
128+
query.title = { $regex: new RegExp(title, "i") };
129+
}
130+
131+
if (complexities && complexities.length > 0) {
132+
query.complexity = { $in: complexities };
133+
}
134+
135+
if (categories && categories.length > 0) {
136+
query.category = { $in: categories };
137+
}
138+
139+
const filteredTotalQuestions = await Question.countDocuments(query);
140+
if (filteredTotalQuestions == 0) {
141+
res.status(404).json({ message: QN_NOT_FOUND });
142+
return;
143+
}
144+
const filteredTotalPages = Math.ceil(filteredTotalQuestions / qnLimit);
145+
146+
const filteredQuestions = await Question.find(query)
147+
.skip((currPageInt - 1) * qnLimit)
148+
.limit(qnLimit);
149+
150+
res.status(200).json({
151+
message: QN_RETRIEVED,
152+
pages: filteredTotalPages,
153+
questions: filteredQuestions,
154+
});
155+
}
156+
} catch (error) {
157+
res.status(500).json({ message: SERVER_ERROR, error });
158+
}
159+
};
160+
161+
export const readQuestionIndiv = async (
162+
req: Request,
163+
res: Response,
164+
): Promise<void> => {
165+
try {
166+
const { id } = req.params;
167+
168+
const questionDetails = await Question.findById(id);
169+
if (!questionDetails) {
170+
res.status(404).json({ message: QN_NOT_FOUND });
171+
return;
172+
}
173+
res.status(200).json({
174+
message: QN_RETRIEVED,
175+
question: questionDetails,
176+
});
177+
} catch (error) {
178+
res.status(500).json({ message: SERVER_ERROR, error });
89179
}
90180
};

backend/question-service/src/routes/questionRoutes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import express from "express";
22
import {
33
createQuestion,
44
updateQuestion,
5+
readQuestionsList,
6+
readQuestionIndiv,
57
} from "../controllers/questionController.ts";
68

79
const router = express.Router();
@@ -10,4 +12,8 @@ router.post("/questions", createQuestion);
1012

1113
router.put("/questions/:id", updateQuestion);
1214

15+
router.post("/questionsquery/:currPage", readQuestionsList);
16+
17+
router.get("/questions/:id", readQuestionIndiv);
18+
1319
export default router;

backend/question-service/src/utils/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ export const QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE =
55

66
export const DUPLICATE_QUESTION_RESPONSE_MESSAGE =
77
"Duplicate question: A question with the same title already exists.";
8+
9+
export const QN_CREATED = "Question created successfully.";
10+
11+
export const QN_NOT_FOUND = "Question not found.";
12+
13+
export const SERVER_ERROR = "Server error.";
14+
15+
export const QN_RETRIEVED = "Question retrieved successfully.";

0 commit comments

Comments
 (0)