@@ -7,19 +7,31 @@ import {
7
7
getRandomQuestionService ,
8
8
searchQuestionsByTitleService ,
9
9
} from '@/services/get/index' ;
10
+ import type {
11
+ ICreateQuestionPayload ,
12
+ IDeleteQuestionPayload ,
13
+ IUpdateQuestionPayload ,
14
+ } from '@/services/post/types' ;
15
+
16
+ import {
17
+ createQuestionService ,
18
+ deleteQuestionService ,
19
+ updateQuestionService ,
20
+ } from '@/services/post' ;
10
21
import type {
11
22
IGetQuestionsPayload ,
12
23
IGetQuestionPayload ,
13
24
IGetRandomQuestionPayload ,
14
25
} from '@/services/get/types' ;
15
26
16
27
export const getQuestions = async ( req : Request , res : Response ) : Promise < Response > => {
28
+ const { questionName, difficulty, topic, pageNum, recordsPerPage } = req . query ;
17
29
const payload : IGetQuestionsPayload = {
18
- questionName : req . query . questionName as string ,
19
- difficulty : req . query . difficulty as string ,
20
- topic : req . query . topic as string [ ] ,
21
- pageNum : parseInt ( req . query . pageNum as string ) || 0 ,
22
- recordsPerPage : parseInt ( req . query . recordsPerPage as string ) || 20 ,
30
+ questionName : questionName as string ,
31
+ difficulty : difficulty as string ,
32
+ topic : topic as string [ ] ,
33
+ pageNum : parseInt ( pageNum as string ) || 0 ,
34
+ recordsPerPage : parseInt ( recordsPerPage as string ) || 20 ,
23
35
} ;
24
36
25
37
try {
@@ -67,6 +79,8 @@ export const getRandomQuestion = async (req: Request, res: Response): Promise<Re
67
79
const result = await getRandomQuestionService ( payload ) ;
68
80
return res . status ( result . code ) . json ( result ) ;
69
81
} catch ( error ) {
82
+ console . log ( 'error' , error ) ;
83
+
70
84
return res
71
85
. status ( StatusCodes . INTERNAL_SERVER_ERROR )
72
86
. json ( { success : false , message : 'An error occurred' , error } ) ;
@@ -93,3 +107,71 @@ export const searchQuestionsByTitle = async (req: Request, res: Response): Promi
93
107
. json ( { success : false , message : 'An error occurred' , error } ) ;
94
108
}
95
109
} ;
110
+
111
+ export const createQuestion = async ( req : Request , res : Response ) : Promise < Response > => {
112
+ const { title, description, difficulty, topics } = req . body ;
113
+
114
+ if ( ! title || ! description || ! difficulty ) {
115
+ return res . status ( StatusCodes . UNPROCESSABLE_ENTITY ) . json ( 'Malformed' ) ;
116
+ }
117
+
118
+ const payload : ICreateQuestionPayload = {
119
+ title,
120
+ description,
121
+ difficulty,
122
+ topics,
123
+ } ;
124
+
125
+ try {
126
+ const result = await createQuestionService ( payload ) ;
127
+ if ( ! result . data || result . code >= 400 ) {
128
+ return res . status ( result . code ) . json ( {
129
+ message : result . message ?? 'An error occurred' ,
130
+ } ) ;
131
+ }
132
+ return res . status ( result . code ) . json ( result . data ) ;
133
+ } catch ( error ) {
134
+ return res
135
+ . status ( StatusCodes . INTERNAL_SERVER_ERROR )
136
+ . json ( { success : false , message : 'An error occurred' , error } ) ;
137
+ }
138
+ } ;
139
+
140
+ export const updateQuestion = async ( req : Request , res : Response ) : Promise < Response > => {
141
+ const { title, description, difficulty, topics } = req . body ;
142
+ if ( ! title && ! description && ! difficulty && ( ! topics || ! Array . isArray ( topics ) ) ) {
143
+ return res . status ( StatusCodes . UNPROCESSABLE_ENTITY ) . json ( 'Malformed' ) ;
144
+ }
145
+
146
+ const payload : IUpdateQuestionPayload = {
147
+ id : parseInt ( req . params . questionId ) ,
148
+ title,
149
+ description,
150
+ difficulty,
151
+ topics,
152
+ } ;
153
+
154
+ try {
155
+ const result = await updateQuestionService ( payload ) ;
156
+ return res . status ( result . code ) . json ( result ) ;
157
+ } catch ( error ) {
158
+ return res
159
+ . status ( StatusCodes . INTERNAL_SERVER_ERROR )
160
+ . json ( { success : false , message : 'An error occurred' , error } ) ;
161
+ }
162
+ } ;
163
+
164
+ export const deleteQuestion = async ( req : Request , res : Response ) : Promise < Response > => {
165
+ const payload : IDeleteQuestionPayload = {
166
+ id : parseInt ( req . params . questionId ) ,
167
+ } ;
168
+
169
+ try {
170
+ const result = await deleteQuestionService ( payload ) ;
171
+ return res . status ( result . code ) . json ( result . success ? 'Ok' : result . message ) ;
172
+ } catch ( error ) {
173
+ return res
174
+ . status ( StatusCodes . INTERNAL_SERVER_ERROR )
175
+ . json ( { success : false , message : 'An error occurred' , error } ) ;
176
+ }
177
+ } ;
0 commit comments