Skip to content

Commit

Permalink
made a get which first checks id then by name
Browse files Browse the repository at this point in the history
  • Loading branch information
zmattes04 committed May 23, 2024
1 parent 3770d12 commit cea41c9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
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

0 comments on commit cea41c9

Please sign in to comment.