<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose');
var errorHandler = require('./errors.server.controller');
var Vocablist = mongoose.model('Vocablist');
var _ = require('lodash');
var Vocab = mongoose.model('Vocab');
var updateVocabHelper = require('../helpers/updateVocab');

function saveVocablistPromise(vocablist) {
  if (vocablist.id) {
    return new Promise(function(resolve, error) {
      vocablist.save(function(err) {
        if (err) {
          error(err);
        } else {
          resolve(vocablist);
        }
      });
    });
  } else {
    return new Promise(function(resolve) {
      resolve(vocablist);
    });
  }
}

function populateVocablist(vocablist) {
  return new Promise(function(resolve, error) {
    vocablist.populate('vocab', function(err) {
      if (err) {
        error(err);
      } else {
        resolve(vocablist);
      }
    });
  });
}

function removeNullFromArray(array) {
  for (var i = 0; i < array.length; i++) {
    if (!array[i]) {
      array.splice(i, 1);
    }
  }
}

/**
 * Create a Vocablist
 */
exports.create = function(req, res) {
  var vocabs = req.body.vocab;
  updateVocabHelper.createVocabs(req.user, vocabs).
    then(function(vocabDocs) {
      var vocablist = req.body;
      vocablist.vocab = vocabDocs;
      vocablist.user = req.user;
      return Vocablist.create(vocablist);
    }).
    then(function(vocablist) {
      res.jsonp(vocablist);
      return;
    }).
    catch(function(err) {
      errorHandler.sendDatabaseErrorResponse(res, err);
    });
};

/**
 * Show the current Vocablist
 */
exports.read = function(req, res) {
  req.vocablist.populate('vocab', function(err) {
    if (err) {
      return res.status(400).send(new Error('Failed to load Vocablist ' + req.vocablist._id));
    } else {
      res.jsonp(req.vocablist);
    }
  });
};

/**
 * Update a Vocablist
 */
exports.update = function(req, res) {
  var vocablist = req.vocablist;
  vocablist = _.extend(vocablist , req.body);
  updateVocabHelper.findAndUpdateVocabs(req.body.vocab).
    then(function(vocabs) {
      removeNullFromArray(vocabs);
      vocablist.vocab = vocabs;
      return saveVocablistPromise(vocablist);
    }).
    then(populateVocablist).
    then(function(vocablist) {
      res.jsonp(vocablist);
    }).
    catch(
      function(err) {
        errorHandler.sendDatabaseErrorResponse(res, err);
      }
    );
};

function deleteVocabs(vocablist) {
  var ids = [];
  for (var i = 0; i < vocablist.vocab.length; i++) {
    ids.push(vocablist.vocab[i]);
  }
  return Vocab.remove({_id: {$in: ids}});
}

/**
 * Delete an Vocablist
 */
exports.delete = function(req, res) {
  var vocablist = req.vocablist;
  deleteVocabs(vocablist).then(function() {
    return vocablist.remove();
  }).then(function() {
    res.jsonp(vocablist);
  }).catch(function(err) {
    errorHandler.sendDatabaseErrorResponse(res, err);
  });
};

function getAllVocablists(user, params) {
  var query = user ? {user: user._id} : {};
  var t = Vocablist.find(query);
  if (params.query) {
    // var r = {$regex: new RegExp(query, 'i')};
    var r = new RegExp(params.query, 'i');
    var conds = [
      {name: r},
      {category: r}
    ];
    if (!isNaN(params.query)) {
      conds.push({chapter: query});
    }
    t.and([{$or: conds}]);
  }
  return t.sort('-created').populate('vocab').exec();
}

/**
 * List of Vocablists
 */
exports.list = function(req, res) {
  var query = req.user ? {user: req.user._id} : {};
  getAllVocablists(req.user, req.query)
    .then(function(vocablists) {
      res.jsonp(vocablists);
    })
    .catch(function(err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    });
};

function stripVocab(vocab) {
  return {
    word: vocab.word,
    translation: vocab.translation,
    timesTested: vocab.timesTested,
    timesCorrect: vocab.timesCorrect,
    lastTested: vocab.lastTested
  };
}

function stripVocablist(vocablist) {
  for (var i = 0; i < vocablist.vocab.length; i++) {
    vocablist.vocab[i] = stripVocab(vocablist.vocab[i]);
  }
  return {
    vocab: vocablist.vocab,
    name: vocablist.name,<commit_msg>Adds param handling for limiting results<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose');
var errorHandler = require('./errors.server.controller');
var Vocablist = mongoose.model('Vocablist');
var _ = require('lodash');
var Vocab = mongoose.model('Vocab');
var updateVocabHelper = require('../helpers/updateVocab');

function saveVocablistPromise(vocablist) {
  if (vocablist.id) {
    return new Promise(function(resolve, error) {
      vocablist.save(function(err) {
        if (err) {
          error(err);
        } else {
          resolve(vocablist);
        }
      });
    });
  } else {
    return new Promise(function(resolve) {
      resolve(vocablist);
    });
  }
}

function populateVocablist(vocablist) {
  return new Promise(function(resolve, error) {
    vocablist.populate('vocab', function(err) {
      if (err) {
        error(err);
      } else {
        resolve(vocablist);
      }
    });
  });
}

function removeNullFromArray(array) {
  for (var i = 0; i < array.length; i++) {
    if (!array[i]) {
      array.splice(i, 1);
    }
  }
}

/**
 * Create a Vocablist
 */
exports.create = function(req, res) {
  var vocabs = req.body.vocab;
  updateVocabHelper.createVocabs(req.user, vocabs).
    then(function(vocabDocs) {
      var vocablist = req.body;
      vocablist.vocab = vocabDocs;
      vocablist.user = req.user;
      return Vocablist.create(vocablist);
    }).
    then(function(vocablist) {
      res.jsonp(vocablist);
      return;
    }).
    catch(function(err) {
      errorHandler.sendDatabaseErrorResponse(res, err);
    });
};

/**
 * Show the current Vocablist
 */
exports.read = function(req, res) {
  req.vocablist.populate('vocab', function(err) {
    if (err) {
      return res.status(400).send(new Error('Failed to load Vocablist ' + req.vocablist._id));
    } else {
      res.jsonp(req.vocablist);
    }
  });
};

/**
 * Update a Vocablist
 */
exports.update = function(req, res) {
  var vocablist = req.vocablist;
  vocablist = _.extend(vocablist , req.body);
  updateVocabHelper.findAndUpdateVocabs(req.body.vocab).
    then(function(vocabs) {
      removeNullFromArray(vocabs);
      vocablist.vocab = vocabs;
      return saveVocablistPromise(vocablist);
    }).
    then(populateVocablist).
    then(function(vocablist) {
      res.jsonp(vocablist);
    }).
    catch(
      function(err) {
        errorHandler.sendDatabaseErrorResponse(res, err);
      }
    );
};

function deleteVocabs(vocablist) {
  var ids = [];
  for (var i = 0; i < vocablist.vocab.length; i++) {
    ids.push(vocablist.vocab[i]);
  }
  return Vocab.remove({_id: {$in: ids}});
}

/**
 * Delete an Vocablist
 */
exports.delete = function(req, res) {
  var vocablist = req.vocablist;
  deleteVocabs(vocablist).then(function() {
    return vocablist.remove();
  }).then(function() {
    res.jsonp(vocablist);
  }).catch(function(err) {
    errorHandler.sendDatabaseErrorResponse(res, err);
  });
};

function getAllVocablists(user, params) {
  var query = user ? {user: user._id} : {};
  var t = Vocablist.find(query);
  if (params.query) {
    // var r = {$regex: new RegExp(query, 'i')};
    var r = new RegExp(params.query, 'i');
    var conds = [
      {name: r},
      {category: r}
    ];
    if (!isNaN(params.query)) {
      conds.push({chapter: query});
    }
    t.and([{$or: conds}]);
  }
  if (params.limit) {
    t.limit(params.limit);
  }
  if (params.startVal) {
    t.lt('created', params.startVal);
  }
  return t.sort('-created').populate('vocab').exec();
}

/**
 * List of Vocablists
 */
exports.list = function(req, res) {
  getAllVocablists(req.user, req.query)
    .then(function(vocablists) {
      res.jsonp(vocablists);
    })
    .catch(function(err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    });
};

function stripVocab(vocab) {
  return {
    word: vocab.word,
    translation: vocab.translation,
    timesTested: vocab.timesTested,
    timesCorrect: vocab.timesCorrect,
    lastTested: vocab.lastTested
  };
}

function stripVocablist(vocablist) {
  for (var i = 0; i < vocablist.vocab.length; i++) {
    vocablist.vocab[i] = stripVocab(vocablist.vocab[i]);
  }
  return {
    vocab: vocablist.vocab,
    name: vocablist.name,
{'fraction_non_alphanumeric': 0.1585150265173836, 'fraction_numerical': 0.002357100766057749, 'mean_word_length': 2.982167996245894, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3963', 'n_tokens_mistral': 3163, 'n_tokens_neox': 3164, 'n_words': 524}