Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51261c5

Browse files
committed
[finished] debuging article tests
1 parent 735a0dc commit 51261c5

File tree

3 files changed

+275
-86
lines changed

3 files changed

+275
-86
lines changed

api/v1/controllers/article.js

Lines changed: 104 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const createArticleController = (req, res, next) => {
1212
if (checkFields) {
1313
res.status(400).json({
1414
status: 'error',
15-
Error: 'Title and Article text are required',
15+
error: 'Title and Article text are required',
1616
});
1717
} else {
1818
findAllArticles()
@@ -49,7 +49,7 @@ const createArticleController = (req, res, next) => {
4949
.catch(() => {
5050
res.status(500).json({
5151
status: 'error',
52-
Error: 'Failed to get articles',
52+
error: 'Failed to get articles',
5353
});
5454
});
5555
}
@@ -61,7 +61,7 @@ const getArticlesController = (req, res, next) => {
6161
if (rows.length === 0) {
6262
res.status(400).json({
6363
status: 'error',
64-
Error: 'There is no Article post yet',
64+
error: 'There is no Article post yet',
6565
});
6666
} else {
6767
const articlesArr = [];
@@ -92,7 +92,7 @@ const getArticlesController = (req, res, next) => {
9292
.catch(() => {
9393
res.status(500).json({
9494
status: 'error',
95-
Error: 'Failed to get articles',
95+
error: 'Failed to get articles',
9696
});
9797
});
9898
};
@@ -101,60 +101,67 @@ const getArticleController = (req, res, next) => {
101101
if (!req.params.articleId || req.params.articleId === '') {
102102
res.status(400).json({
103103
status: 'error',
104-
Error: 'Invalid request',
104+
error: 'Invalid request',
105105
});
106106
} else {
107107
const paramId = parseInt(req.params.articleId, 10);
108108

109109
findOneArticle([paramId])
110110
.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',
152115
});
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+
}
153160
})
154161
.catch(() => {
155162
res.status(500).json({
156163
status: 'error',
157-
Error: 'Failed to get article',
164+
error: 'Failed to get article',
158165
});
159166
});
160167
}
@@ -166,13 +173,19 @@ const updateArticleController = (req, res, next) => {
166173
if (checkFields) {
167174
res.status(400).json({
168175
status: 'error',
169-
Error: 'Title and article fields are required',
176+
error: 'Title and article fields are required',
170177
});
171178
} else {
172179
const paramId = parseInt(req.params.articleId, 10);
173180
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])
176189
.then((row) => {
177190
const {
178191
id, title, article, createdon, authorid,
@@ -182,7 +195,7 @@ const updateArticleController = (req, res, next) => {
182195
const articleText = article;
183196
const articleCreatedOn = createdon;
184197

185-
res.status(200).json({
198+
res.status(201).json({
186199
status: 'success',
187200
data: {
188201
id: articleId,
@@ -196,14 +209,15 @@ const updateArticleController = (req, res, next) => {
196209
.catch(() => {
197210
res.status(500).json({
198211
status: 'error',
199-
Error: 'Failed to update article',
212+
error: 'Failed to update article',
200213
});
201214
});
215+
}
202216
})
203217
.catch(() =>{
204-
res.status(400).json({
218+
res.status(500).json({
205219
status: 'error',
206-
Error: 'Failed to get article',
220+
error: 'Failed to get article',
207221
});
208222
});
209223
}
@@ -213,47 +227,52 @@ const deleteArticleController = (req, res, next) => {
213227
if (!req.params.articleId || req.params.articleId === '') {
214228
res.status(400).json({
215229
status: 'error',
216-
Error: 'Invalid request',
230+
error: 'Invalid request',
217231
});
218232
} else {
219233
const articleId = parseInt(req.params.articleId, 10);
220234

221235
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({
248239
status: 'error',
249-
Error: 'Failed to delete article',
240+
error: 'Invalid request',
250241
});
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+
});}
252271
})
253272
.catch(() => {
254-
res.status(400).json({
273+
res.status(500).json({
255274
status: 'error',
256-
Error: 'Failed to get article',
275+
error: 'Failed to get article',
257276
});
258277
})
259278
}

0 commit comments

Comments
 (0)