Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/controllers/User/Block/blockUser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

/**
* @module Block User Controller
*
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
*
* @description
* This controller will allow the user to block a specific user, if all parameters are correct.
*
*
* @todo
* Nothing for now.
*/
Expand Down Expand Up @@ -40,11 +40,14 @@ module.exports.blockUser = async (req, res) => {
if (!user) {
return res.status(404).send({ msg: 'Current account not found.' })
}
if (user.hasBlocked(userToBlock)) {

const alreadyBlocked = await user.hasBlockedUser(userToBlock);

if (alreadyBlocked) {
return res.status(400).send({ msg: 'You have already blocked this person.' });
}

user.addBlocked(userToBlock);
user.addBlockedUser(userToBlock);

return res.status(200).send({ msg: 'User blocked.' });

Expand Down
9 changes: 5 additions & 4 deletions src/controllers/User/Block/listBlocked.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ module.exports.listBlocked = async (req, res) => {

try {

const user = await User.findOne({
const userBlockedList = await User.findOne({
where: {
id: req.user.id,
},
})
include: 'BlockedUser',
});

if (!user) {
return res.status(404).send({ msg: 'User not found.' });
}

const blockedUsers = await user.getBlocked();
return res.status(200).json(blockedUsers);

return res.status(200).json(userBlockedList.BlockedUser);

} catch (err) {
console.log(err.message);
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/User/Block/queryBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.queryBlock = async (req, res) => {
const [blockedUser, user] = await Promise.all([
User.findOne({
where: {
id: req.params.blockId,
id: req.params.blockedId,
},
})
,
Expand All @@ -41,7 +41,9 @@ module.exports.queryBlock = async (req, res) => {
return res.status(404).send({ msg: 'Current account not found. Try loggin in.' })
}

if (!user.hasBlocked(blockedUser)) {
const alreadyBlocked = await user.hasBlockedUser(blockedUser);

if (!alreadyBlocked) {
return res.status(401).send({ msg: "You have not blocked this person." });
}

Expand Down
6 changes: 4 additions & 2 deletions src/controllers/User/Block/unblockUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ module.exports.unblockUser = async (req, res) => {
return res.status(404).send({ msg: 'Current account not found.' });
}

if (!user.hasBlocked(blockedUser)) {
const alreadyBlocked = await user.hasBlockedUser(blockedUser);

if (!alreadyBlocked) {
return res.status(401).send({ msg: "You have not blocked this person." });
}

user.removeBlocked(blockedUser);
user.removeBlockedUser(blockedUser);

return res.status(200).send({ msg: 'User unblocked.' });

Expand Down
5 changes: 4 additions & 1 deletion src/controllers/User/Followers/followUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ module.exports.followUser = async (req, res) => {
return res.status(404).send({ msg: 'Current account not found.' });
}

if (userToFollow.hasFollower(user)) {

const alreadyFollower = await userToFollow.hasFollower(user);

if (alreadyFollower) {
return res.status(401).send({ msg: "You are already following this person." });
}

Expand Down
10 changes: 5 additions & 5 deletions src/controllers/User/Followers/listFollowers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
*/

module.exports.listFollowers = async (req, res) => {

if (!req.params.id) {
return res.status(400).send({ msg: "Not All Parameters Provided." });
}

try {

const user = await User.findOne({
const userFollowersList = await User.findOne({
where: {
id: req.params.id,
},
include: 'Follower',
});

if (!user) {
if (!userFollowersList) {
return res.status(404).send({ msg: "User not found." });
}

const followers = await user.getFollowers();
return res.status(200).json(followers);
return res.status(200).json(userFollowersList.Follower);

} catch (err) {
console.log(err.message);
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/User/Followers/queryFollower.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ module.exports.queryFollower = async (req, res) => {

])



if (!follower) {
return res.status(404).send({ msg: 'Follower user not found.' });
}
Expand All @@ -47,8 +45,10 @@ module.exports.queryFollower = async (req, res) => {
return res.status(404).send({ msg: 'Followee user not found.' });
}

if (!followee.hasFollower(follower)) {
return res.status(401).send({
const alreadyFollower = await followee.hasFollower(follower);

if (!alreadyFollower) {
return res.status(400).send({
msg: `User with id=${req.params.id} is not followed by user with id=${req.params.followerId}`
});
}
Expand Down
9 changes: 3 additions & 6 deletions src/controllers/User/Followers/unfollowUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ module.exports.unfollowUser = async (req, res) => {
return res.status(400).send({ msg: "Not All Parameters Provided." });
}


try {

const [followee, user] = await Promise.all([

User.findOne({
Expand All @@ -47,13 +45,12 @@ module.exports.unfollowUser = async (req, res) => {
return res.status(404).send({ msg: 'Current user not found.' });
}

if (!followee.hasFollower(user)) {
const alreadyFollower = await followee.hasFollower(user);

if (!alreadyFollower) {
return res.status(401).send({ msg: 'Your are not following this person.' });
}




followee.removeFollower(user);

return res.status(200).send({ msg: 'Your are not longer following this person.' });
Expand Down
11 changes: 9 additions & 2 deletions src/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const { verifyEmail } = require('../middelwares/verifyEmail');
// User Endpoints
router.get('/', userControllers.listUsers);
router.get('/count', userControllers.countUsers);
router.get('/:id/', userControllers.queryUser);
router.get('/:id/status', userControllers.getStatus);


router.post('/signup', userControllers.signup);
router.post('/login', userControllers.login);
Expand All @@ -21,6 +20,14 @@ router.post('/delete', verifyLogin, userControllers.deleteAccount);
router.get('/blockedUsers', verifyLogin, userControllers.listBlocked);
router.get('/blockedUsers/:blockedId', verifyLogin, userControllers.queryBlock);


// User endpoints -
// Note: since id can be anything the get by id route sould be last to be invoked.

router.get('/:id/', userControllers.queryUser);
router.get('/:id/status', userControllers.getStatus);


router.post('/:toBlockId/block', verifyLogin, verifyEmail, userControllers.blockUser);
router.post('/:toUnblockId/unblock', verifyLogin, verifyEmail, userControllers.unblockUser);

Expand Down