Skip to content

Commit

Permalink
Merge branch 'develop' into feature/164798033/like-specific-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotimi Babalola committed Apr 10, 2019
2 parents 0802ee2 + b1f4abd commit 5f23f00
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 45 deletions.
32 changes: 16 additions & 16 deletions server/controllers/article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ const getOneArticle = async (req, res) => {
where: {
id: req.params.id,
},
include: [
{
model: User,
as: 'author',
attributes: [
'id',
'first_name',
'last_name',
'title',
'phone_number',
'email',
'bio',
],
},
],
});

if (!article) {
Expand All @@ -39,23 +54,8 @@ const getOneArticle = async (req, res) => {
});
}

const articleObj = {
id: article.id,
author: article.user_id,
title: article.title,
slug: article.slug,
abstract: article.abstract,
body: article.body,
category: article.category,
imageurl: article.image_url,
bookmarkcount: article.bookmark_count,
likescount: article.likes_count,
createdAt: article.createdAt,
updatedAt: article.updatedAt,
};

return res.status(200).json({
article: articleObj,
article,
});
} catch (error) {
return res.status(500).json({
Expand Down
1 change: 1 addition & 0 deletions server/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = (sequelize, DataTypes) => {
Article.associate = models =>
Article.belongsTo(models.User, {
foreignKey: 'user_id',
as: 'author',
});
return Article;
};
6 changes: 5 additions & 1 deletion server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = (sequelize, DataTypes) => {
});

User.associate = models => {
const { Follower } = models;
const { Follower, Article } = models;

User.hasMany(Follower, {
foreignKey: 'followee_id',
Expand All @@ -92,6 +92,10 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'follower_id',
as: 'follower',
});
User.hasMany(Article, {
foreignKey: 'user_id',
as: 'author',
});
};
return User;
};
30 changes: 2 additions & 28 deletions tests/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,9 @@ describe('ARTICLE', () => {
.set('Authorization', userBToken)
.end((err, res) => {
expect(res).to.have.status(200);
const {
id,
author,
title,
slug,
abstract,
body,
category,
imageurl,
bookmarkcount,
likescount,
createdAt,
updatedAt,
} = res.body.article;
const { id } = res.body.article;
expect(res.body.article).to.be.a('object');
expect(res.body.article).contains({
id,
author,
title,
slug,
abstract,
body,
category,
imageurl,
bookmarkcount,
likescount,
createdAt,
updatedAt,
});
expect(id).to.equal('7139d3af-b8b4-44f6-a49f-9305791700f4');
done();
});
});
Expand Down

0 comments on commit 5f23f00

Please sign in to comment.