text: const Course = require("../models/Course");
  const Bootcamp = require("../models/Bootcamp");
  const ErrorResponse = require("../utils/errorResponse");
  const asyncHandler = require("../middleware/async.js");
  
  // * @route   GET /api/v1/courses
  // * @route   GET /api/v1/bootcamps/:bootcampId/courses
  // @desc    Get
  // @access  Public
  
  exports.getCourses = asyncHandler(async (req, res) => {
    if (req.params.bootcampId) {
      const courses = await Course.find({ bootcamp: req.params.bootcampId });
  
      return res.status(200).json({
        success: true,
        count: courses.length,
        data: courses
      });
    } else {
      res.status(200).json(res.advancedResults);
    }
  });
  
  // * @route   GET /api/v1/courses/:id
  // @desc    Get Single Course
  // @access  Public
  
  exports.getCourse = asyncHandler(async (req, res, next) => {
    const course = await Course.findById(req.params.id).populate({
      path: "bootcamp",
      select: "name description"
    });
  
    if (!course) {
      return next(
        new errorResponse(`No Course with the id of : ${req.params.id}`),
        404
      );
    }
  
    res.status(200).json({ success: true, data: course });
  });
  
  // * @route   POST /api/v1/bootcamps/:bootcampId/courses
  // @desc    Add Course
  // @access  Private
  
  exports.addCourse = asyncHandler(async (req, res, next) => {
    req.body.bootcamp = req.params.bootcampId;
    req.body.user = req.user.id;
    const bootcamp = await Bootcamp.findById(req.params.bootcampId);
  
    if (!bootcamp) {
      return next(
        new errorResponse(
          `No Bootcamp with the id of : ${req.params.bootcampId}`
        ),
        404
      );
    }
  
    // check user is bootcamp owner
    if (bootcamp.user.toString() !== req.user.id && req.user.role !== "admin") {
      return next(
        new ErrorResponse(
          `user with id ${req.user.id} is not authorized to add a course to bootcamp ${bootcamp._id}`,
          401
        )
      );
    }
  
    const course = await Course.create(req.body);
  
    res.status(200).json({ success: true, data: course });
  });
  
  // * @route   PUT /api/v1/courses/:id
  // @desc    Update Course
  // @access  Private
  
  exports.updateCourse = asyncHandler(async (req, res, next) => {
    let course = await Course.findById(req.params.id);
  
    if (!course) {
      return next(
        new errorResponse(`No Course with the id of : ${req.params.id}`)
      );
    }
  
    // check user is course owner
    if (course.user.toString() !== req.user.id && req.user.role !== "admin") {
      return next(
        new ErrorResponse(
          `user with id ${req.user.id} is not authorized to update this course ${course._id}`,
          401
        )
      );
    }
  
    course = await Course.findByIdAndUpdate(req.params.id, req.body, {
      new: true,
      runValidators: true
    });
  
    res.status(200).json({ success: true, data: course });
  });
  
  // * @route   DELETE /api/v1/courses/:id
  // @desc    DELETE Course
  // @access  Private
  
  exports.deleteCourse = asyncHandler(async (req, res, next) => {
    const course = await Course.findById(req.params.id);
  
    if (!course) {
      return next(
        new ErrorResponse(`No Course with the id of : ${req.params.id}`)
      );
    }
  
    // check user is course owner
    if (course.user.toString() !== req.user.id && req.user.role !== "admin") {
      return next(
        new ErrorResponse(
          `user with id ${req.user.id} is not authorized to Delete this course ${course._id}`,
          401
        )
      );
    }
  
    await course.remove();
  
    res.status(200).json({ success: true, data: {} });
  });
  