text: const Category = require('../models/category');
  const { errorHandler }  = require('../errhelp/dbErrorHandler');
  
  exports.categoryById = (req, res, next, id) => {
      Category.findById(id).exec((err, category) => {
          if(err || !category) {
              return res.status(400).json({
                  error: "No such category"
              });
          } 
          req.category = category;
          next();
      });
  }
  
  exports.create = (req, res) => {
         const category = new Category(req.body)
         category.save((err, data) => {
             if(err)  {
                 return res.status(400).json({
                        error: errorHandler(err)
                 });
             }
             res.json({ data });
         });
  };
  
  
  exports.read = (req, res) => {
      return res.json(req.category);
  };
  
  
  
  exports.read = (req, res) => {
      return res.json(req.category);
  };
  //update category
  exports.update = (req, res) => {
      console.log('req.body', req.body);
      console.log('category update param', req.params.categoryId);
  
      const category = req.category;
      category.name = req.body.name;
      category.save((err, data) => {
          if (err) {
              return res.status(400).json({
                  error: errorHandler(err)
              });
          }
          res.json(data);
      });
  };
  
  //delete category
  exports.remove = (req, res) => {
      const category = req.category;
      Product.find({ category }).exec((err, data) => {
          if (data.length >= 1) {
              return res.status(400).json({
                  message: `Sorry. You cant delete ${category.name}. It has ${data.length} associated products.`
              });
          } else {
              category.remove((err, data) => {
                  if (err) {
                      return res.status(400).json({
                          error: errorHandler(err)
                      });
                  }
                  res.json({
                      message: 'Category deleted'
                  });
              });
          }
      });
  };
  
  
  //list
  exports.list = (req, res) => {
      Category.find().exec((err, data) => {
          if (err) {
              return res.status(400).json({
                  error: errorHandler(err)
              });
          }
          res.json(data);
      });
  };
  