<commit_before>/**
 * Using Rails-like standard naming convention for endpoints.
 * GET     /customers              ->  index
 * POST    /customers              ->  create
 * GET     /customers/:id          ->  show
 * PUT     /customers/:id          ->  update
 * DELETE  /customers/:id          ->  destroy
 */

'use strict';
var _ = require('lodash');

// Get list of customers
exports.index = function(req, res, next) {
  req.connection.model('Customer').find()
    .then(res.json.bind(res))
    .catch(next);
};


exports.role = function(req, res) {
  req.connection.model('Customer').find({'role.id': req.params.id}, function (err, customers) {
    if(err) { return handleError(res, err); }
    if(!customers) { return res.send(404); }
    return res.json(customers);
  });
};

// Get a single customer
exports.show = function(req, res) {
  req.connection.model('Customer').findById(req.params.id, function (err, customer) {
    if(err) { return handleError(res, err); }
    if(!customer) { return res.send(404); }
    return res.json(customer);
  });
};

// Creates a new customer in the DB.
exports.create = function(req, res) {
  req.connection.model('Customer').create(req.body, function(err, customer) {
    if(err) { return handleError(res, err); }
    return res.json(201, customer);
  });
};

// Updates an existing customer in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }<commit_msg>Rewrite user show to use promises<commit_after>/**
 * Using Rails-like standard naming convention for endpoints.
 * GET     /customers              ->  index
 * POST    /customers              ->  create
 * GET     /customers/:id          ->  show
 * PUT     /customers/:id          ->  update
 * DELETE  /customers/:id          ->  destroy
 */

'use strict';
var _ = require('lodash');
var errors = require('../../components/errors');
var mongoose = require('mongoose');

// Get list of customers
exports.index = function(req, res, next) {
  req.connection.model('Customer').find()
    .then(res.json.bind(res))
    .catch(next);
};


exports.role = function(req, res) {
  req.connection.model('Customer').find({'role.id': req.params.id}, function (err, customers) {
    if(err) { return handleError(res, err); }
    if(!customers) { return res.send(404); }
    return res.json(customers);
  });
};

// Get a single customer
exports.show = function(req, res, next) {
  req.connection.model('Customer').findById(req.params.id)
    .then(customer => {
      if (customer === null) {
        throw new errors.NotFoundError('user');
      }
      return res.json(customer);
    })
    .catch(mongoose.Error.CastError, () => {
        throw new errors.NotFoundError('user');
    })
    .catch(next);
};

// Creates a new customer in the DB.
exports.create = function(req, res) {
  req.connection.model('Customer').create(req.body, function(err, customer) {
    if(err) { return handleError(res, err); }
    return res.json(201, customer);
  });
};

// Updates an existing customer in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
{'fraction_non_alphanumeric': 0.16516709511568123, 'fraction_numerical': 0.004820051413881748, 'mean_word_length': 2.8815461346633415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2875243', 'n_tokens_mistral': 997, 'n_tokens_neox': 981, 'n_words': 230}