<commit_before>var tty = require('tty.js');
var Server = tty.Server;
var express = tty.express;

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/maintainer');

Server.prototype.initMiddleware = function() {
  this.use(express.bodyParser());
  this.use(express.static(__dirname + '/assets'));
  this.use(express.static(__dirname + '/public'));
};

Server.prototype.initRoutes = function() {
};

var app = Server({
  shell: 'bash',
  port: 3000,
  runScriptOnStart: runScriptOnStart
});

var Job = require('./models/job');

function runScriptOnStart(term, bundle) {
  if (!bundle || !bundle.job) return;
  Job.findOne({ _id: bundle.job }, 'published.content', function(error, job) {
    var script = job.published.content.trim() + '\n';
    term.write(script);
  });
}

app.get('/jobs/:job_id?/:revision_id?', function(req, res, next) {
  var job_id = req.params.job_id;
  if (job_id) {
    var revision_id = req.params.revision_id;
    if (revision_id) {
      Job.findOne({ _id: job_id }, 'revisions').exec(function(error, job) {
        if (error) return next(error);
        for (var i = 0; i < job.revisions.length; i++) {
          if (job.revisions[i]._id == revision_id) {
            return res.send(job.revisions[i]);
          }
        }
        return next();
      });
    } else {
      Job.findOne({ _id: job_id }, '-revisions.content').exec(function(error, job) {
        if (error) return next(error);
        res.send(job);
      });
    }
  } else {
    Job.find({}, '_id published revision_count created_at updated_at').sort('created_at')
      .exec(function(error, jobs) {
      if (error) return next(error);
      res.send(jobs);
    });
  }
});

app.post('/jobs', function(req, res, next) {
  var title = req.body.title;
  var content = req.body.content;
  var job = {
    title: title,
    content: content,
    created_at: new Date()
  };
  var new_job = new Job({
    published: job,
    revisions: [ job ],
    revision_count: 1,
    created_at: new Date(),
    updated_at: new Date()
  });
  new_job.save(function(error) {
    if (error) return next(error);
    res.send(new_job);
  })
});

app.put('/jobs/:job_id', function(req, res, next) {
  var title = req.body.title;
  var content = req.body.content;
  Job.findOne({ _id: req.params.job_id }).exec(function(error, job) {
    if (error) return next(error);
    var updated_job = {
      title: title,
      content: content,
      created_at: new Date()
    }
    job.published = updated_job;
    job.revisions.push(updated_job);
    job.revision_count += 1;
    job.updated_at = new Date();
    job.save(function(error) {
      if (error) return next(error);
      res.send({ status: 'OK' });
    });
  });
});

app.delete('/jobs/:job_id', function(req, res, next) {
  Job.findOneAndRemove({ _id: req.params.job_id }).exec(function(error) {
    if (error) return next(error);
    res.send({ status: 'OK' });
  });
});

app.use(function(err, req, res, next) {
  res.status(err.status || 400);
  res.send({ error: err.toString() });
});

app.use(function(req, res) {
  res.status(404);
  res.send({ error: 'Page Not Found.' });
});

app.listen();
<commit_msg>use better code;<commit_after>var tty = require('tty.js');
var Server = tty.Server;
var express = tty.express;

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/maintainer');

Server.prototype.initMiddleware = function() {
  this.use(express.bodyParser());
  this.use(express.static(__dirname + '/assets'));
  this.use(express.static(__dirname + '/public'));
};

Server.prototype.initRoutes = function() {
};

var app = Server({
  shell: 'bash',
  port: 3000,
  runScriptOnStart: runScriptOnStart
});

var Job = require('./models/job');

function runScriptOnStart(term, bundle) {
  if (!bundle || !bundle.job) return;
  Job.findOne({ _id: bundle.job }, 'published.content', function(error, job) {
    var script = job.published.content.trim() + '\n';
    term.write(script);
  });
}

app.get('/jobs/:job_id?/:revision_id?', function(req, res, next) {
  var job_id = req.params.job_id;
  var revision_id = req.params.revision_id;
  var query;
  if (job_id) {
    if (revision_id) {
      query = Job.findOne({ _id: job_id }, 'revisions').exec().then(function(content) {
        return content.revisions.id(revision_id)
      });
    } else {
      query = Job.findOne({ _id: job_id }, '-revisions.content').exec();
    }
  } else {
    query = Job.find({}, '-revisions').sort('created_at').exec();
  }
  query.then(function(content) {
    if (content === null) return next();
    res.send(content);
  }, function(error) {
    next(error);
  });
});

app.post('/jobs', function(req, res, next) {
  var title = req.body.title;
  var content = req.body.content;
  var job = {
    title: title,
    content: content,
    created_at: new Date()
  };
  var new_job = new Job({
    published: job,
    revisions: [ job ],
    revision_count: 1,
    created_at: new Date(),
    updated_at: new Date()
  });
  new_job.save(function(error) {
    if (error) return next(error);
    res.send(new_job);
  })
});

app.put('/jobs/:job_id', function(req, res, next) {
  var title = req.body.title;
  var content = req.body.content;
  Job.findOne({ _id: req.params.job_id }).exec(function(error, job) {
    if (error) return next(error);
    var updated_job = {
      title: title,
      content: content,
      created_at: new Date()
    }
    job.published = updated_job;
    job.revisions.push(updated_job);
    job.revision_count += 1;
    job.updated_at = new Date();
    job.save(function(error) {
      if (error) return next(error);
      res.send({ status: 'OK' });
    });
  });
});

app.delete('/jobs/:job_id', function(req, res, next) {
  Job.findOneAndRemove({ _id: req.params.job_id }).exec(function(error) {
    if (error) return next(error);
    res.send({ status: 'OK' });
  });
});

app.use(function(err, req, res, next) {
  res.status(err.status || 400);
  res.send({ error: err.toString() });
});

app.use(function(req, res) {
  res.status(404);
  res.send({ error: 'Page Not Found.' });
});

app.listen();

{'fraction_non_alphanumeric': 0.1813564875491481, 'fraction_numerical': 0.0040956749672346, 'mean_word_length': 3.2752100840336134, 'pattern_counts': {'":': 0, '<': 4, '<?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': '2660714', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2139, 'n_words': 446}