@@ -5,9 +5,15 @@ import {
5
5
DUPLICATE_QUESTION_RESPONSE_MESSAGE ,
6
6
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE ,
7
7
QN_DESC_CHAR_LIMIT ,
8
- QN_NOT_FOUND ,
9
- QN_DELETED ,
10
- SERVER_ERROR ,
8
+ QN_CREATED_MESSAGE ,
9
+ QN_NOT_FOUND_MESSAGE ,
10
+ QN_DELETED_MESSAGE ,
11
+ SERVER_ERROR_MESSAGE ,
12
+ QN_RETRIEVED_MESSAGE ,
13
+ PAGE_LIMIT_REQUIRED_MESSAGE ,
14
+ PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ,
15
+ CATEGORIES_NOT_FOUND_MESSAGE ,
16
+ CATEGORIES_RETRIEVED_MESSAGE ,
11
17
} from "../utils/constants.ts" ;
12
18
13
19
import { upload } from "../../config/multer" ;
@@ -45,11 +51,11 @@ export const createQuestion = async (
45
51
await newQuestion . save ( ) ;
46
52
47
53
res . status ( 201 ) . json ( {
48
- message : "Question created successfully" ,
54
+ message : QN_CREATED_MESSAGE ,
49
55
question : newQuestion ,
50
56
} ) ;
51
57
} catch ( error ) {
52
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
58
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
53
59
}
54
60
} ;
55
61
@@ -92,7 +98,7 @@ export const updateQuestion = async (
92
98
93
99
const currentQuestion = await Question . findById ( id ) ;
94
100
if ( ! currentQuestion ) {
95
- res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
101
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
96
102
return ;
97
103
}
98
104
@@ -120,7 +126,7 @@ export const updateQuestion = async (
120
126
question : updatedQuestion ,
121
127
} ) ;
122
128
} catch ( error ) {
123
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
129
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
124
130
}
125
131
} ;
126
132
@@ -132,13 +138,120 @@ export const deleteQuestion = async (
132
138
const { id } = req . params ;
133
139
const currentQuestion = await Question . findById ( id ) ;
134
140
if ( ! currentQuestion ) {
135
- res . status ( 400 ) . json ( { message : QN_NOT_FOUND } ) ;
141
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
136
142
return ;
137
143
}
138
144
139
145
await Question . findByIdAndDelete ( id ) ;
140
- res . status ( 200 ) . json ( { message : QN_DELETED } ) ;
146
+ res . status ( 200 ) . json ( { message : QN_DELETED_MESSAGE } ) ;
141
147
} catch ( error ) {
142
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
148
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
149
+ }
150
+ } ;
151
+
152
+ interface QnListSearchFilterParams {
153
+ page : string ;
154
+ qnLimit : string ;
155
+ title ?: string ;
156
+ complexities ?: string | string [ ] ;
157
+ categories ?: string | string [ ] ;
158
+ }
159
+
160
+ export const readQuestionsList = async (
161
+ req : Request < unknown , unknown , unknown , QnListSearchFilterParams > ,
162
+ res : Response ,
163
+ ) : Promise < void > => {
164
+ try {
165
+ const { page, qnLimit, title, complexities, categories } = req . query ;
166
+
167
+ if ( ! page || ! qnLimit ) {
168
+ res . status ( 400 ) . json ( { message : PAGE_LIMIT_REQUIRED_MESSAGE } ) ;
169
+ return ;
170
+ }
171
+
172
+ const pageInt = parseInt ( page , 10 ) ;
173
+ const qnLimitInt = parseInt ( qnLimit , 10 ) ;
174
+
175
+ if ( pageInt < 1 || qnLimitInt < 1 ) {
176
+ res . status ( 400 ) . json ( { message : PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE } ) ;
177
+ return ;
178
+ }
179
+
180
+ const query : any = { } ;
181
+
182
+ if ( title ) {
183
+ query . title = { $regex : new RegExp ( title , "i" ) } ;
184
+ }
185
+
186
+ if ( complexities ) {
187
+ query . complexity = {
188
+ $in : Array . isArray ( complexities ) ? complexities : [ complexities ] ,
189
+ } ;
190
+ }
191
+
192
+ if ( categories ) {
193
+ query . category = {
194
+ $in : Array . isArray ( categories ) ? categories : [ categories ] ,
195
+ } ;
196
+ }
197
+
198
+ const filteredTotalQuestions = await Question . countDocuments ( query ) ;
199
+ if ( filteredTotalQuestions == 0 ) {
200
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
201
+ return ;
202
+ }
203
+
204
+ const filteredQuestions = await Question . find ( query )
205
+ . skip ( ( pageInt - 1 ) * qnLimitInt )
206
+ . limit ( qnLimitInt ) ;
207
+
208
+ res . status ( 200 ) . json ( {
209
+ message : QN_RETRIEVED_MESSAGE ,
210
+ totalQns : filteredTotalQuestions ,
211
+ questions : filteredQuestions ,
212
+ } ) ;
213
+ } catch ( error ) {
214
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
215
+ }
216
+ } ;
217
+
218
+ export const readQuestionIndiv = async (
219
+ req : Request ,
220
+ res : Response ,
221
+ ) : Promise < void > => {
222
+ try {
223
+ const { id } = req . params ;
224
+
225
+ const questionDetails = await Question . findById ( id ) ;
226
+ if ( ! questionDetails ) {
227
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
228
+ return ;
229
+ }
230
+
231
+ res . status ( 200 ) . json ( {
232
+ message : QN_RETRIEVED_MESSAGE ,
233
+ question : questionDetails ,
234
+ } ) ;
235
+ } catch ( error ) {
236
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
237
+ }
238
+ } ;
239
+
240
+ export const readCategories = async (
241
+ req : Request ,
242
+ res : Response ,
243
+ ) : Promise < void > => {
244
+ try {
245
+ const uniqueCats = await Question . distinct ( "category" ) ;
246
+ if ( ! uniqueCats || uniqueCats . length == 0 ) {
247
+ res . status ( 404 ) . json ( { message : CATEGORIES_NOT_FOUND_MESSAGE } ) ;
248
+ }
249
+
250
+ res . status ( 200 ) . json ( {
251
+ message : CATEGORIES_RETRIEVED_MESSAGE ,
252
+ categories : uniqueCats ,
253
+ } ) ;
254
+ } catch ( error ) {
255
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
143
256
}
144
257
} ;
0 commit comments