Code Activity Solution - Making Likes Functional

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 386:- Code Activity Solution - Making Likes Functional

To enable liking posts in our app, we need to define the necessary actions and routes. Here's how we can do that:

  1. Create a new likesController.js file in the controllers directory.
  2. Define the following actions in the likesController.js file:
    • likePost: This action will be used to like a post. It will create a new like record in the database and associate it with the post and the user who liked it.
    • unlikePost: This action will be used to unlike a post. It will find the like record for the post and the user who liked it, and then delete it from the database.
  3. Define the following routes in the routes.js file:
    • POST /likes/:postId/like: This route will call the likePost action when a user likes a post.
    • DELETE /likes/:postId/unlike: This route will call the unlikePost action when a user unlikes a post.

Here's an example implementation of the likesController.js file:

javascriptCopy code

const Post = require('../models/post'); const Like = require('../models/like'); module.exports.likePost = async function (req, res) {  try {    let post = await Post.findById(req.params.postId);    // Check if the post already has a like from the current user    let existingLike = await Like.findOne({      user: req.user._id,      post: req.params.postId,    });    if (!existingLike) {      // Create a new like record      let newLike = await Like.create({        user: req.user._id,        post: req.params.postId,      });      post.likes.push(newLike);      post.save();    }    return res.json(200, {      message: 'Post liked',    });  } catch (err) {    console.log('Error in liking post', err);    return res.json(500, {      message: 'Internal Server Error',    });  } }; module.exports.unlikePost = async function (req, res) {  try {    let post = await Post.findById(req.params.postId);    // Find the like record for the post and the current user    let existingLike = await Like.findOne({      user: req.user._id,      post: req.params.postId,    });    if (existingLike) {      // Remove the like record      existingLike.remove();      // Remove the like from the post's likes array      post.likes.pull(existingLike._id);      post.save();    }    return res.json(200, {      message: 'Post unliked',    });  } catch (err) {    console.log('Error in unliking post', err);    return res.json(500, {      message: 'Internal Server Error',    });  } };

And here's an example implementation of the routes.js file:

javascriptCopy code

const express = require('express'); const router = express.Router(); const passport = require('passport'); const likesController = require('../controllers/likesController'); router.post(  '/likes/:postId/like',  passport.authenticate('jwt', { session: false }),  likesController.likePost ); router.delete(  '/likes/:postId/unlike',  passport.authenticate('jwt', { session: false }),  likesController.unlikePost ); module.exports = router;

45. Friends + Likes

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?