diff --git a/backend/routes/groupRoutes.ts b/backend/routes/groupRoutes.ts index d313496..b1fcede 100644 --- a/backend/routes/groupRoutes.ts +++ b/backend/routes/groupRoutes.ts @@ -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"); + } } }); diff --git a/backend/routes/itemRoutes.ts b/backend/routes/itemRoutes.ts index 59b4d1e..a9c71bf 100644 --- a/backend/routes/itemRoutes.ts +++ b/backend/routes/itemRoutes.ts @@ -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 {