diff --git a/api/src/controllers/workshopController.ts b/api/src/controllers/workshopController.ts index 8fd36c4..77c1eac 100644 --- a/api/src/controllers/workshopController.ts +++ b/api/src/controllers/workshopController.ts @@ -36,6 +36,24 @@ export const getWorkshop = async (req: Request, res: Response) => { } }; +export const getWorkshopsByUserId = async (req: Request, res: Response) => { + try { + const { userId } = req.params; + + const workshops = await Workshop.find({ + $or: [{ mentor: userId }, { mentee: userId }], + }); + + if (workshops.length === 0) { + return res.status(404).json({ message: "No workshops found for this user" }); + } + + res.status(200).json(workshops); + } catch (error) { + res.status(500).json({ message: "Error retrieving workshops", error }); + } +}; + // POPULATE VERSION (if details of mentor/mentee objects are needed on the frontend like name or picture) // import { Request, Response } from 'express'; diff --git a/api/src/routes/workshop.ts b/api/src/routes/workshop.ts index 4fa779c..f923e48 100644 --- a/api/src/routes/workshop.ts +++ b/api/src/routes/workshop.ts @@ -2,7 +2,7 @@ import express from "express"; import mongoose from "mongoose"; import dbConnect from "../config/db"; // Import the dbConnect function -import { createWorkshop, getWorkshop } from "../controllers/workshopController"; +import { createWorkshop, getWorkshop, getWorkshopsByUserId } from "../controllers/workshopController"; const router = express.Router(); @@ -51,6 +51,11 @@ router.get( }, ); +// Route to get workshops by user ID +router.get("/workshops/user/:userId", async (req: express.Request, res: express.Response) => { + await getWorkshopsByUserId(req, res); +}); + // POPULATE VERSION (if details of mentor/mentee objects are needed on the frontend like name or picture) // import express from 'express';