@@ -5,6 +5,10 @@ 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
12
} from "../utils/constants.ts" ;
9
13
10
14
export const createQuestion = async (
@@ -39,11 +43,11 @@ export const createQuestion = async (
39
43
await newQuestion . save ( ) ;
40
44
41
45
res . status ( 201 ) . json ( {
42
- message : "Question created successfully" ,
46
+ message : QN_CREATED ,
43
47
question : newQuestion ,
44
48
} ) ;
45
49
} catch ( error ) {
46
- res . status ( 500 ) . json ( { message : "Server error" , error } ) ;
50
+ res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
47
51
}
48
52
} ;
49
53
@@ -57,7 +61,7 @@ export const updateQuestion = async (
57
61
58
62
const currentQuestion = await Question . findById ( id ) ;
59
63
if ( ! currentQuestion ) {
60
- res . status ( 404 ) . json ( { message : "Question not found" } ) ;
64
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
61
65
return ;
62
66
}
63
67
@@ -85,6 +89,92 @@ export const updateQuestion = async (
85
89
question : updatedQuestion ,
86
90
} ) ;
87
91
} catch ( error ) {
88
- res . status ( 500 ) . json ( { message : "Server error" , error } ) ;
92
+ res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
93
+ }
94
+ } ;
95
+
96
+ export const readQuestionsList = async (
97
+ req : Request ,
98
+ res : Response ,
99
+ ) : Promise < void > => {
100
+ 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
+ } ) ;
155
+ }
156
+ } catch ( error ) {
157
+ res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
158
+ }
159
+ } ;
160
+
161
+ export const readQuestionIndiv = async (
162
+ req : Request ,
163
+ res : Response ,
164
+ ) : Promise < void > => {
165
+ try {
166
+ const { id } = req . params ;
167
+
168
+ const questionDetails = await Question . findById ( id ) ;
169
+ if ( ! questionDetails ) {
170
+ res . status ( 404 ) . json ( { message : QN_NOT_FOUND } ) ;
171
+ return ;
172
+ }
173
+ res . status ( 200 ) . json ( {
174
+ message : QN_RETRIEVED ,
175
+ question : questionDetails ,
176
+ } ) ;
177
+ } catch ( error ) {
178
+ res . status ( 500 ) . json ( { message : SERVER_ERROR , error } ) ;
89
179
}
90
180
} ;
0 commit comments