From d77cc25c4d5404cf68a00c38d0b0cc57af40927a Mon Sep 17 00:00:00 2001 From: Samuel Thompson Date: Fri, 15 Nov 2024 21:31:24 -0600 Subject: [PATCH 1/5] Create getWorkshopsByUserId endpoint --- api/src/controllers/workshopController.ts | 18 ++++++++++++++++++ api/src/routes/workshop.ts | 7 ++++++- 2 files changed, 24 insertions(+), 1 deletion(-) 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'; From 30d24ec0bd9975fec34567b226979f521804e9df Mon Sep 17 00:00:00 2001 From: Samuel Thompson Date: Sun, 8 Dec 2024 15:33:33 -0600 Subject: [PATCH 2/5] Formatted with prettier --- api/src/routes/workshop.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api/src/routes/workshop.ts b/api/src/routes/workshop.ts index f923e48..1e0014f 100644 --- a/api/src/routes/workshop.ts +++ b/api/src/routes/workshop.ts @@ -2,7 +2,11 @@ import express from "express"; import mongoose from "mongoose"; import dbConnect from "../config/db"; // Import the dbConnect function -import { createWorkshop, getWorkshop, getWorkshopsByUserId } from "../controllers/workshopController"; +import { + createWorkshop, + getWorkshop, + getWorkshopsByUserId, +} from "../controllers/workshopController"; const router = express.Router(); @@ -48,13 +52,16 @@ router.get( "/workshops/:id", async (req: express.Request, res: express.Response) => { await getWorkshop(req, res); - }, + } ); // Route to get workshops by user ID -router.get("/workshops/user/:userId", async (req: express.Request, res: express.Response) => { - await getWorkshopsByUserId(req, res); -}); +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) From b4eb6d75b7c01ed228897c14f3ec7d5ef30855e4 Mon Sep 17 00:00:00 2001 From: Samuel Thompson Date: Sun, 8 Dec 2024 15:35:42 -0600 Subject: [PATCH 3/5] Formatted with prettier workshop controller --- api/src/controllers/workshopController.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/src/controllers/workshopController.ts b/api/src/controllers/workshopController.ts index 77c1eac..106955f 100644 --- a/api/src/controllers/workshopController.ts +++ b/api/src/controllers/workshopController.ts @@ -45,7 +45,9 @@ export const getWorkshopsByUserId = async (req: Request, res: Response) => { }); if (workshops.length === 0) { - return res.status(404).json({ message: "No workshops found for this user" }); + return res + .status(404) + .json({ message: "No workshops found for this user" }); } res.status(200).json(workshops); From 391e3dfd02c3cfd6af3d9b49a1ffe75b041aea62 Mon Sep 17 00:00:00 2001 From: Samuel Thompson Date: Sun, 8 Dec 2024 15:44:12 -0600 Subject: [PATCH 4/5] Reformatted --- api/src/controllers/workshopController.ts | 14 ++++++------ api/src/routes/workshop.ts | 26 +++++++++++------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/api/src/controllers/workshopController.ts b/api/src/controllers/workshopController.ts index 106955f..8041c49 100644 --- a/api/src/controllers/workshopController.ts +++ b/api/src/controllers/workshopController.ts @@ -1,7 +1,7 @@ // NO POPULATE VERSION -import { Request, Response } from "express"; -import { Workshop } from "../model/Workshop"; +import { Request, Response } from 'express'; +import { Workshop } from '../model/Workshop'; export const createWorkshop = async (req: Request, res: Response) => { try { @@ -16,7 +16,7 @@ export const createWorkshop = async (req: Request, res: Response) => { const savedWorkshop = await newWorkshop.save(); res.status(201).json(savedWorkshop); } catch (error) { - res.status(500).json({ message: "Error creating workshop", error }); + res.status(500).json({ message: 'Error creating workshop', error }); } }; @@ -27,12 +27,12 @@ export const getWorkshop = async (req: Request, res: Response) => { const workshop = await Workshop.findById(id); if (!workshop) { - return res.status(404).json({ message: "Workshop not found" }); + return res.status(404).json({ message: 'Workshop not found' }); } res.status(200).json(workshop); } catch (error) { - res.status(500).json({ message: "Error retrieving workshop", error }); + res.status(500).json({ message: 'Error retrieving workshop', error }); } }; @@ -47,12 +47,12 @@ export const getWorkshopsByUserId = async (req: Request, res: Response) => { if (workshops.length === 0) { return res .status(404) - .json({ message: "No workshops found for this user" }); + .json({ message: 'No workshops found for this user' }); } res.status(200).json(workshops); } catch (error) { - res.status(500).json({ message: "Error retrieving workshops", error }); + res.status(500).json({ message: 'Error retrieving workshops', error }); } }; diff --git a/api/src/routes/workshop.ts b/api/src/routes/workshop.ts index 1e0014f..a05acd7 100644 --- a/api/src/routes/workshop.ts +++ b/api/src/routes/workshop.ts @@ -1,12 +1,12 @@ -import express from "express"; -import mongoose from "mongoose"; -import dbConnect from "../config/db"; // Import the dbConnect function +import express from 'express'; +import mongoose from 'mongoose'; +import dbConnect from '../config/db'; // Import the dbConnect function import { createWorkshop, getWorkshop, getWorkshopsByUserId, -} from "../controllers/workshopController"; +} from '../controllers/workshopController'; const router = express.Router(); @@ -19,14 +19,14 @@ const workshopIDSchema = new mongoose.Schema({ s3ID: String, }); -const Workshop = mongoose.model("WorkshopID", workshopIDSchema); +const Workshop = mongoose.model('WorkshopID', workshopIDSchema); // Route to create a new workshop -router.post("/create-workshop", async (req: any, res: any) => { +router.post('/create-workshop', async (req: any, res: any) => { const { name, s3id } = req.body; if (!name || !s3id) { - return res.status(400).json({ message: "Missing required fields" }); + return res.status(400).json({ message: 'Missing required fields' }); } // Create a new workshop @@ -38,29 +38,29 @@ router.post("/create-workshop", async (req: any, res: any) => { try { const savedWorkshop = await newWorkshop.save(); res.status(201).json({ - message: "Workshop created successfully", + message: 'Workshop created successfully', WorkshopID: savedWorkshop, }); } catch (error) { - res.status(401).json({ message: "Failed to create workshop", error }); + res.status(401).json({ message: 'Failed to create workshop', error }); } }); // router.post("/workshops", createWorkshop) // router.get('/workshops/:id', getWorkshop); router.get( - "/workshops/:id", + '/workshops/:id', async (req: express.Request, res: express.Response) => { await getWorkshop(req, res); - } + }, ); // Route to get workshops by user ID router.get( - "/workshops/user/:userId", + '/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) From fe18203feea660549d3b7ce7e6397d5e9bf15fed Mon Sep 17 00:00:00 2001 From: Samuel Thompson Date: Sun, 8 Dec 2024 15:53:09 -0600 Subject: [PATCH 5/5] Reformatted again --- api/src/controllers/workshopController.ts | 14 +++++++------- api/src/routes/workshop.ts | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/api/src/controllers/workshopController.ts b/api/src/controllers/workshopController.ts index 8041c49..106955f 100644 --- a/api/src/controllers/workshopController.ts +++ b/api/src/controllers/workshopController.ts @@ -1,7 +1,7 @@ // NO POPULATE VERSION -import { Request, Response } from 'express'; -import { Workshop } from '../model/Workshop'; +import { Request, Response } from "express"; +import { Workshop } from "../model/Workshop"; export const createWorkshop = async (req: Request, res: Response) => { try { @@ -16,7 +16,7 @@ export const createWorkshop = async (req: Request, res: Response) => { const savedWorkshop = await newWorkshop.save(); res.status(201).json(savedWorkshop); } catch (error) { - res.status(500).json({ message: 'Error creating workshop', error }); + res.status(500).json({ message: "Error creating workshop", error }); } }; @@ -27,12 +27,12 @@ export const getWorkshop = async (req: Request, res: Response) => { const workshop = await Workshop.findById(id); if (!workshop) { - return res.status(404).json({ message: 'Workshop not found' }); + return res.status(404).json({ message: "Workshop not found" }); } res.status(200).json(workshop); } catch (error) { - res.status(500).json({ message: 'Error retrieving workshop', error }); + res.status(500).json({ message: "Error retrieving workshop", error }); } }; @@ -47,12 +47,12 @@ export const getWorkshopsByUserId = async (req: Request, res: Response) => { if (workshops.length === 0) { return res .status(404) - .json({ message: 'No workshops found for this user' }); + .json({ message: "No workshops found for this user" }); } res.status(200).json(workshops); } catch (error) { - res.status(500).json({ message: 'Error retrieving workshops', error }); + res.status(500).json({ message: "Error retrieving workshops", error }); } }; diff --git a/api/src/routes/workshop.ts b/api/src/routes/workshop.ts index a05acd7..9fd6946 100644 --- a/api/src/routes/workshop.ts +++ b/api/src/routes/workshop.ts @@ -1,12 +1,12 @@ -import express from 'express'; -import mongoose from 'mongoose'; -import dbConnect from '../config/db'; // Import the dbConnect function +import express from "express"; +import mongoose from "mongoose"; +import dbConnect from "../config/db"; // Import the dbConnect function import { createWorkshop, getWorkshop, getWorkshopsByUserId, -} from '../controllers/workshopController'; +} from "../controllers/workshopController"; const router = express.Router(); @@ -19,14 +19,14 @@ const workshopIDSchema = new mongoose.Schema({ s3ID: String, }); -const Workshop = mongoose.model('WorkshopID', workshopIDSchema); +const Workshop = mongoose.model("WorkshopID", workshopIDSchema); // Route to create a new workshop -router.post('/create-workshop', async (req: any, res: any) => { +router.post("/create-workshop", async (req: any, res: any) => { const { name, s3id } = req.body; if (!name || !s3id) { - return res.status(400).json({ message: 'Missing required fields' }); + return res.status(400).json({ message: "Missing required fields" }); } // Create a new workshop @@ -38,18 +38,18 @@ router.post('/create-workshop', async (req: any, res: any) => { try { const savedWorkshop = await newWorkshop.save(); res.status(201).json({ - message: 'Workshop created successfully', + message: "Workshop created successfully", WorkshopID: savedWorkshop, }); } catch (error) { - res.status(401).json({ message: 'Failed to create workshop', error }); + res.status(401).json({ message: "Failed to create workshop", error }); } }); // router.post("/workshops", createWorkshop) // router.get('/workshops/:id', getWorkshop); router.get( - '/workshops/:id', + "/workshops/:id", async (req: express.Request, res: express.Response) => { await getWorkshop(req, res); }, @@ -57,7 +57,7 @@ router.get( // Route to get workshops by user ID router.get( - '/workshops/user/:userId', + "/workshops/user/:userId", async (req: express.Request, res: express.Response) => { await getWorkshopsByUserId(req, res); },