Skip to content

Commit fc45f03

Browse files
committed
change read API request type
- changed getting the question list from using POST request to using GET request - allow frontend to specify question limit per page through request query
1 parent ba96215 commit fc45f03

File tree

3 files changed

+82
-72
lines changed

3 files changed

+82
-72
lines changed

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

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ 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,
8+
QN_CREATED_MESSAGE,
9+
QN_NOT_FOUND_MESSAGE,
10+
SERVER_ERROR_MESSAGE,
11+
QN_RETRIEVED_MESSAGE,
12+
PAGE_LIMIT_REQUIRED_MESSAGE,
13+
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
1214
} from "../utils/constants.ts";
1315

1416
export const createQuestion = async (
@@ -43,11 +45,11 @@ export const createQuestion = async (
4345
await newQuestion.save();
4446

4547
res.status(201).json({
46-
message: QN_CREATED,
48+
message: QN_CREATED_MESSAGE,
4749
question: newQuestion,
4850
});
4951
} catch (error) {
50-
res.status(500).json({ message: SERVER_ERROR, error });
52+
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
5153
}
5254
};
5355

@@ -61,7 +63,7 @@ export const updateQuestion = async (
6163

6264
const currentQuestion = await Question.findById(id);
6365
if (!currentQuestion) {
64-
res.status(404).json({ message: QN_NOT_FOUND });
66+
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
6567
return;
6668
}
6769

@@ -89,72 +91,74 @@ export const updateQuestion = async (
8991
question: updatedQuestion,
9092
});
9193
} catch (error) {
92-
res.status(500).json({ message: SERVER_ERROR, error });
94+
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
9395
}
9496
};
9597

98+
interface QnListSearchFilterParams {
99+
page: string;
100+
qnLimit: string;
101+
title?: string;
102+
complexities?: string | string[];
103+
categories?: string | string[];
104+
}
105+
96106
export const readQuestionsList = async (
97-
req: Request,
107+
req: Request<unknown, unknown, unknown, QnListSearchFilterParams>,
98108
res: Response,
99109
): Promise<void> => {
100110
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-
});
111+
const { page, qnLimit, title, complexities, categories } = req.query;
112+
113+
if (!page || !qnLimit) {
114+
res.status(400).json({ message: PAGE_LIMIT_REQUIRED_MESSAGE });
115+
return;
116+
}
117+
118+
const pageInt = parseInt(page, 10);
119+
const qnLimitInt = parseInt(qnLimit, 10);
120+
121+
if (pageInt < 1 || qnLimitInt < 1) {
122+
res.status(400).json({ message: PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE });
123+
return;
124+
}
125+
126+
const query: any = {};
127+
128+
if (title) {
129+
query.title = { $regex: new RegExp(title, "i") };
130+
}
131+
132+
if (complexities) {
133+
query.complexity = {
134+
$in: Array.isArray(complexities) ? complexities : [complexities],
135+
};
136+
}
137+
138+
if (categories) {
139+
query.category = {
140+
$in: Array.isArray(categories) ? categories : [categories],
141+
};
142+
}
143+
144+
const filteredTotalQuestions = await Question.countDocuments(query);
145+
if (filteredTotalQuestions == 0) {
146+
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
147+
return;
155148
}
149+
const filteredTotalPages = Math.ceil(filteredTotalQuestions / qnLimitInt);
150+
151+
const filteredQuestions = await Question.find(query)
152+
.skip((pageInt - 1) * qnLimitInt)
153+
.limit(qnLimitInt);
154+
155+
res.status(200).json({
156+
message: QN_RETRIEVED_MESSAGE,
157+
pages: filteredTotalPages,
158+
questions: filteredQuestions,
159+
});
156160
} catch (error) {
157-
res.status(500).json({ message: SERVER_ERROR, error });
161+
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
158162
}
159163
};
160164

@@ -167,14 +171,14 @@ export const readQuestionIndiv = async (
167171

168172
const questionDetails = await Question.findById(id);
169173
if (!questionDetails) {
170-
res.status(404).json({ message: QN_NOT_FOUND });
174+
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
171175
return;
172176
}
173177
res.status(200).json({
174-
message: QN_RETRIEVED,
178+
message: QN_RETRIEVED_MESSAGE,
175179
question: questionDetails,
176180
});
177181
} catch (error) {
178-
res.status(500).json({ message: SERVER_ERROR, error });
182+
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
179183
}
180184
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ router.post("/questions", createQuestion);
1212

1313
router.put("/questions/:id", updateQuestion);
1414

15-
router.post("/questionsquery/:currPage", readQuestionsList);
15+
router.get("/questions", readQuestionsList);
1616

1717
router.get("/questions/:id", readQuestionIndiv);
1818

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ export const QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE =
66
export const DUPLICATE_QUESTION_RESPONSE_MESSAGE =
77
"Duplicate question: A question with the same title already exists.";
88

9-
export const QN_CREATED = "Question created successfully.";
9+
export const QN_CREATED_MESSAGE = "Question created successfully.";
1010

11-
export const QN_NOT_FOUND = "Question not found.";
11+
export const QN_NOT_FOUND_MESSAGE = "Question not found.";
1212

13-
export const SERVER_ERROR = "Server error.";
13+
export const SERVER_ERROR_MESSAGE = "Server error.";
1414

15-
export const QN_RETRIEVED = "Question retrieved successfully.";
15+
export const QN_RETRIEVED_MESSAGE = "Question retrieved successfully.";
16+
17+
export const PAGE_LIMIT_REQUIRED_MESSAGE =
18+
"Page number and question limit per page should be provided.";
19+
20+
export const PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE =
21+
"Page number and question limit per page should be positive integers.";

0 commit comments

Comments
 (0)