Skip to content

Commit

Permalink
workshop endpoint w/ populate and no populate versions, index.ts boil…
Browse files Browse the repository at this point in the history
…erplate
  • Loading branch information
txingxie committed Nov 3, 2024
1 parent 901ce30 commit f98a897
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
77 changes: 77 additions & 0 deletions api/src/controllers/workshopController.ts
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 });
// }
// };
30 changes: 30 additions & 0 deletions api/src/model/Workshop.ts
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 };
7 changes: 7 additions & 0 deletions api/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
26 changes: 26 additions & 0 deletions api/src/routes/workshop.ts
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;

0 comments on commit f98a897

Please sign in to comment.