<commit_before>var express = require('express');
var assign = require('lodash.assign');

function ensureAPIAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {return next();}
  sendAPIError(res, 'authentication required', 401);
}

function sendAPISuccess(res, data) {
  res.json({data: data});
}

function sendAPIError(res, error, optErrorCode, optDetails) {
  res.status(optErrorCode || 400);
  res.json(assign({}, {error: error, details: optDetails}));
}

function APIRoutes(model) {
  var router = express.Router();

  router.post('/exp/:expId/events', ensureAPIAuthenticated, this.postEvents.bind(this));

  this.router = router;
  this.model = model;
}

APIRoutes.prototype = {
  postEvents: function(req, res) {
    // TODO: validate expId to make sure that it's a real experiment that's 
    // been registered before we do anything else.

    this.model.putExpEvents(
      req.params.expId, 
      req.user.id, 
      req.body.events
    )
      .then(function(numPut) {sendAPISuccess(res, numPut)})
      .catch(function(err) {sendAPIError(res, 500, err)});
  }
};

module.exports = APIRoutes;<commit_msg>Add exp id validation to apiroutes.<commit_after>var express = require('express');
var assign = require('lodash.assign');

function ensureAPIAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {return next();}
  sendAPIError(res, 'authentication required', 401);
}

function sendAPISuccess(res, data) {
  res.json({data: data});
}

function sendAPIError(res, error, optErrorCode, optDetails) {
  res.status(optErrorCode || 400);
  res.json(assign({}, {error: error, details: optDetails}));
}

function APIRoutes(model) {
  var router = express.Router();

  router.post('/exp/:expId/events', ensureAPIAuthenticated, this.postEvents.bind(this));

  this.router = router;
  this.model = model;
}

APIRoutes.prototype = {
  postEvents: function(req, res) {
    this.model.haveExpById(req.params.expId).then(function(haveExp) {
      if (!haveExp) {
        sendAPIError(res, 400, 'The experiment does not exist.');
        return;
      }

      this.model.putExpEvents(req.params.expId, req.body.events).then(
        function(numPut) {sendAPISuccess(res, numPut)},
        function(err) {sendAPIError(res, 500, err)}
      );
    });   
  }
};

module.exports = APIRoutes;
{'fraction_non_alphanumeric': 0.1501736111111111, 'fraction_numerical': 0.009114583333333334, 'mean_word_length': 4.156599552572707, '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': '7178524', 'n_tokens_mistral': 784, 'n_tokens_neox': 765, 'n_words': 160}