Skip to content

Commit 435a293

Browse files
committed
Add delete endpoint and document the apis
1 parent 2176d08 commit 435a293

File tree

5 files changed

+215
-4
lines changed

5 files changed

+215
-4
lines changed

backend/question-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"start": "tsx server.ts",
8+
"dev": "tsx watch server.ts",
89
"test": "echo \"Error: no test specified\" && exit 1",
910
"lint": "eslint ."
1011
},

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
66
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
77
QN_DESC_CHAR_LIMIT,
8+
QN_NOT_FOUND,
9+
QN_DELETED,
10+
SERVER_ERROR,
811
} from "../utils/constants.ts";
912

1013
export const createQuestion = async (
@@ -43,7 +46,7 @@ export const createQuestion = async (
4346
question: newQuestion,
4447
});
4548
} catch (error) {
46-
res.status(500).json({ message: "Server error", error });
49+
res.status(500).json({ message: SERVER_ERROR, error });
4750
}
4851
};
4952

@@ -57,7 +60,7 @@ export const updateQuestion = async (
5760

5861
const currentQuestion = await Question.findById(id);
5962
if (!currentQuestion) {
60-
res.status(404).json({ message: "Question not found" });
63+
res.status(404).json({ message: QN_NOT_FOUND });
6164
return;
6265
}
6366

@@ -85,6 +88,25 @@ export const updateQuestion = async (
8588
question: updatedQuestion,
8689
});
8790
} catch (error) {
88-
res.status(500).json({ message: "Server error", error });
91+
res.status(500).json({ message: SERVER_ERROR, error });
92+
}
93+
};
94+
95+
export const deleteQuestion = async (
96+
req: Request,
97+
res: Response,
98+
): Promise<void> => {
99+
try {
100+
const { id } = req.params;
101+
const currentQuestion = await Question.findById(id);
102+
if (!currentQuestion) {
103+
res.status(400).json({ message: QN_NOT_FOUND });
104+
return;
105+
}
106+
107+
await Question.findByIdAndDelete(id);
108+
res.status(200).json({ message: QN_DELETED });
109+
} catch (error) {
110+
res.send(500).json({ message: SERVER_ERROR, error });
89111
}
90112
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from "express";
22
import {
33
createQuestion,
4+
deleteQuestion,
45
updateQuestion,
56
} from "../controllers/questionController.ts";
67

@@ -10,4 +11,6 @@ router.post("/questions", createQuestion);
1011

1112
router.put("/questions/:id", updateQuestion);
1213

14+
router.delete("/questions/:id", deleteQuestion);
15+
1316
export default router;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ 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_NOT_FOUND = "Question not found.";
10+
11+
export const QN_DELETED = "Question deleted successfully.";
12+
13+
export const SERVER_ERROR = "Server error.";

backend/question-service/swagger.yml

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,71 @@ info:
44
title: Question Service
55
version: 1.0.0
66

7+
components:
8+
schemas:
9+
Question:
10+
properties:
11+
title:
12+
type: string
13+
description: Title
14+
description:
15+
type: string
16+
description: Description
17+
complexity:
18+
type: string
19+
description: Complexity - Easy, Medium, Hard
20+
category:
21+
type: array
22+
items:
23+
type: string
24+
description: Categories
25+
26+
definitions:
27+
Question:
28+
type: object
29+
properties:
30+
_id:
31+
type: string
32+
description: Question id
33+
title:
34+
type: string
35+
description: Title
36+
description:
37+
type: string
38+
description: Description
39+
complexity:
40+
type: string
41+
description: Complexity - Easy, Medium, Hard
42+
category:
43+
type: array
44+
items:
45+
type: string
46+
description: Categories
47+
createdAt:
48+
type: string
49+
description: Date of creation
50+
updatedAt:
51+
type: string
52+
description: Latest update
53+
__v:
54+
type: string
55+
description: Document version
56+
Error:
57+
type: object
58+
properties:
59+
message:
60+
type: string
61+
deescription: Message
62+
ServerError:
63+
type: object
64+
properties:
65+
message:
66+
type: string
67+
description: Message
68+
error:
69+
type: string
70+
description: Error
71+
772
paths:
873
/:
974
get:
@@ -12,7 +77,109 @@ paths:
1277
summary: Root
1378
description: Ping the server
1479
responses:
15-
"200":
80+
200:
81+
description: Successful Response
82+
content:
83+
application/json:
84+
schema:
85+
type: object
86+
properties:
87+
message:
88+
type: string
89+
description: Message
90+
/api/questions:
91+
post:
92+
tags:
93+
- questions
94+
summary: Creates a question
95+
description: Creates a question
96+
requestBody:
97+
required: true
98+
content:
99+
application/json:
100+
schema:
101+
$ref: "#/components/schemas/Question"
102+
responses:
103+
201:
104+
description: Created
105+
content:
106+
application/json:
107+
schema:
108+
type: object
109+
properties:
110+
message:
111+
type: string
112+
description: Message
113+
question:
114+
$ref: "#/definitions/Question"
115+
400:
116+
description: Bad Request
117+
content:
118+
application/json:
119+
schema:
120+
$ref: "#/definitions/Error"
121+
500:
122+
description: Internal Server Error
123+
content:
124+
application/json:
125+
schema:
126+
$ref: "#/definitions/ServerError"
127+
/api/questions/{id}:
128+
put:
129+
tags:
130+
- questions
131+
summary: Updates a question
132+
description: Updates a question
133+
parameters:
134+
- in: path
135+
name: id
136+
type: string
137+
required: true
138+
description: Question id
139+
requestBody:
140+
required: true
141+
content:
142+
application/json:
143+
schema:
144+
$ref: "#/components/schemas/Question"
145+
responses:
146+
200:
147+
description: Successful Response
148+
content:
149+
application/json:
150+
schema:
151+
type: object
152+
properties:
153+
message:
154+
type: string
155+
description: Message
156+
question:
157+
$ref: "#/definitions/Question"
158+
400:
159+
description: Bad Request
160+
content:
161+
application/json:
162+
schema:
163+
$ref: "#/definitions/Error"
164+
500:
165+
description: Internal Server Error
166+
content:
167+
application/json:
168+
schema:
169+
$ref: "#/definitions/ServerError"
170+
delete:
171+
tags:
172+
- questions
173+
summary: Deletes a question
174+
description: Deletes a question
175+
parameters:
176+
- in: path
177+
name: id
178+
type: string
179+
required: true
180+
description: Question id
181+
responses:
182+
200:
16183
description: Successful Response
17184
content:
18185
application/json:
@@ -22,3 +189,15 @@ paths:
22189
message:
23190
type: string
24191
description: Message
192+
400:
193+
description: Bad Request
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"

0 commit comments

Comments
 (0)