Skip to content

Commit

Permalink
follow unfollow users
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavishya2107 committed Feb 8, 2020
1 parent c061aa7 commit 98bbd45
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 52 deletions.
6 changes: 6 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var userSchema = new Schema({
favorited: {
type: [Schema.Types.ObjectId],
ref: "Article"
},
followers: {
type: [String]
},
following: {
type: [String]
}
}, { timestamps: true })

Expand Down
91 changes: 39 additions & 52 deletions routes/api/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var loggedUser = auth.verifyToken;

router.use(loggedUser);

//only for loggedIn users
//view user profile
router.get('/:username', (req, res) => {
let username = req.params.username
User.findOne({ username }, "-password")
Expand All @@ -16,61 +18,46 @@ router.get('/:username', (req, res) => {
})
})

router.post('/:username/follow', (req, res) => {
let username = req.params.username

if (req.user.username === username) return res.status(400).json({ error: "you cannot follow yourself" })

var user = User.findOneAndUpdate({ username })

//follow user
router.post('/:username/follow', async (req, res) => {
username = req.params.username
loginUser = req.user.UserId
console.log(req.user)
try {
var user = await User.findOne({ username })
console.log(user)
if (!user.following.includes(loginUser)) {
var follow = await User.findOneAndUpdate({ username }, { $push: { following: req.user.UserId } })
await User.findByIdAndUpdate(req.user.UserId, { $push: { followers: follow.id } })
res.json({ success: true, msg: "successfully followed" })
} else {
res.json({ msg: `you have already followed the ${username}` })
}
} catch (error) {
res.json({ msg: "Issue in following" })
}
})


// router.post("/user/:user_id/follow-user", passport.authenticate("jwt", { session:false}), (req,res) => {

// // check if the requested user and :user_id is same if same then

// if (req.user.id === req.params.user_id) {
// return res.status(400).json({ alreadyfollow : "You cannot follow yourself"})
// }

// User.findById(req.params.user_id)
// .then(user => {

// // check if the requested user is already in follower list of other user then

// if(user.followers.filter(follower =>
// follower.user.toString() === req.user.id ).length > 0){
// return res.status(400).json({ alreadyfollow : "You already followed the user"})
// }

// user.followers.unshift({user:req.user.id});
// user.save()
// User.findOne({ email: req.user.email })
// .then(user => {
// user.following.unshift({user:req.params.user_id});
// user.save().then(user => res.json(user))
// })
// .catch(err => res.status(404).json({alradyfollow:"you already followed the user"}))
// })
// })



// router.get("/:username", (req, res) => {
// let username = req.params.username;
// User.findOne({ username })
// .populate({
// path: "article favorited followers following",
// populate: {
// path: "author"
// }
// })
// .exec((err, profile) => {
// if (err) res.status(422).json({ err });
// res.status(200).json({ success: true, profile });
// });
// });
//unfollow user
router.delete('/:username/follow', async (req, res) => {
username = req.params.username
loginUser = req.user.UserId
console.log(req.user)
try {
var user = await User.findOne({ username })
console.log(user)
if (user.following.includes(loginUser)) {
var follow = await User.findOneAndUpdate({ username }, { $pull: { following: req.user.UserId } })
await User.findByIdAndUpdate(req.user.UserId, { $pull: { followers: follow.id } })
res.json({ success: true, msg: `successfully unfollowed ${username}` })
} else {
res.json({ msg: `you have already unfollowed the ${username}` })
}
} catch (error) {
res.json({ msg: "Issue in following" })
}
})


module.exports = router;

0 comments on commit 98bbd45

Please sign in to comment.