Making Friendships

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 387:- Making Friendships

In a social network application, making friends between users is a fundamental feature. In this feature, users can request to become friends with each other, and if the other user accepts, the two users become friends. Once two users are friends, they can see each other's posts and interact with each other in other ways on the platform.

To implement this feature, we need to define a schema for friendships, create routes and actions for sending friend requests, accepting friend requests, and displaying a user's friends.

Here are the steps to implement making friendships:

  1. Define a schema for friendships:

    phpCopy code

    const friendshipSchema = new mongoose.Schema(  {    user: {      type: mongoose.Schema.Types.ObjectId,      ref: "User",    },    friend: {      type: mongoose.Schema.Types.ObjectId,      ref: "User",    },    status: {      type: String,      enum: ["pending", "accepted", "rejected"],      default: "pending",    },  },  { timestamps: true } );

    This schema defines a friendship as having a user and friend field, both of which are references to the User model. The status field keeps track of the current status of the friendship request, which can be "pending", "accepted", or "rejected".

  2. Create routes and actions for sending friend requests:

    vbnetCopy code

    // Route for sending a friend request app.post("/friends/:id/request", async (req, res) => {  try {    const user = req.user; // Current user sending the request    const friend = await User.findById(req.params.id); // User receiving the request    if (!friend) {      throw new Error("User not found");    }    const friendship = await Friendship.findOne({      user: user._id,      friend: friend._id,    });    if (friendship && friendship.status !== "rejected") {      throw new Error("Friend request already sent");    }    const newFriendship = new Friendship({      user: user._id,      friend: friend._id,      status: "pending",    });    await newFriendship.save();    res.send({ message: "Friend request sent" });  } catch (err) {    res.status(400).send({ error: err.message });  } });

    This route allows a user to send a friend request to another user. The req.params.id is the id of the user who will receive the request. We first check if the user exists and then look for any existing friendship between the two users. If a friendship already exists and is not rejected, we throw an error. Otherwise, we create a new friendship with a "pending" status.

  3. Create routes and actions for accepting friend requests:

    phpCopy code

    // Route for accepting a friend request app.post("/friends/:id/accept", async (req, res) => {  try {    const user = req.user; // Current user accepting the request    const friendship = await Friendship.findOneAndUpdate(      {        user: req.params.id,        friend: user._id,        status: "pending",      },      { status: "accepted" },      { new: true }    );    if (!friendship) {      throw new Error("Friend request not found");    }    res.send({ message: "Friend request accepted" });  } catch (err) {    res.status(400).send({ error: err.message });  } });

    This route allows a user to accept a friend request from another user. The req.params.id is the id of the user who sent the request. We look for a friendship

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?