Skip to content

Commit 61198af

Browse files
committed
Refactor question controller and services
This commit refactors the question controller and services in the backend. It introduces new functions for creating, updating, and deleting questions. The code changes include adding new imports, defining new payload interfaces, and implementing the corresponding service functions. This refactoring improves the organization and maintainability of the codebase.
1 parent 7b39e5f commit 61198af

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

backend/question/src/controller/question-controller.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import {
77
getRandomQuestionService,
88
searchQuestionsByTitleService,
99
} from '@/services/get/index';
10+
import {
11+
ICreateQuestionPayload,
12+
IDeleteQuestionPayload,
13+
IUpdateQuestionPayload,
14+
} from '../services/post/types';
15+
16+
import {
17+
createQuestionService,
18+
deleteQuestionService,
19+
updateQuestionService,
20+
} from '../services/post';
1021
import type {
1122
IGetQuestionsPayload,
1223
IGetQuestionPayload,
@@ -87,6 +98,52 @@ export const searchQuestionsByTitle = async (req: Request, res: Response): Promi
8798
try {
8899
const result = await searchQuestionsByTitleService(title.toString(), page, limit);
89100
return res.status(result.code).json(result);
101+
} catch (error) {
102+
return res.status(500).json({ success: false, message: 'An error occurred', error });
103+
}
104+
};
105+
106+
export const createQuestion = async (req: Request, res: Response): Promise<Response> => {
107+
const payload: ICreateQuestionPayload = {
108+
title: req.body.title,
109+
description: req.body.description,
110+
difficulty: req.body.difficulty,
111+
topics: req.body.topics,
112+
};
113+
114+
try {
115+
const result = await createQuestionService(payload);
116+
return res.status(result.code).json(result);
117+
} catch (error) {
118+
return res.status(500).json({ success: false, message: 'An error occurred', error });
119+
}
120+
};
121+
122+
export const updateQuestion = async (req: Request, res: Response): Promise<Response> => {
123+
const payload: IUpdateQuestionPayload = {
124+
id: parseInt(req.params.questionId),
125+
title: req.body.title,
126+
description: req.body.description,
127+
difficulty: req.body.difficulty,
128+
topics: req.body.topics,
129+
};
130+
131+
try {
132+
const result = await updateQuestionService(payload);
133+
return res.status(result.code).json(result);
134+
} catch (error) {
135+
return res.status(500).json({ success: false, message: 'An error occurred', error });
136+
}
137+
};
138+
139+
export const deleteQuestion = async (req: Request, res: Response): Promise<Response> => {
140+
const payload: IDeleteQuestionPayload = {
141+
id: parseInt(req.params.questionId),
142+
};
143+
144+
try {
145+
const result = await deleteQuestionService(payload);
146+
return res.status(result.code).json(result);
90147
} catch (error) {
91148
return res
92149
.status(StatusCodes.INTERNAL_SERVER_ERROR)

backend/question/src/routes/question.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
getQuestions,
66
getQuestionDetails,
77
getRandomQuestion,
8+
createQuestion,
9+
updateQuestion,
10+
deleteQuestion,
811
} from '@/controller/question-controller';
912

1013
const router = Router();
@@ -17,4 +20,8 @@ router.get('/:questionId', getQuestionDetails);
1720

1821
router.post('/random', getRandomQuestion);
1922

23+
router.post('/questions', createQuestion);
24+
router.put('/questions/:questionId', updateQuestion);
25+
router.delete('/questions/:questionId', deleteQuestion);
26+
2027
export default router;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { db } from '../../lib/db/index';
2+
import { eq } from 'drizzle-orm';
3+
import { questions } from '../../lib/db/schema';
4+
import { ICreateQuestionPayload, IUpdateQuestionPayload, IDeleteQuestionPayload } from './types';
5+
6+
export const createQuestionService = async (payload: ICreateQuestionPayload) => {
7+
try {
8+
const [newQuestion] = await db
9+
.insert(questions)
10+
.values({
11+
title: payload.title,
12+
description: payload.description,
13+
difficulty: payload.difficulty,
14+
topic: payload.topics.map(String),
15+
})
16+
.returning();
17+
18+
return { success: true, code: 201, data: newQuestion };
19+
} catch (error) {
20+
console.error('Error creating question:', error);
21+
return { success: false, code: 500, message: 'Failed to create question' };
22+
}
23+
};
24+
25+
export const updateQuestionService = async (payload: IUpdateQuestionPayload) => {
26+
try {
27+
const [updatedQuestion] = await db
28+
.update(questions)
29+
.set({
30+
title: payload.title,
31+
description: payload.description,
32+
difficulty: payload.difficulty,
33+
topic: payload.topics.map(String),
34+
})
35+
.where(eq(questions.id, String(payload.id)))
36+
.returning();
37+
38+
if (!updatedQuestion) {
39+
return { success: false, code: 404, message: 'Question not found' };
40+
}
41+
42+
return { success: true, code: 200, data: updatedQuestion };
43+
} catch (error) {
44+
console.error('Error updating question:', error);
45+
return { success: false, code: 500, message: 'Failed to update question' };
46+
}
47+
};
48+
49+
export const deleteQuestionService = async (payload: IDeleteQuestionPayload) => {
50+
try {
51+
const [deletedQuestion] = await db
52+
.delete(questions)
53+
.where(eq(questions.id, String(payload.id)))
54+
.returning();
55+
56+
if (!deletedQuestion) {
57+
return { success: false, code: 404, message: 'Question not found' };
58+
}
59+
60+
return { success: true, code: 200, message: 'Question deleted successfully' };
61+
} catch (error) {
62+
console.error('Error deleting question:', error);
63+
return { success: false, code: 500, message: 'Failed to delete question' };
64+
}
65+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface ICreateQuestionPayload {
2+
title: string;
3+
description: string;
4+
difficulty: string;
5+
topics: number[];
6+
}
7+
8+
export interface IUpdateQuestionPayload extends ICreateQuestionPayload {
9+
id: number;
10+
}
11+
12+
export interface IDeleteQuestionPayload {
13+
id: number;
14+
}

0 commit comments

Comments
 (0)