Skip to content

Commit

Permalink
bug(articles):fixed get specific user articles with readtime&createdA…
Browse files Browse the repository at this point in the history
…t [finisheds#169311398]
  • Loading branch information
nshutijonathan committed Oct 23, 2019
1 parent 9f82434 commit eb39c71
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
76 changes: 56 additions & 20 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,23 @@ class Articles {
])
);

await Promise.all(allArticles.map(async (article) => {
try {
const userDetails = await Userservice.getOneUser(article.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
return true;
} catch (error) {
throw (error);
}
}));
await Promise.all(
allArticles.map(async (article) => {
try {
const userDetails = await Userservice.getOneUser(article.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
return true;
} catch (error) {
throw error;
}
})
);
const popularArticles = allArticles.slice(0);
popularArticles.sort((a, b) => b.views - a.views);
const mostPopular = popularArticles.slice(0, 9);
Expand Down Expand Up @@ -160,16 +162,50 @@ class Articles {
const findArticles = await db.findAll({
where: { authorId: req.auth.id }
});

if (!findArticles) {
return res.status(200).send({
message: 'no articles'
});
}
if (findArticles) {
return res.status(200).send({
data: findArticles
});
}
const FoundArticles = _.map(
findArticles,
_.partialRight(_.pick, [
'authorId',
'slug',
'title',
'description',
'body',
'taglist',
'favorited',
'favoritedcount',
'flagged',
'images',
'views',
'createdAt'
])
);
await Promise.all(
FoundArticles.map(async (value) => {
try {
const userDetails = await Userservice.getOneUser(value.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(value.body);
const timeAgo = moment(value.createdAt).fromNow();
value.readtime = readTime;
value.username = username;
value.userImage = image;
value.timeCreated = timeAgo;
return true;
} catch (error) {
throw error;
}
})
);

return res.status(200).send({
data: FoundArticles
});
}

/**
Expand Down
10 changes: 10 additions & 0 deletions test/test.articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ describe('Articles', () => {
done();
});
});
it('should return resource not found message if endpoint does not exist', (done) => {
chai
.request(server)
.get('/api/v1/articles/user/articl')
.end((error, res) => {
expect(res.status).to.be.equal(404);
expect(res.body).to.have.deep.property('error', 'Resource not found');
done();
});
});
});

0 comments on commit eb39c71

Please sign in to comment.