@@ -12,7 +12,7 @@ const createArticleController = (req, res, next) => {
12
12
if ( checkFields ) {
13
13
res . status ( 400 ) . json ( {
14
14
status : 'error' ,
15
- Error : 'Title and Article text are required' ,
15
+ error : 'Title and Article text are required' ,
16
16
} ) ;
17
17
} else {
18
18
findAllArticles ( )
@@ -49,7 +49,7 @@ const createArticleController = (req, res, next) => {
49
49
. catch ( ( ) => {
50
50
res . status ( 500 ) . json ( {
51
51
status : 'error' ,
52
- Error : 'Failed to get articles' ,
52
+ error : 'Failed to get articles' ,
53
53
} ) ;
54
54
} ) ;
55
55
}
@@ -61,7 +61,7 @@ const getArticlesController = (req, res, next) => {
61
61
if ( rows . length === 0 ) {
62
62
res . status ( 400 ) . json ( {
63
63
status : 'error' ,
64
- Error : 'There is no Article post yet' ,
64
+ error : 'There is no Article post yet' ,
65
65
} ) ;
66
66
} else {
67
67
const articlesArr = [ ] ;
@@ -92,7 +92,7 @@ const getArticlesController = (req, res, next) => {
92
92
. catch ( ( ) => {
93
93
res . status ( 500 ) . json ( {
94
94
status : 'error' ,
95
- Error : 'Failed to get articles' ,
95
+ error : 'Failed to get articles' ,
96
96
} ) ;
97
97
} ) ;
98
98
} ;
@@ -101,60 +101,67 @@ const getArticleController = (req, res, next) => {
101
101
if ( ! req . params . articleId || req . params . articleId === '' ) {
102
102
res . status ( 400 ) . json ( {
103
103
status : 'error' ,
104
- Error : 'Invalid request' ,
104
+ error : 'Invalid request' ,
105
105
} ) ;
106
106
} else {
107
107
const paramId = parseInt ( req . params . articleId , 10 ) ;
108
108
109
109
findOneArticle ( [ paramId ] )
110
110
. then ( ( row ) => {
111
- const {
112
- id, title, article, createdon, authorid,
113
- } = row ;
114
- const articleId = id ;
115
- const articleTitle = title ;
116
- const articleText = article ;
117
- const articleCreatedOn = createdon ;
118
- const articleAuthorId = authorid ;
119
-
120
- findArticleComments ( [ paramId ] )
121
- . then ( ( rows ) => {
122
- const commentArr = [ ] ;
123
- rows . forEach ( ( commentRow ) => {
124
- const { comment } = commentRow ;
125
- const commentText = comment ;
126
- const commentAuthorId = commentRow . authorid ;
127
-
128
- const values = {
129
- commentId : commentRow . id ,
130
- comment : commentText ,
131
- authorId : commentAuthorId ,
132
- } ;
133
- commentArr . push ( values ) ;
134
- } ) ;
135
- res . status ( 200 ) . json ( {
136
- status : 'success' ,
137
- data : {
138
- id : articleId ,
139
- createdOn : articleCreatedOn ,
140
- title : articleTitle ,
141
- article : articleText ,
142
- authorId : articleAuthorId ,
143
- comments : commentArr ,
144
- } ,
145
- } ) ;
146
- } )
147
- . catch ( ( ) => {
148
- res . status ( 500 ) . json ( {
149
- status : 'error' ,
150
- Error : 'Failed to get article comments' ,
151
- } ) ;
111
+ if ( row . length === 0 ) {
112
+ res . status ( 400 ) . json ( {
113
+ status : 'error' ,
114
+ error : 'Invalid request' ,
152
115
} ) ;
116
+ } else {
117
+ const {
118
+ id, title, article, createdon, authorid,
119
+ } = row ;
120
+ const articleId = id ;
121
+ const articleTitle = title ;
122
+ const articleText = article ;
123
+ const articleCreatedOn = createdon ;
124
+ const articleAuthorId = authorid ;
125
+
126
+ findArticleComments ( [ paramId ] )
127
+ . then ( ( rows ) => {
128
+ const commentArr = [ ] ;
129
+ rows . forEach ( ( commentRow ) => {
130
+ const { comment } = commentRow ;
131
+ const commentText = comment ;
132
+ const commentAuthorId = commentRow . authorid ;
133
+
134
+ const values = {
135
+ commentId : commentRow . id ,
136
+ comment : commentText ,
137
+ authorId : commentAuthorId ,
138
+ } ;
139
+ commentArr . push ( values ) ;
140
+ } ) ;
141
+ res . status ( 200 ) . json ( {
142
+ status : 'success' ,
143
+ data : {
144
+ id : articleId ,
145
+ createdOn : articleCreatedOn ,
146
+ title : articleTitle ,
147
+ article : articleText ,
148
+ authorId : articleAuthorId ,
149
+ comments : commentArr ,
150
+ } ,
151
+ } ) ;
152
+ } )
153
+ . catch ( ( ) => {
154
+ res . status ( 500 ) . json ( {
155
+ status : 'error' ,
156
+ error : 'Failed to get article comments' ,
157
+ } ) ;
158
+ } ) ;
159
+ }
153
160
} )
154
161
. catch ( ( ) => {
155
162
res . status ( 500 ) . json ( {
156
163
status : 'error' ,
157
- Error : 'Failed to get article' ,
164
+ error : 'Failed to get article' ,
158
165
} ) ;
159
166
} ) ;
160
167
}
@@ -166,13 +173,19 @@ const updateArticleController = (req, res, next) => {
166
173
if ( checkFields ) {
167
174
res . status ( 400 ) . json ( {
168
175
status : 'error' ,
169
- Error : 'Title and article fields are required' ,
176
+ error : 'Title and article fields are required' ,
170
177
} ) ;
171
178
} else {
172
179
const paramId = parseInt ( req . params . articleId , 10 ) ;
173
180
findOneArticle ( [ paramId ] )
174
- . then ( ( ) => {
175
- updateArticle ( [ req . body . title , req . body . article , paramId ] )
181
+ . then ( ( row ) => {
182
+ if ( row . length === 0 ) {
183
+ res . status ( 400 ) . json ( {
184
+ status : 'error' ,
185
+ error : 'Article does not exist' ,
186
+ } ) ;
187
+ } else {
188
+ updateArticle ( [ req . body . title , req . body . article , paramId ] )
176
189
. then ( ( row ) => {
177
190
const {
178
191
id, title, article, createdon, authorid,
@@ -182,7 +195,7 @@ const updateArticleController = (req, res, next) => {
182
195
const articleText = article ;
183
196
const articleCreatedOn = createdon ;
184
197
185
- res . status ( 200 ) . json ( {
198
+ res . status ( 201 ) . json ( {
186
199
status : 'success' ,
187
200
data : {
188
201
id : articleId ,
@@ -196,14 +209,15 @@ const updateArticleController = (req, res, next) => {
196
209
. catch ( ( ) => {
197
210
res . status ( 500 ) . json ( {
198
211
status : 'error' ,
199
- Error : 'Failed to update article' ,
212
+ error : 'Failed to update article' ,
200
213
} ) ;
201
214
} ) ;
215
+ }
202
216
} )
203
217
. catch ( ( ) => {
204
- res . status ( 400 ) . json ( {
218
+ res . status ( 500 ) . json ( {
205
219
status : 'error' ,
206
- Error : 'Failed to get article' ,
220
+ error : 'Failed to get article' ,
207
221
} ) ;
208
222
} ) ;
209
223
}
@@ -213,47 +227,52 @@ const deleteArticleController = (req, res, next) => {
213
227
if ( ! req . params . articleId || req . params . articleId === '' ) {
214
228
res . status ( 400 ) . json ( {
215
229
status : 'error' ,
216
- Error : 'Invalid request' ,
230
+ error : 'Invalid request' ,
217
231
} ) ;
218
232
} else {
219
233
const articleId = parseInt ( req . params . articleId , 10 ) ;
220
234
221
235
findOneArticle ( [ articleId ] )
222
- . then ( ( ) => {
223
- deleteArticle ( [ articleId ] )
224
- . then ( ( row ) => {
225
- const {
226
- id, title, article, createdon, authorid,
227
- } = row ;
228
-
229
- const articleTitle = title ;
230
- const articleText = article ;
231
- const articleCreatedOn = createdon ;
232
- const userId = authorid ;
233
-
234
- res . status ( 200 ) . json ( {
235
- status : 'success' ,
236
- data : {
237
- message : 'Article successfully deleted' ,
238
- Id : id ,
239
- title : articleTitle ,
240
- article : articleText ,
241
- createdOn : articleCreatedOn ,
242
- authorId : userId ,
243
- } ,
244
- } ) ;
245
- } )
246
- . catch ( ( ) => {
247
- res . status ( 500 ) . json ( {
236
+ . then ( ( row ) => {
237
+ if ( row . length === 0 ) {
238
+ res . status ( 400 ) . json ( {
248
239
status : 'error' ,
249
- Error : 'Failed to delete article ' ,
240
+ error : 'Invalid request ' ,
250
241
} ) ;
251
- } ) ;
242
+ } else { deleteArticle ( [ articleId ] )
243
+ . then ( ( row ) => {
244
+ const {
245
+ id, title, article, createdon, authorid,
246
+ } = row ;
247
+
248
+ const articleTitle = title ;
249
+ const articleText = article ;
250
+ const articleCreatedOn = createdon ;
251
+ const userId = authorid ;
252
+
253
+ res . status ( 200 ) . json ( {
254
+ status : 'success' ,
255
+ data : {
256
+ message : 'Article successfully deleted' ,
257
+ Id : id ,
258
+ title : articleTitle ,
259
+ article : articleText ,
260
+ createdOn : articleCreatedOn ,
261
+ authorId : userId ,
262
+ } ,
263
+ } ) ;
264
+ } )
265
+ . catch ( ( ) => {
266
+ res . status ( 500 ) . json ( {
267
+ status : 'error' ,
268
+ error : 'Failed to delete article' ,
269
+ } ) ;
270
+ } ) ; }
252
271
} )
253
272
. catch ( ( ) => {
254
- res . status ( 400 ) . json ( {
273
+ res . status ( 500 ) . json ( {
255
274
status : 'error' ,
256
- Error : 'Failed to get article' ,
275
+ error : 'Failed to get article' ,
257
276
} ) ;
258
277
} )
259
278
}
0 commit comments