@@ -5,10 +5,12 @@ import {
5
5
DUPLICATE_QUESTION_RESPONSE_MESSAGE ,
6
6
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE ,
7
7
QN_DESC_CHAR_LIMIT ,
8
- QN_CREATED ,
9
- QN_NOT_FOUND ,
10
- SERVER_ERROR ,
11
- QN_RETRIEVED ,
8
+ QN_CREATED_MESSAGE ,
9
+ QN_NOT_FOUND_MESSAGE ,
10
+ SERVER_ERROR_MESSAGE ,
11
+ QN_RETRIEVED_MESSAGE ,
12
+ PAGE_LIMIT_REQUIRED_MESSAGE ,
13
+ PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE ,
12
14
} from "../utils/constants.ts" ;
13
15
14
16
export const createQuestion = async (
@@ -43,11 +45,11 @@ export const createQuestion = async (
43
45
await newQuestion . save ( ) ;
44
46
45
47
res . status ( 201 ) . json ( {
46
- message : QN_CREATED ,
48
+ message : QN_CREATED_MESSAGE ,
47
49
question : newQuestion ,
48
50
} ) ;
49
51
} catch ( error ) {
50
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
52
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
51
53
}
52
54
} ;
53
55
@@ -61,7 +63,7 @@ export const updateQuestion = async (
61
63
62
64
const currentQuestion = await Question . findById ( id ) ;
63
65
if ( ! currentQuestion ) {
64
- res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
66
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
65
67
return ;
66
68
}
67
69
@@ -89,72 +91,74 @@ export const updateQuestion = async (
89
91
question : updatedQuestion ,
90
92
} ) ;
91
93
} catch ( error ) {
92
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
94
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
93
95
}
94
96
} ;
95
97
98
+ interface QnListSearchFilterParams {
99
+ page : string ;
100
+ qnLimit : string ;
101
+ title ?: string ;
102
+ complexities ?: string | string [ ] ;
103
+ categories ?: string | string [ ] ;
104
+ }
105
+
96
106
export const readQuestionsList = async (
97
- req : Request ,
107
+ req : Request < unknown , unknown , unknown , QnListSearchFilterParams > ,
98
108
res : Response ,
99
109
) : Promise < void > => {
100
110
try {
101
- const qnLimit = 10 ;
102
-
103
- const { currPage } = req . params ;
104
- const currPageInt = parseInt ( currPage ) ;
105
-
106
- if ( ! req . body || Object . keys ( req . body ) . length == 0 ) {
107
- const totalQuestions = await Question . countDocuments ( ) ;
108
- if ( totalQuestions == 0 ) {
109
- res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
110
- return ;
111
- }
112
- const totalPages = Math . ceil ( totalQuestions / qnLimit ) ;
113
-
114
- const currPageQuestions = await Question . find ( )
115
- . skip ( ( currPageInt - 1 ) * qnLimit )
116
- . limit ( qnLimit ) ;
117
-
118
- res . status ( 200 ) . json ( {
119
- message : QN_RETRIEVED ,
120
- pages : totalPages ,
121
- questions : currPageQuestions ,
122
- } ) ;
123
- } else {
124
- const { title, complexities, categories } = req . body ;
125
- const query : any = { } ;
126
-
127
- if ( title ) {
128
- query . title = { $regex : new RegExp ( title , "i" ) } ;
129
- }
130
-
131
- if ( complexities && complexities . length > 0 ) {
132
- query . complexity = { $in : complexities } ;
133
- }
134
-
135
- if ( categories && categories . length > 0 ) {
136
- query . category = { $in : categories } ;
137
- }
138
-
139
- const filteredTotalQuestions = await Question . countDocuments ( query ) ;
140
- if ( filteredTotalQuestions == 0 ) {
141
- res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
142
- return ;
143
- }
144
- const filteredTotalPages = Math . ceil ( filteredTotalQuestions / qnLimit ) ;
145
-
146
- const filteredQuestions = await Question . find ( query )
147
- . skip ( ( currPageInt - 1 ) * qnLimit )
148
- . limit ( qnLimit ) ;
149
-
150
- res . status ( 200 ) . json ( {
151
- message : QN_RETRIEVED ,
152
- pages : filteredTotalPages ,
153
- questions : filteredQuestions ,
154
- } ) ;
111
+ const { page, qnLimit, title, complexities, categories } = req . query ;
112
+
113
+ if ( ! page || ! qnLimit ) {
114
+ res . status ( 400 ) . json ( { message : PAGE_LIMIT_REQUIRED_MESSAGE } ) ;
115
+ return ;
116
+ }
117
+
118
+ const pageInt = parseInt ( page , 10 ) ;
119
+ const qnLimitInt = parseInt ( qnLimit , 10 ) ;
120
+
121
+ if ( pageInt < 1 || qnLimitInt < 1 ) {
122
+ res . status ( 400 ) . json ( { message : PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE } ) ;
123
+ return ;
124
+ }
125
+
126
+ const query : any = { } ;
127
+
128
+ if ( title ) {
129
+ query . title = { $regex : new RegExp ( title , "i" ) } ;
130
+ }
131
+
132
+ if ( complexities ) {
133
+ query . complexity = {
134
+ $in : Array . isArray ( complexities ) ? complexities : [ complexities ] ,
135
+ } ;
136
+ }
137
+
138
+ if ( categories ) {
139
+ query . category = {
140
+ $in : Array . isArray ( categories ) ? categories : [ categories ] ,
141
+ } ;
142
+ }
143
+
144
+ const filteredTotalQuestions = await Question . countDocuments ( query ) ;
145
+ if ( filteredTotalQuestions == 0 ) {
146
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
147
+ return ;
155
148
}
149
+ const filteredTotalPages = Math . ceil ( filteredTotalQuestions / qnLimitInt ) ;
150
+
151
+ const filteredQuestions = await Question . find ( query )
152
+ . skip ( ( pageInt - 1 ) * qnLimitInt )
153
+ . limit ( qnLimitInt ) ;
154
+
155
+ res . status ( 200 ) . json ( {
156
+ message : QN_RETRIEVED_MESSAGE ,
157
+ pages : filteredTotalPages ,
158
+ questions : filteredQuestions ,
159
+ } ) ;
156
160
} catch ( error ) {
157
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
161
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
158
162
}
159
163
} ;
160
164
@@ -167,14 +171,14 @@ export const readQuestionIndiv = async (
167
171
168
172
const questionDetails = await Question . findById ( id ) ;
169
173
if ( ! questionDetails ) {
170
- res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
174
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND_MESSAGE } ) ;
171
175
return ;
172
176
}
173
177
res . status ( 200 ) . json ( {
174
- message : QN_RETRIEVED ,
178
+ message : QN_RETRIEVED_MESSAGE ,
175
179
question : questionDetails ,
176
180
} ) ;
177
181
} catch ( error ) {
178
- res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
182
+ res . status ( 500 ) . json ( { message : SERVER_ERROR_MESSAGE , error } ) ;
179
183
}
180
184
} ;
0 commit comments