1
\$\begingroup\$

Right now my route is doing the following:

  1. Searching for the post by id from the request parameters

  2. Check if the user already likes the post

  3. If the user likes the post, then increase likeCount and push user_id to the likes array of the current post and vice versa (dec, pull)

  4. Adding post id to myLikes array of user and vice versa

Any thoughts / suggestions on this code would be appreciated.

router.post('/like/:id', (req, res) => {
    User.find({
            'posts._id': req.params.id
        },
        'posts.$',
        (err, doc) => {
            if (err) console.log(err)
            const alreadyLike = doc[0].posts[0].likes.some(like => like._id == req.user.id);

            if (alreadyLike) {
                User.update({
                        'posts._id': req.params.id
                    }, 
                    {
                        $inc: { 'posts.$.likeCount' : -1},
                        $pull: { 'posts.$.likes': { _id: req.user.id }}
                    },
                    err => {
                        if (err) console.log(err)
                        res.send('dislike');
                });

                User.findByIdAndUpdate(
                    req.user._id,
                    {
                        $pull: {
                            myLikes: {
                                _id: req.params.id
                            }
                        }
                    },
                    (err, user) => {
                        if (err) console.log(err);
                });
            } else {
                User.update({
                        'posts._id': req.params.id
                    },
                    {
                        $inc: { 'posts.$.likeCount' : 1 },
                        $push: {'posts.$.likes': { _id: req.user.id }}
                    },
                    err => {
                        if (err) console.log(err)
                        Legend.find({
                                'posts._id': req.params.id
                            },
                            (err, user) => {
                                io.to(connectedUsers[user[0]._id]).emit('likePost', req.user.username);
                                res.send('like');
                            });
                    });

                    User.findByIdAndUpdate(
                        req.user._id,
                        {
                            $push: {
                                myLikes:{
                                    $each: [{
                                        _id: req.params.id
                                    }],
                                    $position: 0
                                }
                            }
                        },
                        (err, user) => {
                            if (err) console.log(err);
                    });
            }
        });
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.