User Routes
User.js controller
- In previous section we have created User.js Routes and now we will create User.js controller.
- we import
getUser,getUserFriends,addRemoveFriendfromuser.jscontroller so now we will create these functions.
server
├── controllers
│ ├── user.jsimport User from "../models/User.js";
/* READ */
export const getUser = async (req, res) => {
try {
const { id } = req.params;
const user = await User.findById(id);
res.status(200).json(user);
} catch (err) {
res.status(404).json({ message: err.message });
}
};
export const getUserFriends = async (req, res) => {
try {
const { id } = req.params;
const user = await User.findById(id);
const friends = await Promise.all(
user.friends.map((id) => User.findById(id))
);
const formattedFriends = friends.map(
({ _id, firstName, lastName, occupation, location, picturePath }) => {
return { _id, firstName, lastName, occupation, location, picturePath };
}
);
res.status(200).json(formattedFriends);
} catch (err) {
res.status(404).json({ message: err.message });
}
};
/* UPDATE */
export const addRemoveFriend = async (req, res) => {
try {
const { id, friendId } = req.params;
const user = await User.findById(id);
const friend = await User.findById(friendId);
if (user.friends.includes(friendId)) {
user.friends = user.friends.filter((id) => id !== friendId);
friend.friends = friend.friends.filter((id) => id !== id);
} else {
user.friends.push(friendId);
friend.friends.push(id);
}
await user.save();
await friend.save();
const friends = await Promise.all(
user.friends.map((id) => User.findById(id))
);
const formattedFriends = friends.map(
({ _id, firstName, lastName, occupation, location, picturePath }) => {
return { _id, firstName, lastName, occupation, location, picturePath };
}
);
res.status(200).json(formattedFriends);
} catch (err) {
res.status(404).json({ message: err.message });
}
};Let's break down each method and what it does:
Method 1: getUser
This method is used to retrieve a user's information by their id.
- It takes in the req and res objects as parameters.
export const getUser = async (req, res) => {- The id is extracted from the request's params object using destructuring.
const { id } = req.params;- The User model's findById method is used to find the user with the given id.
const user = await User.findById(id);- If a user is found, it is returned as a JSON object with a 200 status code.
res.status(200).json(user);- If no user is found, a 404 status code with an error message is returned.
res.status(404).json({ message: err.message });Method 2: getUserFriends
This method is used to retrieve a user's list of friends by their id.
- It takes in the req and res objects as parameters.
export const getUserFriends = async (req, res) => {- The id is extracted from the request's params object using destructuring.
const { id } = req.params;- The User model's findById method is used to find the user with the given id.
const user = await User.findById(id);- The Promise.all method is used to retrieve information for each friend by mapping through the user's friends array and using the findById method for each friend.
const friends = await Promise.all(
user.friends.map((id) => User.findById(id))
);- The information for each friend is then formatted as a JSON object with only specific properties (e.g. _id, firstName, lastName, occupation, location, picturePath).
const formattedFriends = friends.map(
({ _id, firstName, lastName, occupation, location, picturePath }) => {
return { _id, firstName, lastName, occupation, location, picturePath };
}
);- If a user is found and they have friends, the list of formatted friends is returned as a JSON object with a 200 status code.
res.status(200).json(formattedFriends);- If no user is found or the user has no friends, a 404 status code with an error message is returned.
res.status(404).json({ message: err.message });Method 3: addRemoveFriend
This method is used to add or remove a friend from a user's list of friends.
- It takes in the req and res objects as parameters.
export const addRemoveFriend = async (req, res) => {- The id and friendId are extracted from the request's params object using destructuring.
const { id, friendId } = req.params;- The User model's findById method is used to find the user with the given id.
const user = await User.findById(id);- The User model's findById method is used to find the friend with the given friendId.
const friend = await User.findById(friendId);-
If the friend is already in the user's friend list, both the user and friend's friends arrays are updated to remove each other's ids.
-
If the friend is not already in the user's friend list, both the user and friend's friends arrays are updated to add each other's ids.
if (user.friends.includes(friendId)) {
user.friends = user.friends.filter((id) => id !== friendId);
friend.friends = friend.friends.filter((id) => id !== id);
} else {
user.friends.push(friendId);
friend.friends.push(id);
}- The user and friend are then saved to the database using the save method.
await user.save();
await friend.save();- The Promise.all method is used to retrieve information for each friend by mapping through the user's updated friends array and using the findById method for each friend.
const friends = await Promise.all(
user.friends.map((id) => User.findById(id))
);- The information for each friend is then formatted as a JSON object with only specific properties (e.g. _id, firstName, lastName, occupation, location, picturePath).
const formattedFriends = friends.map(
({ _id, firstName, lastName, occupation, location, picturePath }) => {
return { _id, firstName, lastName, occupation, location, picturePath };
}
);- The updated list of formatted friends is returned as a JSON object with a 200 status code.
res.status(200).json(formattedFriends);- If no user is found, a 404 status code with an error message is returned.
res.status(404).json({ message: err.message });These methods provide basic functionality for managing a user's friend list in GalaxyGossip.