Skip to content

Commit

Permalink
Create getWorkshopsByUserId endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
samthomp14 committed Nov 16, 2024
1 parent 98da4e5 commit d77cc25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 18 additions & 0 deletions api/src/controllers/workshopController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 6 additions & 1 deletion api/src/routes/workshop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit d77cc25

Please sign in to comment.