<commit_before>var models = require('../db/models.js');
var util = require('../core/utilities.js');
module.exports = function(router) {

  //input: {x: float, y: float, z: float, message: string, userToken: string}
  router.post('/messages', function(req, res) {
    util.log('RECEIVED a POST request', req.body);
    models.createMessage(req.body)
      .then(function(msg) {
        util.log('SENT success code to user', msg);
        res.sendStatus(201);
      }, function(err) {
        util.log('SENT error code to user', err);
        res.sendStatus(400);
      });
  });

  //input: {userToken: string}
  router.post('/usertoken', function (req, res) {
    var token = req.body.userToken;
    util.log('RECEIVED a new user token from user', token);
    models.createUser(token)
      .then(function(user) {
        util.log('SENT success code to user', user);
        res.sendStatus(201);
      }, function(err) {
        util.log('SENT error code to user', err);
        res.sendStatus(400);
      });
  });

  //input: {messageId: string, userToken: string}
  router.post('/upvote', function (req, res) {
    util.log('RECEIVED upvote request', req.body);
    models.createVote(+req.body.messageId, req.body.userToken);
    res.sendStatus(200);
  });

  //input: {messageId: string, message: string}
  router.post('/comment', function (req, res) {
    util.log("RECEIVED comment", req.body);
    models.createComment(req.body);
    res.sendStatus(200);
  });
};<commit_msg>Refactor duplicae POST requests code<commit_after>var models = require('../db/models.js');
var util = require('../core/utilities.js');
module.exports = function(router) {

  var reject = function(req, res, err) {
    util.log('RECEIVED a POST request', req.body);
    util.log('SENT error code to user', err);
    res.sendStatus(400);
  };

  var resolve = function(req, res, success) {
    util.log('RECEIVED a POST request', req.body);
    util.log('SENT success code to user', msg);
    res.sendStatus(201);
  };

  //input: {x: float, y: float, z: float, message: string, userToken: string}
  router.post('/messages', function(req, res) {
    models.createMessage(req.body).then(
        resolve.bind(this, req, res),
        reject.bind(this, req, res)
        );
  });

  //input: {userToken: string}
  router.post('/usertoken', function (req, res) {
    var token = req.body.userToken;
    models.createUser(token).then(
        resolve.bind(this, req, res),
        reject.bind(this, req, res)
        );
  });

  //input: {messageId: string, userToken: string}
  router.post('/upvote', function (req, res) {
    models.createVote(req.body.messageId, req.body.userToken).then(
        resolve.bind(this, req, res),
        reject.bind(this, req, res)
        );
  });

  //input: {messageId: string, message: string}
  router.post('/comment', function (req, res) {
    models.createComment(req.body).then(
        resolve.bind(this, req, res),
        reject.bind(this, req, res)
        );
  });
};
{'fraction_non_alphanumeric': 0.17129474740715958, 'fraction_numerical': 0.008029441284710606, 'mean_word_length': 3.051490514905149, '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': '5601147', 'n_tokens_mistral': 1010, 'n_tokens_neox': 977, 'n_words': 226}