<commit_before>const Router = require('express').Router;
const bodyParser = require('body-parser').json();
const handleErr = require(__dirname + '/../lib/handle_err');

module.exports = function(connection, authenticat) {
  const Recipe = require(__dirname + '/../models/recipe')(connection);
  var recipeRouter = Router();

  recipeRouter.post('/recipes', authenticat.tokenAuth, bodyParser, (req, res) => {
    var newRecipe = new Recipe(req.body);
    newRecipe.creatorId = req.user._id;

    newRecipe.save((err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  recipeRouter.put('/recipes/:id', authenticat.tokenAuth, bodyParser, (req, res) => {
    var recipeData = req.body;
    delete recipeData._id;
    Recipe.update({ _id: req.params.id }, recipeData, (err, data) => {
      if (!data.nModified) return res.status(500).json({ msg: 'no recipe found' });
      if (err) return handleErr(err, res);
      res.status(200).json({ msg: 'recipe updated' });
    });
  });

  recipeRouter.delete('/recipes/:id', authenticat.tokenAuth, (req, res) => {
    Recipe.findOneAndRemove({ _id: req.params.id }, (err) => {
      if (err) return handleErr(err, res);
      res.status(200).json({ msg: 'recipe deleted' });
    });
  });

  recipeRouter.get('/recipes/all', authenticat.tokenAuth, (req, res) => {
    Recipe.find(null, (err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  recipeRouter.get('/recipes/mine', authenticat.tokenAuth, (req, res) => {
    Recipe.find({ creatorId: req.user._id }, (err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  recipeRouter.get('/recipes/:id', authenticat.tokenAuth, (req, res) => {
    Recipe.findOne({ _id: req.params.id }, (err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  return recipeRouter;
};<commit_msg>roles and admin protection to delete route<commit_after>const Router = require('express').Router;
const bodyParser = require('body-parser').json();
const handleErr = require(__dirname + '/../lib/handle_err');

module.exports = function(connection, authenticat) {
  const Recipe = require(__dirname + '/../models/recipe')(connection);
  var recipeRouter = Router();

  recipeRouter.post('/recipes', authenticat.tokenAuth, bodyParser, (req, res) => {
    var newRecipe = new Recipe(req.body);
    newRecipe.creatorId = req.user._id;

    newRecipe.save((err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  recipeRouter.put('/recipes/:id', authenticat.tokenAuth, bodyParser, (req, res) => {
    var recipeData = req.body;
    delete recipeData._id;
    Recipe.update({ _id: req.params.id }, recipeData, (err, data) => {
      if (!data.nModified) return res.status(500).json({ msg: 'no recipe found' });
      if (err) return handleErr(err, res);
      res.status(200).json({ msg: 'recipe updated' });
    });
  });

  recipeRouter.delete('/recipes/:id', authenticat.tokenAuth, authenticat.roleAuth('baker'),
  (req, res) => {
    if (req.user.admin) {
      return Recipe.findOneAndRemove({ _id: req.params.id }, (err) => {
        if (err) return handleErr(err, res);
        res.status(200).json({ msg: 'recipe deleted by admin' });
      });
    }

    Recipe.findOneAndRemove({ _id: req.params.id, creatorId: req.user._id }, (err) => {
      if (err) return handleErr(err, res);
      res.status(200).json({ msg: 'recipe deleted by creator' });
    });
  });

  recipeRouter.get('/recipes/all', authenticat.tokenAuth, (req, res) => {
    Recipe.find(null, (err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  recipeRouter.get('/recipes/mine', authenticat.tokenAuth, authenticat.roleAuth('baker'),
  (req, res) => {
    Recipe.find({ creatorId: req.user._id }, (err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  recipeRouter.get('/recipes/:id', authenticat.tokenAuth, (req, res) => {
    Recipe.findOne({ _id: req.params.id }, (err, data) => {
      if (err) return handleErr(err, res);
      res.status(200).json(data);
    });
  });

  return recipeRouter;
};
{'fraction_non_alphanumeric': 0.1893956934475573, 'fraction_numerical': 0.010419078490391294, 'mean_word_length': 3.435318275154004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, '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': '3184166', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1452, 'n_words': 298}