text: const {Category} = require('../models/category');
  const express = require('express');
  const router = express.Router();
  
  router.get(`/`, async (req, res) => {
      const categoryList = await Category.find();
  
      if(!categoryList) {
          res.status(500).json({success: false})
      }
      res.status(200).send(categoryList);
  })
  
  router.get('/:id', async(req, res) => {
      const category = await Category.findById(req.params.id);
  
      if(!category) {
          res.status(500).json({message: "The category with the given id was not found"});
      }
      res.status(200).send(category);
  })
  
  router.post(`/`, async (req, res) => {
      let category = new Category({
          name: req.body.name,
          icon: req.body.icon,
          color: req.body.color
      });
  
      category = await category.save();
  
      if(!category) {
          return res.status(400).send("The category can't be created")
      }
  
      res.send(category);
  })
  
  router.put(`/:id`, async (req, res) => {
      const category = await Category.findByIdAndUpdate(
          req.params.id, {
              name: req.body.name,
              icon: req.body.icon,
              color: req.body.color
          },
          {
              new: true // return the new updated data in postman response (not the old data before updating)
          }
      );
  
      if(!category) {
          return res.status(400).send("The category can't be created")
      }
  
      res.send(category);
  })
  
  router.delete(`/:id`, async (req, res) => {
      Category.findByIdAndRemove(req.params.id).then(category => {
          if (category) {
              return res.status(200).json({success: true, message: "The category is deleted"});
          } else{
              return res.status(404).json({success: false, message: "This category doesn't exist"});
          }
      }).catch(err => {
          return res.status(400).json({success: false, error: err});
      });
  })
  
  module.exports = router;