Skip to content
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
16 changes: 13 additions & 3 deletions backend/controllers/courseController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export const getCourseById = async (

if (id) {
// Find course by ID and populate related fields
const course = await Course.findById(id).populate(["speakers"]).exec();
const course = await Course.findById(id)
.populate(["speakers", "handouts"])
.exec();

if (!course) {
res.status(404).json({
Expand Down Expand Up @@ -118,6 +120,7 @@ export const createCourse = async (
productInfo,
shortUrl,
draft,
registrationLimit,
} = req.body;

// Validate required fields
Expand Down Expand Up @@ -175,6 +178,7 @@ export const createCourse = async (
productInfo,
shortUrl,
draft,
registrationLimit,
});

const savedCourseResponse = await newCourseResponse.save();
Expand Down Expand Up @@ -402,9 +406,15 @@ export const getCourseUsers = async (
return;
}

// Check if the course exists and populate the students field
// Check if the course exists and populate the students field with their roles
console.log("Looking up course in database...");
const course = await Course.findById(courseId).populate("students");
const course = await Course.findById(courseId).populate({
path: "students",
populate: {
path: "role",
model: "UserType",
},
});
console.log("Found course:", course ? "Yes" : "No");
if (!course) {
console.log("Course not found in database");
Expand Down
31 changes: 26 additions & 5 deletions backend/controllers/handoutController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Request, Response } from "express";
import Handout from "../models/handoutModel";
import Course from "../models/courseModel";

// @desc Get all handouts or filter handouts by courseId
// @route GET /api/handouts
// @access Public
export const getHandouts = async (req: Request, res: Response): Promise<void> => {
export const getHandouts = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { courseId } = req.query;

Expand Down Expand Up @@ -39,7 +43,10 @@ export const getHandouts = async (req: Request, res: Response): Promise<void> =>
// @desc Create a new handout
// @route POST /api/handouts
// @access Public
export const createHandout = async (req: Request, res: Response): Promise<void> => {
export const createHandout = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { courseId, fileUrl, fileType } = req.body;

Expand Down Expand Up @@ -82,7 +89,10 @@ export const createHandout = async (req: Request, res: Response): Promise<void>
// @desc Update a handout
// @route PUT /api/handouts/:id
// @access Public
export const updateHandout = async (req: Request, res: Response): Promise<void> => {
export const updateHandout = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { id } = req.params;
const { fileUrl, fileType } = req.body;
Expand Down Expand Up @@ -124,9 +134,13 @@ export const updateHandout = async (req: Request, res: Response): Promise<void>
// @desc Delete a handout
// @route DELETE /api/handouts/:id
// @access Public
export const deleteHandout = async (req: Request, res: Response): Promise<void> => {
export const deleteHandout = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { id } = req.params;
const { courseId } = req.body;

const deletedHandout = await Handout.findByIdAndDelete(id);
if (!deletedHandout) {
Expand All @@ -137,6 +151,13 @@ export const deleteHandout = async (req: Request, res: Response): Promise<void>
return;
}

// Remove reference from Course.handouts
if (courseId) {
await Course.findByIdAndUpdate(courseId, {
$pull: { handouts: id },
});
}

res.status(200).json({
success: true,
data: deletedHandout,
Expand All @@ -155,4 +176,4 @@ export const deleteHandout = async (req: Request, res: Response): Promise<void>
});
}
}
};
};
229 changes: 121 additions & 108 deletions backend/controllers/surveyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,126 +5,139 @@ import Survey, { ISurvey } from "../models/surveyModel";
// @route GET /api/surveys
// @access Public
export const getSurvey = async (req: Request, res: Response): Promise<void> => {
try {
// Use `findOneAndUpdate` to find the survey and create one if it doesn't exist
const survey = await Survey.findOneAndUpdate(
{}, // Find the first document (survey)
{ $setOnInsert: { questions: [] } }, // If no document is found, insert a new one with empty questions
{ new: true, upsert: true } // Return the updated/new document, and if not found, create it
).populate({
path: "questions",
model: "Question",
});

res.status(200).json(survey);
} catch (error) {
if (error instanceof Error) {
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
try {
// Use `findOneAndUpdate` to find the survey and create one if it doesn't exist
const survey = await Survey.findOneAndUpdate(
{}, // Find the first document (survey)
{ $setOnInsert: { questions: [] } }, // If no document is found, insert a new one with empty questions
{ new: true, upsert: true } // Return the updated/new document, and if not found, create it
).populate({
path: "questions",
model: "Question",
});

res.status(200).json(survey);
} catch (error) {
if (error instanceof Error) {
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
};

// @desc Create a new survey (only if it doesn't already exist)
// @route POST /api/surveys
// @access Public
// export const createSurvey = async (req: Request, res: Response): Promise<void> => {
// try {
// // Check if a survey already exists
// const existingSurvey = await Survey.findOne();

// if (existingSurvey) {
// res.status(200).json({
// survey: existingSurvey,
// message: "Survey already exists. You can update it instead.",
// });
// return;
// }

// const { questions } = req.body;

// if (!questions) {
// res.status(400).json({ message: "Questions are required" });
// return;
// }

// // Create a new survey
// const survey = new Survey({
// questions,
// });
// await survey.save();

// res.status(201).json({
// survey,
// message: "Survey created successfully",
// });
// } catch (error) {
// if (error instanceof Error) {
// res.status(500).json({ message: error.message });
// } else {
// res.status(500).json({ message: "An unknown error occurred" });
// }
// }
// };
export const createSurvey = async (
req: Request,
res: Response
): Promise<void> => {
try {
// Check if a survey already exists
const existingSurvey = await Survey.findOne();

if (existingSurvey) {
res.status(200).json({
survey: existingSurvey,
message: "Survey already exists. You can update it instead.",
});
return;
}

const { questions } = req.body;

if (!questions) {
res.status(400).json({ message: "Questions are required" });
return;
}

// Create a new survey
const survey = new Survey({
questions,
});
await survey.save();

res.status(201).json({
survey,
message: "Survey created successfully",
});
} catch (error) {
if (error instanceof Error) {
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
};

// @desc Update the existing survey
// @route PUT /api/surveys
// @access Public
export const updateSurvey = async (req: Request, res: Response): Promise<void> => {
try {
const { questions } = req.body;

// Find the single existing survey (since there is only one survey)
const survey = await Survey.findOne();

if (!survey) {
res.status(404).json({ message: "Survey not found" });
return;
}

// Update the survey with new questions
survey.question = questions;
await survey.save();

res.status(200).json(survey);
} catch (error) {
if (error instanceof Error) {
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
export const updateSurvey = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { questions } = req.body;

console.log(questions);

// Find the single existing survey (since there is only one survey)
const survey = await Survey.findOne();

if (!survey) {
res.status(404).json({ message: "Survey not found" });
return;
}

// Update the survey with new questions
survey.questions = questions;
await survey.save();
console.log("survey", survey);

res.status(200).json(survey);
} catch (error) {
if (error instanceof Error) {
console.error(error);
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
};

// @desc Delete the survey
// @route DELETE /api/surveys
// @access Public
export const deleteSurvey = async (req: Request, res: Response): Promise<void> => {
try {
// Since there is only one survey, no need to look for it by ID
const survey = await Survey.findOne();

if (!survey) {
res.status(404).json({
success: false,
message: "Survey not found",
});
return;
}

// Delete the survey
await Survey.deleteOne({ _id: survey._id });

res.status(200).json({
success: true,
message: "Survey deleted successfully",
});
} catch (error) {
if (error instanceof Error) {
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
export const deleteSurvey = async (
req: Request,
res: Response
): Promise<void> => {
try {
// Since there is only one survey, no need to look for it by ID
const survey = await Survey.findOne();

if (!survey) {
res.status(404).json({
success: false,
message: "Survey not found",
});
return;
}

// Delete the survey
await Survey.deleteOne({ _id: survey._id });

res.status(200).json({
success: true,
message: "Survey deleted successfully",
});
} catch (error) {
if (error instanceof Error) {
res.status(500).json({ message: error.message });
} else {
res.status(500).json({ message: "An unknown error occurred" });
}
}
};
Loading