Skip to content

Commit

Permalink
chore(getSingleArticle): add additional property
Browse files Browse the repository at this point in the history
- Add additional property that holds true or false if user has bookmarked the article or not

[Finishes #165597004]
  • Loading branch information
danprocoder committed Apr 25, 2019
1 parent b3c01f4 commit fc5ffd8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
24 changes: 18 additions & 6 deletions server/controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import articleNotificationMarkup from '../utils/markups/articleNotificationMarku

const {
Article,
Bookmark,
Tag,
User,
Rating,
Expand Down Expand Up @@ -404,15 +405,26 @@ class ArticleController {
}

const clap = await this.getClap(id, article.id);
let isBookmarked = false;

if (req.user.id && req.user.id !== req.article.userId) {
await History.create({
userId: req.user.id,
articleId: req.article.id,
readingTime: readTime.text.split(' read')[0]
// If user is logged in
if (req.user.id) {
// Check if article is bookmarked
const bookmarkRecord = await Bookmark.findOne({
where: { userId: req.user.id, articleId: article.id }
});
isBookmarked = bookmarkRecord != null;

// If user is not the author of the article
if (req.user.id !== req.article.userId) {
await History.create({
userId: req.user.id,
articleId: req.article.id,
readingTime: readTime.text.split(' read')[0]
});
}
}
response(res).success({ article: singleArticle[0], clap });
response(res).success({ article: singleArticle[0], clap, isBookmarked });
} catch (error) {
return response(res).serverError({ errors: { server: error } });
}
Expand Down
1 change: 1 addition & 0 deletions server/test/articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ describe('GET single article /api/articles/:slug', () => {
const { title } = res.body.article;
expect(res.status).to.be.equal(200);
expect(title).to.be.equal('This is an article');
expect(typeof res.body.isBookmarked).to.be.equal('boolean');
done(err);
});
});
Expand Down

0 comments on commit fc5ffd8

Please sign in to comment.