@@ -7,30 +7,31 @@ import {
77 getRandomQuestionService ,
88 searchQuestionsByTitleService ,
99} from '@/services/get/index' ;
10- import {
10+ import type {
1111 ICreateQuestionPayload ,
1212 IDeleteQuestionPayload ,
1313 IUpdateQuestionPayload ,
14- } from '.. /services/post/types' ;
14+ } from '@ /services/post/types' ;
1515
1616import {
1717 createQuestionService ,
1818 deleteQuestionService ,
1919 updateQuestionService ,
20- } from '.. /services/post' ;
20+ } from '@ /services/post' ;
2121import type {
2222 IGetQuestionsPayload ,
2323 IGetQuestionPayload ,
2424 IGetRandomQuestionPayload ,
2525} from '@/services/get/types' ;
2626
2727export const getQuestions = async ( req : Request , res : Response ) : Promise < Response > => {
28+ const { questionName, difficulty, topic, pageNum, recordsPerPage } = req . query ;
2829 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 ,
3435 } ;
3536
3637 try {
@@ -101,40 +102,62 @@ export const searchQuestionsByTitle = async (req: Request, res: Response): Promi
101102 const result = await searchQuestionsByTitleService ( title . toString ( ) , page , limit ) ;
102103 return res . status ( result . code ) . json ( result ) ;
103104 } 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 } ) ;
105108 }
106109} ;
107110
108111export 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+
109118 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,
114123 } ;
115124
116125 try {
117126 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 ) ;
119133 } 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 } ) ;
121137 }
122138} ;
123139
124140export 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+
125146 const payload : IUpdateQuestionPayload = {
126147 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,
131152 } ;
132153
133154 try {
134155 const result = await updateQuestionService ( payload ) ;
135156 return res . status ( result . code ) . json ( result ) ;
136157 } 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 } ) ;
138161 }
139162} ;
140163
@@ -145,7 +168,7 @@ export const deleteQuestion = async (req: Request, res: Response): Promise<Respo
145168
146169 try {
147170 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 ) ;
149172 } catch ( error ) {
150173 return res
151174 . status ( StatusCodes . INTERNAL_SERVER_ERROR )
0 commit comments