@@ -7,19 +7,31 @@ import {
77 getRandomQuestionService ,
88 searchQuestionsByTitleService ,
99} 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' ;
1021import type {
1122 IGetQuestionsPayload ,
1223 IGetQuestionPayload ,
1324 IGetRandomQuestionPayload ,
1425} from '@/services/get/types' ;
1526
1627export const getQuestions = async ( req : Request , res : Response ) : Promise < Response > => {
28+ const { questionName, difficulty, topic, pageNum, recordsPerPage } = req . query ;
1729 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 ,
2335 } ;
2436
2537 try {
@@ -67,6 +79,8 @@ export const getRandomQuestion = async (req: Request, res: Response): Promise<Re
6779 const result = await getRandomQuestionService ( payload ) ;
6880 return res . status ( result . code ) . json ( result ) ;
6981 } catch ( error ) {
82+ console . log ( 'error' , error ) ;
83+
7084 return res
7185 . status ( StatusCodes . INTERNAL_SERVER_ERROR )
7286 . json ( { success : false , message : 'An error occurred' , error } ) ;
@@ -93,3 +107,71 @@ export const searchQuestionsByTitle = async (req: Request, res: Response): Promi
93107 . json ( { success : false , message : 'An error occurred' , error } ) ;
94108 }
95109} ;
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