diff --git a/api/src/controllers/workshopController.ts b/api/src/controllers/workshopController.ts new file mode 100644 index 0000000..248c831 --- /dev/null +++ b/api/src/controllers/workshopController.ts @@ -0,0 +1,77 @@ +// NO POPULATE VERSION + +import { Request, Response } from 'express'; +import { Workshop } from '../model/Workshop'; + +export const createWorkshop = async (req: Request, res: Response) => { + try { + const { mentorId, menteeId, textContent } = req.body; + + const newWorkshop = new Workshop({ + mentor: mentorId, + mentee: menteeId, + textContent, + }); + + const savedWorkshop = await newWorkshop.save(); + res.status(201).json(savedWorkshop); + } catch (error) { + res.status(500).json({ message: 'Error creating workshop', error }); + } +}; + +export const getWorkshop = async (req: Request, res: Response) => { + try { + const { id } = req.params; + + const workshop = await Workshop.findById(id); + + if (!workshop) { + 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 }); + } +}; + +// POPULATE VERSION (if details of mentor/mentee objects are needed on the frontend like name or picture) + +// import { Request, Response } from 'express'; +// import { Workshop } from '../model/Workshop'; + +// export const createWorkshop = async (req: Request, res: Response) => { +// try { +// const { mentorId, menteeId, textContent } = req.body; + +// const newWorkshop = new Workshop({ +// mentor: mentorId, +// mentee: menteeId, +// textContent, +// }); + +// const savedWorkshop = await newWorkshop.save(); +// res.status(201).json(savedWorkshop); +// } catch (error) { +// res.status(500).json({ message: 'Error creating workshop', error }); +// } +// }; + +// export const getWorkshop = async (req: Request, res: Response) => { +// try { +// const { id } = req.params; + +// const workshop = await Workshop.findById(id) +// .populate('mentor') // Populate full user details for mentor +// .populate('mentee'); // Populate full user details for mentee + +// if (!workshop) { +// 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 }); +// } +// }; diff --git a/api/src/model/Workshop.ts b/api/src/model/Workshop.ts new file mode 100644 index 0000000..3d17871 --- /dev/null +++ b/api/src/model/Workshop.ts @@ -0,0 +1,30 @@ +import mongoose, { Schema, Document } from 'mongoose'; +import { IUser } from './User'; + +// interface +interface IWorkshop extends Document { + mentor: Schema.Types.ObjectId | IUser; + mentee: Schema.Types.ObjectId | IUser; + textContent: string; + createdAt: Date; + + updateContent(newContent: string): Promise; +} + +// workshop schema +const WorkshopSchema: Schema = new Schema({ + mentor: { type: Schema.Types.ObjectId, ref: 'User', required: true }, + mentee: { type: Schema.Types.ObjectId, ref: 'User', required: true }, + textContent: { type: String, required: true }, + createdAt: { type: Date, default: Date.now } +}); + +// update text content of the workshop +WorkshopSchema.methods.updateContent = async function (newContent: string): Promise { + this.textContent = newContent; + await this.save(); +}; + +// mongoose model +const Workshop = mongoose.model('Workshop', WorkshopSchema); +export { Workshop, IWorkshop }; diff --git a/api/src/routes/index.ts b/api/src/routes/index.ts index b6bf883..8520ab4 100644 --- a/api/src/routes/index.ts +++ b/api/src/routes/index.ts @@ -1,7 +1,14 @@ import user from "./user" +import express from 'express'; +import workshopRoutes from './workshop'; // Import workshop routes export { user } +const router = express.Router(); +router.use('/workshops', workshopRoutes); // Use workshop routes + +export default router; + // import express, { Request, Response, Application } from "express" // import cors from "cors" // import dbConnect from "./config/dbConnect" diff --git a/api/src/routes/workshop.ts b/api/src/routes/workshop.ts new file mode 100644 index 0000000..dbe6f3a --- /dev/null +++ b/api/src/routes/workshop.ts @@ -0,0 +1,26 @@ +// NO POPULATE VERSION + +import express from 'express'; +import { createWorkshop, getWorkshop } from '../controllers/workshopController'; + +const router = express.Router(); + +router.post('/workshops', createWorkshop); +// router.get('/workshops/:id', getWorkshop); +router.get('/workshops/:id', async (req: express.Request, res: express.Response) => { + await getWorkshop(req, res); +}); + +export default router; + +// POPULATE VERSION (if details of mentor/mentee objects are needed on the frontend like name or picture) + +// import express from 'express'; +// import { createWorkshop, getWorkshop } from '../controllers/workshopController'; + +// const router = express.Router(); + +// router.post('/workshops', createWorkshop); +// router.get('/workshops/:id', getWorkshop); + +// export default router;