@@ -7,30 +7,31 @@ import {
7
7
getRandomQuestionService ,
8
8
searchQuestionsByTitleService ,
9
9
} from '@/services/get/index' ;
10
- import {
10
+ import type {
11
11
ICreateQuestionPayload ,
12
12
IDeleteQuestionPayload ,
13
13
IUpdateQuestionPayload ,
14
- } from '.. /services/post/types' ;
14
+ } from '@ /services/post/types' ;
15
15
16
16
import {
17
17
createQuestionService ,
18
18
deleteQuestionService ,
19
19
updateQuestionService ,
20
- } from '.. /services/post' ;
20
+ } from '@ /services/post' ;
21
21
import type {
22
22
IGetQuestionsPayload ,
23
23
IGetQuestionPayload ,
24
24
IGetRandomQuestionPayload ,
25
25
} from '@/services/get/types' ;
26
26
27
27
export const getQuestions = async ( req : Request , res : Response ) : Promise < Response > => {
28
+ const { questionName, difficulty, topic, pageNum, recordsPerPage } = req . query ;
28
29
const payload : IGetQuestionsPayload = {
29
- questionName : req . query . questionName as string ,
30
- difficulty : req . query . difficulty as string ,
31
- topic : req . query . topic as string [ ] ,
32
- pageNum : parseInt ( req . query . pageNum as string ) || 0 ,
33
- 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 ,
34
35
} ;
35
36
36
37
try {
@@ -101,40 +102,62 @@ export const searchQuestionsByTitle = async (req: Request, res: Response): Promi
101
102
const result = await searchQuestionsByTitleService ( title . toString ( ) , page , limit ) ;
102
103
return res . status ( result . code ) . json ( result ) ;
103
104
} catch ( error ) {
104
- return res . status ( 500 ) . json ( { success : false , message : 'An error occurred' , error } ) ;
105
+ return res
106
+ . status ( StatusCodes . INTERNAL_SERVER_ERROR )
107
+ . json ( { success : false , message : 'An error occurred' , error } ) ;
105
108
}
106
109
} ;
107
110
108
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
+
109
118
const payload : ICreateQuestionPayload = {
110
- title : req . body . title ,
111
- description : req . body . description ,
112
- difficulty : req . body . difficulty ,
113
- topics : req . body . topics ,
119
+ title,
120
+ description,
121
+ difficulty,
122
+ topics,
114
123
} ;
115
124
116
125
try {
117
126
const result = await createQuestionService ( payload ) ;
118
- return res . status ( result . code ) . json ( result ) ;
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 ) ;
119
133
} catch ( error ) {
120
- return res . status ( 500 ) . json ( { success : false , message : 'An error occurred' , error } ) ;
134
+ return res
135
+ . status ( StatusCodes . INTERNAL_SERVER_ERROR )
136
+ . json ( { success : false , message : 'An error occurred' , error } ) ;
121
137
}
122
138
} ;
123
139
124
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
+
125
146
const payload : IUpdateQuestionPayload = {
126
147
id : parseInt ( req . params . questionId ) ,
127
- title : req . body . title ,
128
- description : req . body . description ,
129
- difficulty : req . body . difficulty ,
130
- topics : req . body . topics ,
148
+ title,
149
+ description,
150
+ difficulty,
151
+ topics,
131
152
} ;
132
153
133
154
try {
134
155
const result = await updateQuestionService ( payload ) ;
135
156
return res . status ( result . code ) . json ( result ) ;
136
157
} catch ( error ) {
137
- return res . status ( 500 ) . json ( { success : false , message : 'An error occurred' , error } ) ;
158
+ return res
159
+ . status ( StatusCodes . INTERNAL_SERVER_ERROR )
160
+ . json ( { success : false , message : 'An error occurred' , error } ) ;
138
161
}
139
162
} ;
140
163
@@ -145,7 +168,7 @@ export const deleteQuestion = async (req: Request, res: Response): Promise<Respo
145
168
146
169
try {
147
170
const result = await deleteQuestionService ( payload ) ;
148
- return res . status ( result . code ) . json ( result ) ;
171
+ return res . status ( result . code ) . json ( result . success ? 'Ok' : result . message ) ;
149
172
} catch ( error ) {
150
173
return res
151
174
. status ( StatusCodes . INTERNAL_SERVER_ERROR )
0 commit comments