-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
workshop endpoint w/ populate and no populate versions, index.ts boil…
…erplate
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
// } | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void>; | ||
} | ||
|
||
// workshop schema | ||
const WorkshopSchema: Schema<IWorkshop> = 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<void> { | ||
this.textContent = newContent; | ||
await this.save(); | ||
}; | ||
|
||
// mongoose model | ||
const Workshop = mongoose.model<IWorkshop>('Workshop', WorkshopSchema); | ||
export { Workshop, IWorkshop }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |