<commit_before>var User = require('../models/user');
var Category = require('../models/category');

module.exports = {
  index : function(req, res){
    Category.find({}, function(err, category){
      if(err) res.send(err);
      res.render('admin/category',{
          category:category
      });
    });
  },
  new : function(req, res){
    res.render('admin/category');
  },

  get : function(req, res){
    Category.find({}, function(err, categories){
      if(err) res.send(err);
      res.send(categories);
    });
  },

  add : function(req, res){
    Category.findOne({name:req.body.category}, function(err, foundCategory){
      if(err) res.send(err);
      if(foundCategory) res.send("category already exists idiot");
      else{
        var category = new Category();
        category.name = req.body.name;

        category.save(function(err, category){
          if(err) res.send(err);
          
          //res.flash('message', 'Category saved successfully');
          res.redirect('/admin/add');
        });
      }
    });
  }
};<commit_msg>add comments to define function of routes, add routes to delete categories, change rendering directory to admin/directory/... for routes with templates, change redirect directory to /admin/index<commit_after>var User     = require('../models/user');
var Category = require('../models/category');

module.exports = {
  index : function(req, res){
      //route should assist viewing and getting categories in the db and displaying in template form
    Category.find({}, function(err, category){
      if(err) res.send(err);
      res.render('admin/category/index',{
          category:category
      });
    });
  },

  new : function(req, res){
    //route should give template to add categories to db
    res.render('admin/category/new');
  },

  get : function(req, res){
    //route should send categories to front page
    Category.find({}, function(err, categories){
      if(err) res.send(err);
      res.send(categories);
    });
  },

  delete : function(req, res){
    //route should delete category according to id
    Category.findOne({_id:req.params.id}, function(err, category){
      if(err) res.send(err);
      if(category){
        category.remove(function(err){
          if(err) res.send(err);
          res.redirect('/admin/category/index');
        });
      }else{
        res.send("can't delete category not in the system");
      }
    });
  },

  add : function(req, res){
    //route for adding category to database which first checks to avoid duplication
    Category.findOne({name:req.body.category}, function(err, foundCategory){
      if(err) res.send(err);
      if(foundCategory){ 
        res.send("category already exists idiot");
        console.log(foundCategory);
      }
      else{
        var category = new Category();
        category.name = req.body.name;

        category.save(function(err, category){
          if(err) res.send(err);
          
          //res.flash('message', 'Category saved successfully');
          res.redirect('/admin/index');
        });
      }
    });
  }
};
{'fraction_non_alphanumeric': 0.15080906148867315, 'fraction_numerical': 0.0, 'mean_word_length': 2.58584686774942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3992786', 'n_tokens_mistral': 880, 'n_tokens_neox': 867, 'n_words': 215}