Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10 create the friends list component #57

Merged
merged 12 commits into from
May 22, 2024
7 changes: 5 additions & 2 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ app.use((req: Request, res: Response, next: NextFunction) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
"Origin, X-Requested-With, Content-Type, Accept",
);
res.header(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, OPTIONS, DELETE, PUT",
);
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE, PUT");
next();
});

Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.12.11",
"@typescript-eslint/parser": "^7.10.0",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"eslint": "^9.3.0",
"nodemon": "^3.1.0"
}
Expand Down
46 changes: 39 additions & 7 deletions backend/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import User, { IUser } from "../models/userSchema";
import connectDB from "../connection";
import { authenticateUser, generateAccessToken } from "../auth";
import bcrypt from "bcrypt";

import mongoose from "mongoose";
const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
Expand All @@ -22,19 +22,26 @@ router.get("/", async (req: Request, res: Response) => {
}
});

router.get("/:id", async (req: Request, res: Response) => {
router.get("/:userid", async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();
const { id } = req.params;

try {
const user = await User.findById(id);
console.log("Here");

// Use findById correctly with the id parameter from the request
const user = await User.findById(req.params.userid);

// Check if group is null or undefined
if (!user) {
res.status(404).send("User not found");
} else {
res.send(user);
return res.status(404).send("No users found"); // Use return to exit the function after sending the response
}

// Send the found user
res.send(user);
console.log("Sent user");
} catch (error) {
console.error("Error fetching user:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
}
});
Expand Down Expand Up @@ -130,4 +137,29 @@ router.delete("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id/remove-friend", async (req: Request, res: Response) => {
connectDB();
const userId = req.params.id;
const { friendId } = req.body; // Expecting friendId in the request body
console.log(friendId);
try {
const user = await User.findById(userId);
console.log(user);
if (user) {
// Remove the friend's ObjectId from the user's friends array
user.friends = user.friends.filter(
(friend: mongoose.Types.ObjectId) => !friend.equals(friendId),
);
await user.save();

res.status(200).send({ message: "Friend removed successfully" });
} else {
res.status(404).send({ message: "User not found" });
}
} catch (error) {
console.error("Error removing friend:", error);
res.status(500).send({ message: "Internal server error" });
}
});

export { router as userEndpoints };
Loading
Loading