Skip to content

Commit

Permalink
Merge pull request #54 from andela/bg-article-reaction-count-165410830
Browse files Browse the repository at this point in the history
#165410830 Return reaction count along with an article
  • Loading branch information
9jaswag committed Apr 23, 2019
2 parents 343d9d6 + 067b929 commit 6260b45
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 5 deletions.
30 changes: 26 additions & 4 deletions controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,36 @@ export default class ArticleController {
}
]
});

const likes = await article.getArticleReactions({ where: { likes: true } });
const dislikes = await article.getArticleReactions({ where: { likes: false } });

if (req.user) {
const { id } = req.user;
const viewer = await User.findOne({ where: { id } });
await viewer.addView(article);

let userReaction;

const getUserReaction = await article.getArticleReactions({ where: { userId: id } });
if (getUserReaction[0]) {
userReaction = (getUserReaction[0].likes) ? 'like' : 'dislike';
} else userReaction = null;

return res.status(200).json({
success: true,
article: article.dataValues,
likeCount: likes.length,
dislikeCount: dislikes.length,
userReaction
});
}

return res.status(200).json({
success: true,
article: article.dataValues
article: article.dataValues,
likeCount: likes.length,
dislikeCount: dislikes.length,
});
} catch (error) {
return res.status(404).json({
Expand Down Expand Up @@ -471,7 +493,7 @@ export default class ArticleController {
static getQuery(type, amount) {
const orders = {
ratings:
`
`
SELECT "Articles".*, ROUND(AVG("Ratings".rating), 1) AS avg_rating
FROM "Ratings"
JOIN "Articles" ON "Ratings"."articleId" = "Articles".id
Expand All @@ -480,14 +502,14 @@ export default class ArticleController {
LIMIT ${Number(amount) || 5}
`,
latest:
`
`
SELECT *
FROM "Articles"
ORDER BY "Articles"."createdAt" DESC
LIMIT ${Number(amount) || 5}
`,
comments:
`
`
SELECT "Articles".*, COUNT("Comments".comment) AS comment_count
FROM "Comments"
JOIN "Articles" ON "Comments"."articleSlug" = "Articles".slug
Expand Down
2 changes: 2 additions & 0 deletions models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module.exports = (sequelize, DataTypes) => {
});
Article.hasMany(models.Reaction, {
foreignKey: 'articleSlug',
as: 'articleReactions',
sourceKey: 'slug'
});
Article.hasMany(models.Comment, {
foreignKey: 'articleSlug',
Expand Down
102 changes: 101 additions & 1 deletion test/article.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,17 @@ describe('/GET articles', () => {
chai
.request(app)
.get(`/api/v1/articles/${articleSlug}`)
.set('authorization', token)
.end((err, res) => {
const { status, body: { success, article } } = res;
const {
status, body: {
success,
article,
likeCount,
dislikeCount,
userReaction
}
} = res;
expect(status).to.be.equal(200);
expect(success).to.be.equal(true);
expect(article).to.be.an('object');
Expand All @@ -358,6 +367,36 @@ describe('/GET articles', () => {
expect(article).to.haveOwnProperty('body');
expect(article).to.haveOwnProperty('description');
expect(article).to.haveOwnProperty('author');
expect(likeCount).to.be.equal(0);
expect(dislikeCount).to.be.equal(0);
expect(userReaction).to.be.equal(null);
done(err);
});
});
it('should return the article if user is not logged in', (done) => {
chai
.request(app)
.get(`/api/v1/articles/${articleSlug}`)
.end((err, res) => {
const {
status, body: {
success,
article,
likeCount,
dislikeCount,
}
} = res;
expect(status).to.be.equal(200);
expect(success).to.be.equal(true);
expect(article).to.be.an('object');
expect(article).to.haveOwnProperty('id');
expect(article).to.haveOwnProperty('slug');
expect(article).to.haveOwnProperty('title');
expect(article).to.haveOwnProperty('body');
expect(article).to.haveOwnProperty('description');
expect(article).to.haveOwnProperty('author');
expect(likeCount).to.be.equal(0);
expect(dislikeCount).to.be.equal(0);
done(err);
});
});
Expand Down Expand Up @@ -537,6 +576,21 @@ describe('/POST articles like', () => {
});
});

it('should create another article', (done) => {
chai
.request(app)
.post('/api/v1/articles')
.set('authorization', userToken)
.send(article2)
.end((err, res) => {
articleSlug3 = res.body.article.slug;
expect(res).to.have.status(201);
expect(res.body).to.have.property('success').equal(true);
expect(res.body).to.have.property('message').equal('New article created successfully');
done(err);
});
});

it('should return an error if the article is not found', (done) => {
chai
.request(app)
Expand Down Expand Up @@ -564,6 +618,20 @@ describe('/POST articles like', () => {
});
});

it('should create an article like reaction', (done) => {
chai
.request(app)
.post(`/api/v1/like_article/${articleSlug3}`)
.set('authorization', userToken2)
.end((err, res) => {
reaction = res.body.reaction;
expect(res).to.have.status(201);
expect(res.body).to.have.property('success').equal(true);
expect(res.body).to.have.property('message').equal('You have liked this article');
done(err);
});
});

it('should unlike an article reaction if the article has been liked', (done) => {
chai
.request(app)
Expand All @@ -578,6 +646,38 @@ describe('/POST articles like', () => {
});
});

describe('Get a liked article by its slug', () => {
it('should return the article', (done) => {
chai
.request(app)
.get(`/api/v1/articles/${articleSlug3}`)
.set('authorization', userToken2)
.end((err, res) => {
const {
status, body: {
success,
article,
likeCount,
dislikeCount,
userReaction
}
} = res;
expect(status).to.be.equal(200);
expect(success).to.be.equal(true);
expect(article).to.be.an('object');
expect(article).to.haveOwnProperty('id');
expect(article).to.haveOwnProperty('slug');
expect(article).to.haveOwnProperty('title');
expect(article).to.haveOwnProperty('body');
expect(article).to.haveOwnProperty('description');
expect(article).to.haveOwnProperty('author');
expect(likeCount).to.be.equal(1);
expect(dislikeCount).to.be.equal(0);
expect(userReaction).to.be.equal('like');
done(err);
});
});
});

describe('/POST articles dislike', () => {
it('should create an article dislike reaction', (done) => {
Expand Down

0 comments on commit 6260b45

Please sign in to comment.