<commit_before>/**
 * Copyright reelyActive 2015
 * We believe in an open Internet of Things
 */


var nedb = require('nedb');

var PLACE_DB = "hlc-places.db";
var TEST_NAME = "test";
var TEST_IDS = [ "001bc50940800000", "001bc50940810000" ];


/**
 * PlaceManager Class
 * Manages the database of places
 * @constructor
 */
function PlaceManager() {
  var self = this;
  this.db = new nedb( { filename: PLACE_DB, autoload: true } );
};


/**
 * Create a new place in the database
 * @param {String} name Name of the place to add.
 * @param {String} ids Comma-separated list of ids associated with the place.
 * @param {function} callback Function to call on completion.
 */
PlaceManager.prototype.create = function(name, ids, callback) {
  var self = this;

  if(!name) {
    callback("Place name is missing");
  }
  else if(!ids) {
    callback("Identifiers are missing");
  }
  else {
    var idArray = ids.split(',');
    var association = { _id: name, ids: idArray };
    this.db.insert(association, function(err, place) {
      var id;
      if(err && (err.errorType == 'uniqueViolated')) {
        err = "Place already exists";
      }
      if(place) {
        id = place._id;
        delete place._id;
      }
      callback(err, id, place);
    });
  }
}


/**
 * Replace a place in the database
 * @param {String} name Name of the place to update.
 * @param {String} ids Comma-separated list of ids associated with the place.
 * @param {function} callback Function to call on completion.
 */
PlaceManager.prototype.replace = function(name, ids, callback) {
  var self = this;

  if(!ids) {
    callback("Identifiers are missing");
  }
  else {
    var idArray = ids.split(',');
    var association = { ids: idArray };
    this.db.update({ _id: name }, { $set: association }, { upsert: true },
                   function(err, numReplaced, place) {
      if(err) { err = err.message; }
      if(place) {
        id = place._id;
        delete place._id;
      }
      else { place = association };
      callback(err, name, place);
    });
  }<commit_msg>POST & PUT at/ accept ids as array and comma-delimited string<commit_after>/**
 * Copyright reelyActive 2015
 * We believe in an open Internet of Things
 */


var nedb = require('nedb');

var PLACE_DB = "hlc-places.db";
var TEST_NAME = "test";
var TEST_IDS = [ "001bc50940800000", "001bc50940810000" ];


/**
 * PlaceManager Class
 * Manages the database of places
 * @constructor
 */
function PlaceManager() {
  var self = this;
  this.db = new nedb( { filename: PLACE_DB, autoload: true } );
};


/**
 * Create a new place in the database
 * @param {String} name Name of the place to add.
 * @param {String} ids Comma-separated list of ids associated with the place.
 * @param {function} callback Function to call on completion.
 */
PlaceManager.prototype.create = function(name, ids, callback) {
  var self = this;

  if(!name) {
    callback("Place name is missing");
  }
  else if(!ids) {
    callback("Identifiers are missing");
  }
  else {
    if(typeof ids === 'string') {
      ids = ids.split(',');
    }
    var association = { _id: name, ids: ids };
    this.db.insert(association, function(err, place) {
      var id;
      if(err && (err.errorType == 'uniqueViolated')) {
        err = "Place already exists";
      }
      if(place) {
        id = place._id;
        delete place._id;
      }
      callback(err, id, place);
    });
  }
}


/**
 * Replace a place in the database
 * @param {String} name Name of the place to update.
 * @param {String} ids Comma-separated list of ids associated with the place.
 * @param {function} callback Function to call on completion.
 */
PlaceManager.prototype.replace = function(name, ids, callback) {
  var self = this;

  if(!ids) {
    callback("Identifiers are missing");
  }
  else {
    if(typeof ids === 'string') {
      ids = ids.split(',');
    }
    var association = { ids: ids };
    this.db.update({ _id: name }, { $set: association }, { upsert: true },
                   function(err, numReplaced, place) {
      if(err) { err = err.message; }
      if(place) {
        id = place._id;
        delete place._id;
      }
      else { place = association };
      callback(err, name, place);
    });
  }
{'fraction_non_alphanumeric': 0.13709296838131194, 'fraction_numerical': 0.015101462954223691, 'mean_word_length': 2.728232189973615, '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': '4696730', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1310, 'n_words': 421}