<commit_before>var mongoose = require('mongoose');
var CableType = mongoose.model('CableType');

var auth = require('../lib/auth');


module.exports = function(app) {
  app.get('/cabletypes', auth.ensureAuthenticated, function(req, res) {
    res.render('cabletype', {
      roles: req.session.roles
    });
  });

  app.get('/cabletypes/manage', auth.ensureAuthenticated, function(req, res) {
    if (req.session.roles.indexOf('admin') !== -1) {
      return res.render('cabletypemgmt');
    } else {
      return res.send(403, 'You are not authorized to access this resource');
    }
  });

  app.get('/cabletypes/json', auth.ensureAuthenticated, function(req, res) {
    CableType.find(function(err, docs) {
      if (err) {
        return res.send(500, err.message);
      }
      res.json(docs);
    });
  });

  app.post('/cabletypes', auth.ensureAuthenticated, function(req, res) {
    if (req.session.roles.length === 0 || req.session.roles.indexOf('manage') === -1) {
      return res.send(403, "You are not authorized to access this resource. ");
    }
    var newType = {
      name: req.body.name || 'updateme',
      characteristics: req.body.characteristics || '',
      diameter: req.diameter || '',
      service: req.service || '',
      voltage: req.voltage || '',
      insulation: req.insulation || '',
      jacket: req.jacket || '',
      raceway: req.raceway || '',
      tid: req.tid || '',
      model: req.model || '',
      comments: req.comments || '',
      spec: req.spec || ''
    };
    (new CableType(newType)).save(function(err, type) {
      if (err && err.code) {
        console.dir(err);
        // see test/duplicatedCableNumber.js for a test of this case
        if (err.code == 11000) {
          console.log(err.msg || err.errmsg);
          return res.send(400, 'please update the cable type named ' + newType.name);
        } else {
          console.error(err.msg || err.errmsg);
          return res.send(500, err.msg || err.errmsg);
        }
      }
      var url = req.protocol + '://' + req.get('host') + '/cabletypes/' + type._id;
      res.set('Location', url);
      return res.json(201, type);
    });
  });<commit_msg>update cable type err<commit_after>var mongoose = require('mongoose');
var CableType = mongoose.model('CableType');

var auth = require('../lib/auth');


module.exports = function(app) {
  app.get('/cabletypes', auth.ensureAuthenticated, function(req, res) {
    res.render('cabletype', {
      roles: req.session.roles
    });
  });

  app.get('/cabletypes/manage', auth.ensureAuthenticated, function(req, res) {
    if (req.session.roles.indexOf('admin') !== -1) {
      return res.render('cabletypemgmt');
    } else {
      return res.send(403, 'You are not authorized to access this resource');
    }
  });

  app.get('/cabletypes/json', auth.ensureAuthenticated, function(req, res) {
    CableType.find(function(err, docs) {
      if (err) {
        return res.send(500, err.message);
      }
      res.json(docs);
    });
  });

  app.post('/cabletypes', auth.ensureAuthenticated, function(req, res) {
    if (req.session.roles.length === 0 || req.session.roles.indexOf('manage') === -1) {
      return res.send(403, "You are not authorized to access this resource. ");
    }
    var newType = {
      name: req.body.name || 'updateme',
      characteristics: req.body.characteristics || '',
      diameter: req.diameter || '',
      service: req.service || '',
      voltage: req.voltage || '',
      insulation: req.insulation || '',
      jacket: req.jacket || '',
      raceway: req.raceway || '',
      tid: req.tid || '',
      model: req.model || '',
      comments: req.comments || '',
      spec: req.spec || ''
    };
    (new CableType(newType)).save(function(err, type) {
      if (err && err.code) {
        console.dir(err);
        if (err.code == 11000) {
          console.err(err.msg || err.err);
          return res.send(400, 'please update the cable type named ' + newType.name);
        } else {
          console.error(err.msg || err.err);
          return res.send(500, err.msg || err.err);
        }
      }
      var url = req.protocol + '://' + req.get('host') + '/cabletypes/' + type._id;
      res.set('Location', url);
      return res.json(201, type);
    });
  });
{'fraction_non_alphanumeric': 0.17820392890551917, 'fraction_numerical': 0.01216089803554724, 'mean_word_length': 2.5552784704904408, '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': '3175607', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1379, 'n_words': 281}