const Course = require("../models/Course"); const User = require("../models/User"); // we use req,res because it is not use in our rout module.exports.addCourse = (req,res) => { let newCourse = new Course({ name:req.body.name, description:req.body.description, price:req.body.price }); return newCourse.save().then((course,error ) =>{ if(error){ return res.send(false); }else{ return res.send(true); } }).catch(); } module.exports.getAllCourses = (req,res) =>{ // to retrieve all the documents in the "course" collection, we will use find({}) method. return Product.find({}).then(result =>{ return res.send(result); }); } module.exports.activeproduct = (req,res) =>{ return Course.find({isActive:true}).then(result =>{ return res.send(result); }); } // module.exports.getCourse = (req,res) =>{ // return Course.findById(req.params.courseId).then(result =>{ // return res.send(result); // }); // } // module.exports.updateCourse = (req, res) => { // // Specify the fields/properties of the document to be updated // let updatedCourse = { // name: req.body.name, // description: req.body.description, // price: req.body.price // } // // Syntax // // findByIdAndUpdate(documentId, updatesToBeApplied) // return Course.findByIdAndUpdate(req.params.courseId, updatedCourse).then((course, error) => { // // If course was not updated // if(error){ // return res.send(false); // // If course was updated successfully // } else { // return res.send(true); // } // }); // } // module.exports.archiveCourse = (req,res)=>{ // let updatedActiveField = { // isActive:false // } // return Course.findByIdAndUpdate(req.params.courseId, updatedActiveField).then((course, error) =>{ // if(error){ // return res.send(false); // // If course was updated successfully // } else { // return res.send(true); // } // }); // } // module.exports.searchCoursesByName = async (req, res) => { // try { // const { courseName } = req.body; // // Use a regular expression to perform a case-insensitive search // // i = this will search something that case- insentitive will not matter // // // const courses = await Course.find({ // name: { $regex: courseName, $options: 'i' } // }); // res.json(courses); // } catch (error) { // console.error(error); // res.status(500).json({ error: 'Internal Server Error' }); // } // }; module.exports.getEmailsOfEnrolledUsers = async (req, res) => { const courseId = req.params.courseId; // Use req.params instead of req.body try { // Find the course by courseId const course = await Course.findById(courseId); if (!course) { return res.status(404).json({ message: 'Course not found' }); } // Get the userIds of enrolled users from the course const userIds = course.enrollees.map(enrollee => enrollee.userId); // Find the users with matching userIds const enrolledUsers = await User.find({ _id: { $in: userIds } }); // Use userIds variable instead of undefined "users" // Extract the emails from the enrolled users const emails = enrolledUsers.map(user => user.email); // Use map instead of forEach res.status(200).json({ userEmails: emails }); // Use the correct variable name userEmails instead of emails } catch (error) { res.status(500).json({ message: 'An error occurred while retrieving enrolled users' }); } }; module.exports.activateCourse = (req,res)=>{ let updatedActiveField = { isActive:true } return Course.findByIdAndUpdate(req.params.courseId, updatedActiveField).then((course, error) =>{ if(error){ return res.send(false); // If course was updated successfully } else { return res.send(true); } }); } // Controller function to update the user profile module.exports.updateProfile = async (req, res) => { try { // Get the user ID from the authenticated token const userId = req.user.id; // Retrieve the updated profile information from the request body const { firstName, lastName, mobileNo } = req.body; // Update the user's profile in the database const updatedUser = await User.findByIdAndUpdate( userId, { firstName, lastName, mobileNo }, { new: true } ); res.json(updatedUser); } catch (error) { console.error(error); res.status(500).json({ message: 'Failed to update profile' }); } }