<commit_before>"use strict";

var util        = require('util'),
    express     = require('express'),
    collections = require('./collections'),
    validate    = require('./validate'),
    _           = require('underscore');

var app = module.exports = express();

app.use(express.bodyParser());

app.get('/', function (req, res) {
  res.jsonp({foo: 'bar'});
});


/*
  Check that the collection is in the allowed list.
*/
app.param('collection', function (req, res, next, collection) {

  if (! collections[collection]) {
    res
      .status(404)
      .jsonp({
        error: "collection '" + collection + "' not found"
      });
  } else {
    next();
  }
});

app.get('/:collection', function (req, res) {
  res.send("FIXME");
});

app.post('/:collection', function (req, res, next) {

  var collectionName = req.params.collection;
  var body = req.body;

  // If there is no id create one
  if (!body.id) {
    body.id = 'FIXME - replace with mongo object id';
  }

  validate(collectionName, body, function (err, errors) {

    if (err) { return next(err); }

    if (errors.length === 0) {
      // insert validation and writing to database here
      var id = "0123456789abcdef01234567";

      res
        .status(201)
        .location([collectionName, id].join('/'))
        .send();
    } else {

      var details = _.map(errors, function (error) {
        return util.format(
          "Error '%s' with '%s'.",
          error.message,
          error.schemaUri
        );
      });
      res
        .status(400)
        .send({errors: details});

    }
  });
});

<commit_msg>Extract body validation to reusable function<commit_after>"use strict";

var util        = require('util'),
    express     = require('express'),
    collections = require('./collections'),
    validate    = require('./validate'),
    _           = require('underscore');

var app = module.exports = express();

app.use(express.bodyParser());

app.get('/', function (req, res) {
  res.jsonp({foo: 'bar'});
});


/*
  Check that the collection is in the allowed list.
*/
app.param('collection', function (req, res, next, collection) {

  if (! collections[collection]) {
    res
      .status(404)
      .jsonp({
        error: "collection '" + collection + "' not found"
      });
  } else {
    next();
  }
});

app.get('/:collection', function (req, res) {
  res.send("FIXME");
});


function validateBody (req, res, next) {

  var collectionName = req.params.collection;
  var body = req.body;
  
  // If there is no id create one
  if (!body.id) {
    //FIXME - replace with mongo object id
    body.id = '123456789012345678901234';
  }

  validate(collectionName, body, function (err, errors) {

    if (err) {
      return next(err);
    } else if (errors.length === 0) {
      return next(null);
    } else {
    
      var details = _.map(errors, function (error) {
        return util.format(
          "Error '%s' with '%s'.",
          error.message,
          error.schemaUri
        );
      });
      
      res
        .status(400)
        .send({errors: details});
    }
  });
}


app.post('/:collection', validateBody, function (req, res) {

  var collectionName = req.params.collection;
  var body = req.body;
  var id   = body.id;

  // FIXME - save the document here

  res
    .status(201)
    .location([collectionName, id].join('/'))
    .send();
});


{'fraction_non_alphanumeric': 0.16483842276904834, 'fraction_numerical': 0.018381262970649275, 'mean_word_length': 2.3505461767626614, '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': '2579535', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1034, 'n_words': 272}