Skip to content

Commit 454ed1f

Browse files
committed
add read category endpoint
- add endpoint for reading unique question categories for frontend filter component - update api documentation
1 parent 0ef57ee commit 454ed1f

File tree

4 files changed

+182
-6
lines changed

4 files changed

+182
-6
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
QN_RETRIEVED_MESSAGE,
1313
PAGE_LIMIT_REQUIRED_MESSAGE,
1414
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
15+
CATEGORIES_NOT_FOUND_MESSAGE,
16+
CATEGORIES_RETRIEVED_MESSAGE,
1517
} from "../utils/constants.ts";
1618

1719
import { upload } from "../../config/multer";
@@ -136,7 +138,7 @@ export const deleteQuestion = async (
136138
const { id } = req.params;
137139
const currentQuestion = await Question.findById(id);
138140
if (!currentQuestion) {
139-
res.status(400).json({ message: QN_NOT_FOUND_MESSAGE });
141+
res.status(404).json({ message: QN_NOT_FOUND_MESSAGE });
140142
return;
141143
}
142144

@@ -235,3 +237,22 @@ export const readQuestionIndiv = async (
235237
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
236238
}
237239
};
240+
241+
export const readCategories = async (
242+
req: Request,
243+
res: Response,
244+
): Promise<void> => {
245+
try {
246+
const uniqueCats = await Question.distinct("category");
247+
if (!uniqueCats || uniqueCats.length == 0) {
248+
res.status(404).json({ message: CATEGORIES_NOT_FOUND_MESSAGE });
249+
}
250+
251+
res.status(200).json({
252+
message: CATEGORIES_RETRIEVED_MESSAGE,
253+
categories: uniqueCats,
254+
});
255+
} catch (error) {
256+
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
257+
}
258+
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
updateQuestion,
77
readQuestionsList,
88
readQuestionIndiv,
9+
readCategories,
910
} from "../controllers/questionController.ts";
1011

1112
const router = express.Router();
@@ -16,6 +17,8 @@ router.post("/questions/images", createImageLink);
1617

1718
router.put("/questions/:id", updateQuestion);
1819

20+
router.get("/questions/categories", readCategories);
21+
1922
router.get("/questions", readQuestionsList);
2023

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ export const PAGE_LIMIT_REQUIRED_MESSAGE =
2121

2222
export const PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE =
2323
"Page number and question limit per page should be positive integers.";
24+
25+
export const CATEGORIES_NOT_FOUND_MESSAGE = "No categories found.";
26+
27+
export const CATEGORIES_RETRIEVED_MESSAGE =
28+
"Categories retrieved successfully.";

backend/question-service/swagger.yml

Lines changed: 152 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ definitions:
5858
properties:
5959
message:
6060
type: string
61-
deescription: Message
61+
description: Message
6262
ServerError:
6363
type: object
6464
properties:
@@ -124,6 +124,83 @@ paths:
124124
application/json:
125125
schema:
126126
$ref: "#/definitions/ServerError"
127+
get:
128+
tags:
129+
- questions
130+
summary: Reads a list of questions
131+
description: Reads a limited list of questions based on current page and question limit per page, taking into account search and filter conditions (if any).
132+
parameters:
133+
- in: query
134+
name: page
135+
type: integer
136+
required: true
137+
description: Page of questions to return
138+
- in: query
139+
name: qnLimit
140+
type: integer
141+
required: true
142+
description: Limit on number of questions to return
143+
- in: query
144+
name: title
145+
type: string
146+
required: false
147+
description: Question title search keywords
148+
- in: query
149+
name: complexities
150+
schema:
151+
oneOf:
152+
- type: string
153+
- type: array
154+
items:
155+
type: string
156+
required: false
157+
description: Question complexity filters
158+
- in: query
159+
name: categories
160+
schema:
161+
oneOf:
162+
- type: string
163+
- type: array
164+
items:
165+
type: string
166+
required: false
167+
description: Question category filters
168+
responses:
169+
200:
170+
description: Successful Response
171+
content:
172+
application/json:
173+
schema:
174+
type: object
175+
properties:
176+
message:
177+
type: string
178+
description: Message
179+
pages:
180+
type: integer
181+
description: Total number of pages of questions
182+
questions:
183+
type: array
184+
items:
185+
$ref: "#/definitions/Question"
186+
400:
187+
description: Bad Request
188+
content:
189+
application/json:
190+
schema:
191+
$ref: "#/definitions/Error"
192+
404:
193+
description: Question Not Found
194+
content:
195+
application/json:
196+
schema:
197+
$ref: "#/definitions/Error"
198+
500:
199+
description: Internal Server Error
200+
content:
201+
application/json:
202+
schema:
203+
$ref: "#/definitions/ServerError"
127204
/api/questions/{id}:
128205
put:
129206
tags:
@@ -155,8 +232,8 @@ paths:
155232
description: Message
156233
question:
157234
$ref: "#/definitions/Question"
158-
400:
159-
description: Bad Request
235+
404:
236+
description: Question Not Found
160237
content:
161238
application/json:
162239
schema:
@@ -189,8 +266,78 @@ paths:
189266
message:
190267
type: string
191268
description: Message
192-
400:
193-
description: Bad Request
269+
404:
270+
description: Question Not Found
271+
content:
272+
application/json:
273+
schema:
274+
$ref: "#/definitions/Error"
275+
500:
276+
description: Internal Server Error
277+
content:
278+
application/json:
279+
schema:
280+
$ref: "#/definitions/ServerError"
281+
get:
282+
tags:
283+
- questions
284+
summary: Reads a question
285+
description: Reads a question
286+
parameters:
287+
- in: path
288+
name: id
289+
type: string
290+
required: true
291+
description: Question id
292+
responses:
293+
200:
294+
description: Successful Response
295+
content:
296+
application/json:
297+
schema:
298+
type: object
299+
properties:
300+
message:
301+
type: string
302+
description: Message
303+
question:
304+
$ref: "#/definitions/Question"
305+
404:
306+
description: Question Not Found
307+
content:
308+
application/json:
309+
schema:
310+
$ref: "#/definitions/Error"
311+
500:
312+
description: Internal Server Error
313+
content:
314+
application/json:
315+
schema:
316+
$ref: "#/definitions/ServerError"
317+
/api/questions/categories:
318+
get:
319+
tags:
320+
- questions
321+
summary: Returns question categories
322+
description: Returns list of unique question categories
323+
responses:
324+
200:
325+
description: Successful Response
326+
content:
327+
application/json:
328+
schema:
329+
type: object
330+
properties:
331+
message:
332+
type: string
333+
description: Message
334+
categories:
335+
type: array
336+
items:
337+
type: string
338+
description: Categories
339+
404:
340+
description: Categories Not Found
194341
content:
195342
application/json:
196343
schema:

0 commit comments

Comments
 (0)