'use strict';

var SQliteAdapter = require('services/sqlite_adapter');

var Container = function() {
  SQliteAdapter.call(this, 'containers');
  this.schemaAttrs = ['id', 'container_name'];
};

Container.prototype = Object.create(SQliteAdapter.prototype);

Container.prototype.find = function(attrs) {
  attrs = attrs || {};
  if(this.validateAttrs.call(this, attrs)) {
    var selection = this.prepareSelection(attrs);
    return this.execute('SELECT * FROM ' + this.tableName + ' ' + selection).then(function(container) {
      return container;
    }).catch(function(err) {
      console.log(err.stack);
    });
  }
};

Container.prototype.findAll = function() {
  return this.execute('SELECT * FROM ' + this.tableName).then(function(containers) {
    return containers;
  }).catch(function(err) {
    console.log(err.stack);
  });
};

var ContainerFactory = (function() {
  var instance;
  function createInstance() {
    var container = new Container();
    return container;
  }

  return {
    getInstance: function () {
      if (!instance) {
        instance = createInstance();
      }
      return instance;
    }
  };
})();

module.exports = ContainerFactory;

'use strict';

var SQliteAdapter = require('services/sqlite_adapter');

var Container = function() {
  SQliteAdapter.call(this, 'containers');
  this.schemaAttrs = ['id', 'container_name'];
};

Container.prototype = Object.create(SQliteAdapter.prototype);

Container.prototype.find = function(attrs) {
  attrs = attrs || {};
  if(this.validateAttrs.call(this, attrs)) {
    var selection = this.prepareSelection(attrs);
    return this.execute('SELECT * FROM ' + this.tableName + ' ' + selection).then(function(container) {
      return container;
    }).catch(function(err) {
      console.log(err.stack);
    });
  }
};

Container.prototype.findAll = function() {
  return this.execute('SELECT * FROM ' + this.tableName).then(function(containers) {
    return containers || [];
  }).catch(function(err) {
    console.log(err.stack);
  });
};

var ContainerFactory = (function() {
  var instance;
  function createInstance() {
    var container = new Container();
    return container;
  }

  return {
    getInstance: function () {
      if (!instance) {
        instance = createInstance();
      }
      return instance;
    }
  };
})();

module.exports = ContainerFactory;

Return empty array when findAll returns undefined from DB
Return empty array when findAll returns undefined from DB
