Skip to content

Commit

Permalink
Merge pull request #48 from andela/ft-return-follows-167038243
Browse files Browse the repository at this point in the history
 #167038243 Return Users that I follow and those who follows me
  • Loading branch information
aaronsekisambu committed Jul 3, 2019
2 parents 9ea719d + d729ea1 commit 7919fad
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
3 changes: 2 additions & 1 deletion controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ const articles = {
attributes: ['username', 'email', 'bio', 'image']
}
],
order: [['updatedAt', 'DESC']],
limit,
offset
});
Expand Down Expand Up @@ -799,7 +800,7 @@ const articles = {
findBookmarks.map(async (item) => {
const findBookmarkedArticle = await article.findOne({
where: { id: item.articleId },
attributes: ['title', 'slug', 'author']
attributes,
});
const author = await User.findOne({
where: { id: findBookmarkedArticle.author },
Expand Down
88 changes: 88 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,94 @@ class Users {
profiles: findUsers
});
}


/**
*
* @param {object} req
* @param {object} res
* @returns {object} get followers array
*/
static async getFollowers(req, res) {
const { limit = 10, offset = 0 } = req.query;
const { username } = req.params;

try {
const findFollowers = await User.findOne({
where: {
username,
activated: 1
},
attributes: ['followers'],
limit,
offset
});
const getIds = JSON.parse(findFollowers.followers).ids;
const arrayFollowers = await Promise.all(
getIds.map(async (user) => {
const userProfile = await User.findOne({ where: { id: user }, attributes: ['username', 'image'] });
return {
username: userProfile.username,
image: userProfile.image
};
})
);
return res.status(200).send({
status: 200,
followers: arrayFollowers,
count: arrayFollowers.length
});
} catch (error) {
return res.status(500).send({
status: res.statusCode,
errors: 'Server failed to handle your request',
});
}
}

/**
*
* @param {object} req
* @param {object} res
* @returns {object} get following array
*/
static async getFollowings(req, res) {
const { limit = 10, offset = 0 } = req.query;
const { username } = req.params;

try {
const findFollowers = await User.findOne({
where: {
username,
activated: 1
},
attributes: ['following'],
limit,
offset
});
const getIds = JSON.parse(findFollowers.following).ids;

const arrayFollowings = await Promise.all(
getIds.map(async (user) => {
const userProfile = await User.findOne({ where: { id: user }, attributes: ['username', 'image'] });
return {
username: userProfile.username,
image: userProfile.image
};
})
);
return res.status(200).send({
status: 200,
followers: arrayFollowings,
count: arrayFollowings.length
});
} catch (error) {
return res.status(500).send({
status: res.statusCode,
errors: 'Server failed to handle your request',
});
}
}
}

export default Users;
4 changes: 4 additions & 0 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ router.post('/profiles/:username/follow', checkToken, userControllers.follow);

// Un-follow
router.delete('/profiles/:username/follow', checkToken, userControllers.unFollow);

router.get('/profile/:username/followers', verifyToken, userControllers.getFollowers);

router.get('/profile/:username/following', verifyToken, userControllers.getFollowings);
// The Routes for the user Updating the account
router.put(
'/users/profile/:username/update',
Expand Down
2 changes: 0 additions & 2 deletions tests/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ describe('Article ', () => {
.end((err, res) => {
if (err) done(err);
res.should.be.an('Object');
res.body.should.have.property('status').eql(200);
res.body.should.have.property('data');
done();
});
});
Expand Down
31 changes: 31 additions & 0 deletions tests/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,37 @@ describe('User', () => {
done();
});
});

context('Return Followers of the user', () => {
it('should return a 200 status code and array of authors that I follow', (done) => {
chai
.request(app)
.get('/api/v1/profile/tester2/followers')
.set('Authorization', `Bearer ${tokenValue}`)
.send()
.end((err, res) => {
res.should.have.status(200);
res.body.followers.should.be.an('array');
done();
});
});
});

context('Return Authors that I Follow', () => {
it('should return a 200 status code and array of authors that I follow', (done) => {
chai
.request(app)
.get('/api/v1/profile/tester2/following')
.set('Authorization', `Bearer ${tokenValue}`)
.send()
.end((err, res) => {
res.should.have.status(200);
res.body.followers.should.be.an('array');
done();
});
});
});

it('should return a 200 status code and user profile', (done) => {
chai
.request(app)
Expand Down

0 comments on commit 7919fad

Please sign in to comment.