Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workshop endpoint w/ populate and no populate versions, index.ts route boilerplate #9

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading