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

made a get which first checks id then by name #61

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions backend/routes/groupRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,32 @@ router.get("/:groupid", async (req: Request, res: Response) => {
console.log("Here");

// Use findById correctly with the id parameter from the request
const group = await Group.findById(req.params.groupid);
const groupById = await Group.findById(req.params.groupid);

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

// Send the found user
res.send(group);
res.send(groupById);
console.log("Sent Group");
} catch (error) {
console.error("Error fetching group:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
console.log("Now trying to find by GroupName");
try {
const groupsByName = await Group.find({ groupName: req.params.groupid });
console.log(groupsByName);
if (!groupsByName) {
return res.status(404).send("No groups found"); // Use return to exit the function after sending the response
}

// Send the found user
res.send(groupsByName);
console.log("Sent Groups");
} catch (error) {
console.error("Error fetching group:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
}
}
});

Expand Down
36 changes: 36 additions & 0 deletions backend/routes/itemRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ router.get("/", async (req: Request, res: Response) => {
}
});

router.get("/:itemid", async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

try {
console.log("Here");

// Use findById correctly with the id parameter from the request
const itemById = await Item.findById(req.params.itemid);

// Check if group is null or undefined
if (!itemById) {
return res.status(404).send("No item found"); // Use return to exit the function after sending the response
}
// Send the found user
res.send(itemById);
console.log("Sent item");
} catch (error) {
console.log("Now trying to find by Name");
try {
const itemsByName = await Item.find({ name: req.params.itemid });
console.log(itemsByName);
if (!itemsByName) {
return res.status(404).send("No items found"); // Use return to exit the function after sending the response
}

// Send the found user
res.send(itemsByName);
console.log("Sent items");
} catch (error) {
console.error("Error fetching group:", error); // Log the error for debugging
res.status(500).send("Internal Server Error");
}
}
});

router.post("/", async (req: Request, res: Response) => {
connectDB();
try {
Expand Down
Loading