This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
477 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const firstFollowMockData = { | ||
authorId: 1, | ||
followerId: 2 | ||
}; | ||
|
||
export const secondFollowMockData = { | ||
authorId: 1, | ||
followerId: 3 | ||
}; | ||
|
||
export const thirdFollowMockData = { | ||
authorId: 2, | ||
followerId: 90000 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import models from '../models'; | ||
import errorResponse from '../helpers/errorResponse'; | ||
import getFollowViews from '../helpers/getfollowViews'; | ||
|
||
const { User, Article, Follow } = models; | ||
|
||
/** | ||
* @class FollowController | ||
* @description Follow related Operations | ||
*/ | ||
class FollowController { | ||
/** | ||
* @description | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @returns {object} - return success repsonse | ||
*/ | ||
static followAuthor(req, res) { | ||
const { authorId } = req.params; | ||
const { id } = req.userData; | ||
const successResponse = (message, followStatus) => res.status(200).json({ | ||
status: 'success', | ||
message, | ||
followStatus | ||
}); | ||
User.findOne({ | ||
where: { id: authorId }, | ||
include: [{ | ||
model: Article, | ||
as: 'publishedArticles', | ||
}] | ||
}) | ||
.then((author) => { | ||
switch (true) { | ||
case (!author): { | ||
return errorResponse('', res, 'user not found', 404); | ||
} | ||
case (+author.id === +id): { | ||
return errorResponse('', res, 'you cant follow yourself', 403); | ||
} | ||
case (author.publishedArticles.length < 5): { | ||
return errorResponse('', res, 'you cant follow this user', 403); | ||
} | ||
case (author.publishedArticles.length >= 5): | ||
Follow.findOrCreate({ | ||
where: { authorId, followerId: id } | ||
}) | ||
.spread((followed, status) => { | ||
if (status) { | ||
return successResponse('you followed this author', true); | ||
} | ||
Follow.destroy({ | ||
where: { | ||
authorId, followerId: id | ||
} | ||
}) | ||
.then((unfollowed) => { | ||
if (unfollowed) { | ||
return successResponse( | ||
'you unfollowed this author', false | ||
); | ||
} | ||
}) | ||
.catch(err => errorResponse(err, res)); | ||
}) | ||
.catch(err => errorResponse(err, res)); | ||
break; | ||
default: | ||
} | ||
}) | ||
.catch(error => errorResponse(error, res)); | ||
} | ||
|
||
/** | ||
* @description | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
* @returns {object} - return success repsonse | ||
*/ | ||
static displayFollowView(req, res) { | ||
const { userId } = req.params; | ||
getFollowViews('authorId', userId, 'follower') | ||
.then((followerView) => { | ||
getFollowViews('followerId', userId, 'following') | ||
.then((followingView) => { | ||
const following = followingView.map(x => x.following); | ||
const followers = followerView.map(x => x.follower); | ||
return res.status(200).json({ | ||
status: 'success', | ||
following, | ||
followers, | ||
}); | ||
}) | ||
.catch(err => errorResponse(err, res)); | ||
}); | ||
} | ||
} | ||
|
||
export default FollowController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* This arrow function reformats errors. | ||
* @param {string} err - the error object | ||
* @param {string} res - the response object | ||
* @param {string} message - error message | ||
* @param {string} statusCode - the status code | ||
* @returns {object} return email if it exist or null if it doesn't. | ||
*/ | ||
const errorResponse = (err, res, message, statusCode = '') => { | ||
res.status(statusCode || 500).json({ | ||
status: 'failure', | ||
errors: { | ||
message: [message || err.message] | ||
} | ||
}); | ||
}; | ||
|
||
export default errorResponse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import models from '../models'; | ||
|
||
const { Follow, User } = models; | ||
|
||
const getFollowViews = (queryParam, userId, view) => Follow.findAll({ | ||
where: { | ||
[`${queryParam}`]: userId | ||
}, | ||
attributes: [], | ||
include: [{ | ||
model: User, | ||
as: view, | ||
attributes: ['id', 'fullName', 'avatarUrl'] | ||
}] | ||
}); | ||
|
||
export default getFollowViews; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => { | ||
queryInterface.createTable('Follows', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
authorId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
} | ||
}, | ||
followerId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: queryInterface => queryInterface.dropTable('Follows') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Follow = sequelize.define('Follow', { | ||
authorId: { | ||
allowNull: false, | ||
type: DataTypes.INTEGER, | ||
}, | ||
followerId: { | ||
allowNull: false, | ||
type: DataTypes.INTEGER | ||
}, | ||
}, {}); | ||
Follow.associate = (models) => { | ||
const { User } = models; | ||
Follow.belongsTo(User, { | ||
as: 'following', | ||
foreignKey: 'authorId', | ||
onDelete: 'CASCADE' | ||
}); | ||
Follow.belongsTo(User, { | ||
as: 'follower', | ||
foreignKey: 'followerId', | ||
onDelete: 'CASCADE' | ||
}); | ||
}; | ||
return Follow; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.