Skip to content

Commit

Permalink
Commit message
Browse files Browse the repository at this point in the history
ch(refactoring): implement code refactoring

- refactoring code

[( #]
  • Loading branch information
Michael Nzube authored and Patrick Ngabonziza committed Aug 1, 2019
1 parent c17f4b9 commit 5c5b70b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
24 changes: 24 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "2"
exclude_patterns:
- "node_modules"
checks:
argument-count:
enabled: false
complex-logic:
enabled: false
file-lines:
enabled: false
method-complexity:
enabled: false
method-count:
enabled: false
method-lines:
enabled: false
nested-control-flow:
enabled: false
return-statements:
enabled: false
similar-code:
enabled: false
identical-code:
enabled: false
1 change: 0 additions & 1 deletion src/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class articleContoller {
await ReadingStatsHelper.updateStatistic(req.params.id);
if (highlight) {
await updateHightlights(req.params.id);
// return res.status(200).json({ article, highlight });
}
return res.status(200).json({ status: 200,
article: { slug: article.slug,
Expand Down
32 changes: 7 additions & 25 deletions src/controllers/followers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import models from '../models';
import { follow, getUser, checkUser, unfollowUser } from './helpers/followersHelper';
import { follow, getUser, checkUser, unfollowUser, getFollowers, getFollowing } from './helpers/followersHelper';

const { User, Follow } = models;
/**
* @description CRUD for followers
* @author: Patrick Ngabonziza
Expand All @@ -23,11 +21,7 @@ export default class FollowerController {
if (followedUser) return res.status(409).json({ status: 409, message: `Already following ${findUser.username}` });
await follow(findUser.id, req.user.id);
return res.status(201).json({ status: 201,
message: `Following ${findUser.username}`,
username: findUser.username,
bio: findUser.bio,
image: findUser.image,
following: findUser.following });
message: `Following ${findUser.username}` });
} catch (error) {
return res.status(500).json({ message: 'server error' });
}
Expand All @@ -39,19 +33,15 @@ export default class FollowerController {
* @returns {Object} Unfollow user
*/
static async unfollowUser(req, res) {
const { username } = req.params;
const userId = req.user.id;
try {
const { username } = req.params;
const userId = req.user.id;
const findUser = await getUser(username);
if (!findUser) return res.status(404).json({ status: 404, message: 'Username not found' });
const followedUser = await checkUser(findUser.id);
return followedUser ? await unfollowUser(userId, findUser.id)
&& res.status(200).json({ status: 200,
message: `Unfollowed ${findUser.username}`,
username: findUser.username,
bio: findUser.bio,
image: findUser.image,
following: findUser.following })
message: `Unfollowed ${findUser.username}` })
: res.status(404).json({ status: 404, message: { follow: `Not following "${findUser.username}"` } });
} catch (error) {
return res.status(500).json({ status: 500, message: 'Server error' });
Expand All @@ -65,11 +55,7 @@ export default class FollowerController {
*/
static async followers(req, res) {
try {
const followers = await Follow.findAll({ where: { followed: req.user.id },
include: [{ model: User,
as: 'follower',
attributes: ['id', 'username', 'email',
'image', 'bio', 'following'] }] });
const followers = await getFollowers(req.user.id);
return followers.length
? res.status(200).json({ status: 200,
message: 'Followers',
Expand All @@ -87,11 +73,7 @@ export default class FollowerController {
*/
static async following(req, res) {
try {
const following = await Follow.findAll({ where: { userId: req.user.id },
include: [{ model: User,
as: 'followedUser',
attributes: ['id', 'username', 'email',
'image', 'bio', 'following'] }] });
const following = await getFollowing(req.user.id);
return following.length
? res.status(200).json({ status: 200,
message: 'Following',
Expand Down
21 changes: 20 additions & 1 deletion src/controllers/helpers/followersHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,23 @@ const checkUser = followed => Follow.findOne({ where: { followed } });

const unfollowUser = (userId, followed) => Follow.destroy({ where: { userId, followed } });

export { follow, getUser, checkUser, unfollowUser };
const getFollowers = (userId) => {
const followers = Follow.findAll({ where: { followed: userId },
include: [{ model: User,
as: 'follower',
attributes: ['id', 'username', 'email',
'image', 'bio', 'following'] }] });

return followers;
};

const getFollowing = (userId) => {
const following = Follow.findAll({ where: { userId },
include: [{ model: User,
as: 'followedUser',
attributes: ['id', 'username', 'email',
'image', 'bio', 'following'] }] });

return following;
};
export { follow, getUser, checkUser, unfollowUser, getFollowers, getFollowing };

0 comments on commit 5c5b70b

Please sign in to comment.