Skip to content

Commit

Permalink
Merge pull request #58 from andela/develop
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
9jaswag committed Apr 24, 2019
2 parents 4fa93e5 + c147d49 commit 6a3df87
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 46 deletions.
55 changes: 55 additions & 0 deletions controllers/follow.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { User } from '../models';

/**
* Controller for follow and unfollow
Expand Down Expand Up @@ -70,4 +71,58 @@ export default class FollowController {
message: 'User unfollowed successfully.'
});
}

/**
* @description Get user's followers and followings stats
* @param {object} req http request object
* @param {object} res http response object
* @returns {object} response
*/
static async getUserFollowers(req, res) {
const { id } = req.user;
const attributes = {
attributes: ['id', 'email', 'username', 'name', 'image', 'bio']
};
try {
const user = await User.findOne({ where: { id } });
const userFollowers = await user.getFollowers(attributes);
return res.status(200).json({
success: true,
message: "Successfully gotten user's followers",
followers: userFollowers,
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}

/**
* @description Get user's followings stats
* @param {object} req http request object
* @param {object} res http response object
* @returns {object} response
*/
static async getUserFollowings(req, res) {
const { id } = req.user;
const attributes = {
attributes: ['id', 'email', 'username', 'name', 'image', 'bio']
};
try {
const user = await User.findOne({ where: { id } });
const userFollowings = await user.getFollowing(attributes);
return res.status(200).json({
success: true,
message: "Successfully gotten user's followings",
followings: userFollowings
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}
}
24 changes: 23 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import jwt from 'jsonwebtoken';
import shortId from 'shortid';
import sendMail from '../helpers/emails';
import getName from '../helpers/user';
import { User } from '../models';
import { User, Article } from '../models';

dotenv.config();

Expand Down Expand Up @@ -262,4 +262,26 @@ export default class UserController {
});
}
}

/**
* @description Get user's article count
* @param {object} req http request object
* @param {object} res http response object
* @returns {object} response
*/
static async getUserArticlesCount(req, res) {
const { id: userId } = req.user;
try {
const { count } = await Article.findAndCountAll({ where: { userId } });
return res.status(200).json({
success: true,
articleCount: count
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}
}
2 changes: 1 addition & 1 deletion models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = (sequelize, DataTypes) => {
image: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'https://img.icons8.com/ios-glyphs/30/000000/user-male.png'
defaultValue: 'https://img.icons8.com/ios-glyphs/30/000000/user.png'
},
interests: {
type: DataTypes.ARRAY(DataTypes.STRING),
Expand Down
57 changes: 18 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"eslint-plugin-import": "^2.16.0",
"faker": "^4.1.0",
"husky": "^1.3.1",
"mocha": "^6.1.3",
"mocha": "^6.1.4",
"mochawesome": "^3.1.1",
"nock": "^10.0.6",
"nyc": "^13.3.0",
Expand Down
31 changes: 28 additions & 3 deletions routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ import {
validateImages,
validateInterest
} from '../middleware/validation';
import FollowController from '../controllers/follow';
import followController from '../controllers/follow';
import followVerification from '../middleware/follow';
import CommentController from '../controllers/comment';

const {
followUser, unfollowUser, getUserFollowers, getUserFollowings
} = followController;

const {
createArticle,
updateArticle,
Expand All @@ -48,6 +52,7 @@ const {
getAllArticles,
getArticlesByHighestField
} = ArticleController;

const { viewProfile, editProfile, updateProfileImage } = ProfileController;

const apiRoutes = express.Router();
Expand Down Expand Up @@ -240,14 +245,14 @@ apiRoutes.post(
'/follow/:id',
Auth.verifyUser,
followVerification,
FollowController.followUser
followUser
);

apiRoutes.post(
'/unfollow/:id',
Auth.verifyUser,
followVerification,
FollowController.unfollowUser
unfollowUser
);
apiRoutes.route('/articles/:slug/comments')
.post(
Expand Down Expand Up @@ -295,4 +300,24 @@ apiRoutes.route('/comments/:id/like')
CommentController.likeComment
);

apiRoutes.route('/user/followers')
.get(
Auth.verifyUser,
isUserVerified,
getUserFollowers
);

apiRoutes.route('/user/followings')
.get(
Auth.verifyUser,
isUserVerified,
getUserFollowings
);

apiRoutes.route('/user/articlescount')
.get(
Auth.verifyUser,
UserController.getUserArticlesCount
);

export default apiRoutes;
Loading

0 comments on commit 6a3df87

Please sign in to comment.