<commit_before>var express = require('express');
var router = express.Router();
var jade = require('jade');
var database = require('../database');

database.connectDb(function(err) {
  if (err) throw err;
  var db = database.getDb();
  router.post('/:method', function(req, res, next) {
    if (req.params.method === 'edit') {
      db.collection('items').find({'_id':database.getID(req.body.id)}).toArray(function(err, result) {
        if (err) {
          throw err;
        } 
        else {
          //db.collection('items').update({'_id':database.getID(req.body.id)}, result[0]);
          res.send(result[0]);
        }
      });
    }
    else if (req.params.method === 'delete') {
      db.collection('items').deleteOne({'_id':database.getID(req.body.id)}, function(err) {
        if (err) throw err;
      });
    }
    else {
      var keys = Object.keys(req.body);
      for(var i = 0; i < keys.length; i++) {
        if (keys[i].substring(0,3) === 'sub') {
          var key = keys[i].split(','),
              field = req.body[keys[i]],
              sub = key[0].substring(3),
              main = key[1];
          if (typeof(req.body[main]) === 'undefined') {
            req.body[main] = {};
          }
          req.body[main][sub] = field;
          delete req.body[keys[i]];
        }
      }
      var extended = req.body.Extended
      req.body.Extended = extended === 'true' ? true : false;
      req.body['Image'] = req.body.Images[0]
      if (typeof(req.body.Images) !== 'object') {
        req.body['Image'] = req.body.Images
      }
      if (req.body.mode === 'new') {
        delete req.body.mode;
        db.collection('items').insert(req.body);
      }
      else {
        var modeID = req.body.mode
        delete req.body.mode;
        db.collection('items').update({'_id':database.getID(modeID)}, req.body);
      }
      //processField(req.body);
      //console.log(req.body)
      res.redirect('/works');
    }
  });

  router.get('/', function(req, res, next) {
    db.collection('dataTemplates').findOne(function(err, result) {
      var templates = result;
      db.collection('items').find({}).toArray(function(err, result) {
        var items = result;
        res.render('itemdata', { 
          itemKeys: Object.keys(items),
          items: items,
          template: templates["Project"],
          templateKeys: Object.keys(templates["Project"])
        });
      });
    });
  });
});

function processField(data) {
  var keys = Object.keys(data);
  var fields = [];
  for (var i = 0; i < keys.length; i++) {
    if (typeof(data[keys[i]]) === 'object') {
       fields.push(processField(data[keys[i]]));
    }
    else {
      fields.push(data[keys[i]]);
    }
  }
  return fields;
}

module.exports = router;<commit_msg>Removed leftover lines<commit_after>var express = require('express');
var router = express.Router();
var jade = require('jade');
var database = require('../database');

database.connectDb(function(err) {
  if (err) throw err;
  var db = database.getDb();
  router.post('/:method', function(req, res, next) {
    if (req.params.method === 'edit') {
      db.collection('items').find({'_id':database.getID(req.body.id)}).toArray(function(err, result) {
        if (err) {
          throw err;
        } 
        else {
          res.send(result[0]);
        }
      });
    }
    else if (req.params.method === 'delete') {
      db.collection('items').deleteOne({'_id':database.getID(req.body.id)}, function(err) {
        if (err) throw err;
      });
    }
    else {
      var keys = Object.keys(req.body);
      for(var i = 0; i < keys.length; i++) {
        if (keys[i].substring(0,3) === 'sub') {
          var key = keys[i].split(','),
              field = req.body[keys[i]],
              sub = key[0].substring(3),
              main = key[1];
          if (typeof(req.body[main]) === 'undefined') {
            req.body[main] = {};
          }
          req.body[main][sub] = field;
          delete req.body[keys[i]];
        }
      }
      var extended = req.body.Extended
      req.body.Extended = extended === 'true' ? true : false;
      req.body['Image'] = req.body.Images[0]
      if (typeof(req.body.Images) !== 'object') {
        req.body['Image'] = req.body.Images
      }
      if (req.body.mode === 'new') {
        delete req.body.mode;
        db.collection('items').insert(req.body);
      }
      else {
        var modeID = req.body.mode
        delete req.body.mode;
        db.collection('items').update({'_id':database.getID(modeID)}, req.body);
      }
      res.redirect('/works');
    }
  });

  router.get('/', function(req, res, next) {
    db.collection('dataTemplates').findOne(function(err, result) {
      var templates = result;
      db.collection('items').find({}).toArray(function(err, result) {
        var items = result;
        res.render('itemdata', { 
          itemKeys: Object.keys(items),
          items: items,
          template: templates["Project"],
          templateKeys: Object.keys(templates["Project"])
        });
      });
    });
  });
});

module.exports = router;
{'fraction_non_alphanumeric': 0.188050930460333, 'fraction_numerical': 0.003525954946131244, 'mean_word_length': 2.2052730696798495, 'pattern_counts': {'":': 0, '<': 6, '<?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': '2926418', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1610, 'n_words': 287}