-
Notifications
You must be signed in to change notification settings - Fork 6
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
9 changed files
with
384 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,103 @@ | ||
import models from '../models'; | ||
|
||
const { users, Follow } = models; | ||
/** | ||
* @param {class} --Followers controller | ||
*/ | ||
class follower { | ||
/** | ||
* Users can follow each other | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} res message | ||
*/ | ||
|
||
static async follow(req, res) { | ||
try { | ||
const { username } = req.params; | ||
const checkUser = await users.findOne({ where: { username } }); | ||
const loggedinUser = await users.findOne({ where: { username: req.decoded.username } }); | ||
|
||
if (!checkUser) { | ||
return res.status(404).json({ error: 'User does not exists!' }); | ||
} | ||
if (checkUser.id === loggedinUser.id) { | ||
return res.status(400).json({ error: 'You can not follow yourself' }); | ||
} | ||
|
||
const following = await Follow.findOne({ | ||
where: { userId: loggedinUser.id, followed: checkUser.id } | ||
}); | ||
|
||
if (!following) { | ||
await Follow.create({ | ||
followed: checkUser.id, | ||
userId: loggedinUser.id | ||
}); | ||
return res.status(201).json({ | ||
message: `you are following ${checkUser.username}`, | ||
}); | ||
} | ||
|
||
await Follow.destroy({ where: { userId: loggedinUser.id, followed: checkUser.id } }); | ||
return res.status(200).json({ | ||
message: `you unfollowed ${checkUser.username}`, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Something went wrong, please try again!' }); | ||
} | ||
} | ||
|
||
/** | ||
* Users can get his/her followers | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} res message | ||
*/ | ||
|
||
static async followers(req, res) { | ||
try { | ||
const loggedinUser = await users.findOne({ where: { username: req.decoded.username } }); | ||
const followers = await Follow.findAll({ | ||
attributes: [], | ||
where: { followed: loggedinUser.id }, | ||
include: [{ model: users, as: 'User', attributes: ['username', 'image'] }] | ||
}); | ||
return followers.length | ||
? res.status(200).json({ | ||
message: 'Followers', | ||
followers | ||
}) | ||
: res.status(404).json({ error: "You don't have followers" }); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Failed to fetch followers, please try again!' }); | ||
} | ||
} | ||
|
||
/** | ||
* Users can get his followings | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} res message | ||
*/ | ||
|
||
static async following(req, res) { | ||
try { | ||
const loggedinUser = await users.findOne({ where: { username: req.decoded.username } }); | ||
const following = await Follow.findAll({ | ||
attributes: [], | ||
where: { userId: loggedinUser.id }, | ||
include: [{ model: users, as: 'User', attributes: ['username', 'image'] }] | ||
}); | ||
return following.length | ||
? res.status(200).json({ | ||
message: 'Following', | ||
following | ||
}) | ||
: res.status(404).json({ error: "You aren't following anyone" }); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Failed to fetch following, please try again!' }); | ||
} | ||
} | ||
} | ||
export default follower; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable( | ||
'Follow', { | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
followed: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
} | ||
), | ||
down: queryInterface => queryInterface.dropTable('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Follow = sequelize.define( | ||
'Follow', { | ||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
}, | ||
followed: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
} | ||
}, | ||
{ | ||
timestamps: true, | ||
tableName: 'Follow' | ||
} | ||
); | ||
Follow.associate = (models) => { | ||
Follow.belongsTo(models.users, { | ||
foreignKey: 'userId', | ||
as: 'follower', | ||
targetkey: 'id', | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE' | ||
}); | ||
Follow.belongsTo(models.users, { | ||
foreignKey: 'followed', | ||
as: 'User', | ||
targetkey: 'id', | ||
onUpdate: 'CASCADE', | ||
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
Oops, something went wrong.