If you have any query feel free to chat us!
Happy Coding! Happy Learning!
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:
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".
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.
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
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.