Skip to content

Commit

Permalink
Merge d776621 into 343d9d6
Browse files Browse the repository at this point in the history
  • Loading branch information
kleva-j committed Apr 23, 2019
2 parents 343d9d6 + d776621 commit b2574c4
Show file tree
Hide file tree
Showing 8 changed files with 678 additions and 772 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 requiredOutput = {
attributes: ['id', 'email', 'username', 'name', 'image', 'bio']
};
try {
const user = await User.findOne({ where: { id } });
const userFollowers = await user.getFollowers(requiredOutput);
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 requiredOutput = {
attributes: ['id', 'email', 'username', 'name', 'image', 'bio']
};
try {
const user = await User.findOne({ where: { id } });
const userFollowings = await user.getFollowing(requiredOutput);
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';

const { HOST_URL_FRONTEND } = process.env;

Expand Down Expand Up @@ -260,4 +260,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,
numberOfArticlesCreated: 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'
}
};

Expand Down
Loading

0 comments on commit b2574c4

Please sign in to comment.