Source: src/index.js

"use strict";

var Sequelize = require("sequelize");


/**
 * Details for the connection
 * @type {Sequelize}
 */
var sequelize = new Sequelize("aviation", "aviator", null, {
  host: "localhost",
  dialect: "postgres",
  port: 5432,

  pool: {
    max: 5,
    min: 0,
    idle: 1 + 1000
  },
  logging: false
});

/**
 * Importing modules.
 */

var Airport = sequelize.import("../modules/airport.js");


/**
 * Airport Methods.
 */

function getAirportByIcao(icao, callback) {
  Airport.findOne({
    where: {
      icao: icao
    }
  }).then(function (airport) {
    // console.log(airport.getName);
    callback(airport.dataValues);
  });
}

function getAirportName(value, type, callback) {
  Airport.findOne({
    where: {
      [type]: value
    }
  }).then(function (data) {
    callback(data.getName);
  });
}

function getAirportCoordinates(value, type, callback) {
  Airport.findOne({
    where: {
      [type]: value
    }
  }).then(function (data) {
    callback(data.getLocation);
  });
}


// bermi: previous methods refactored into this one so we can use it for all 
// the methods we want.


/**
 * Retrieves the data information from the database.
 * @param  {Object}   options
 * @param  {String} options.type [ icao | iata | name | nickname ]
 * @param  {String} options.value the value according to the type specified.
 * @param  {String} options.method [ Name | Location | Latitude | Longitude ] 
 * 
 * @param  {Function} callback 
 * @return {string}            requested information.
 * 
 * @example
 * ```javascript
 * getAirportData({
 *      value: "ALC",
 *      type: "iata",
 *      method: "Latitude"
 *    }, function (data) {
 *      console.log(data);
 *      }
 * ```
 */
function getAirportData(options, callback) {
  var logging = options.verbose || false;

  if (options.verbose === true) {
    logging = console.log;//eslint-disable-line no-console
  }

  sequelize.sync({ logging: logging });
  Airport.findOne({
    where: {
      [options.type]: options.value
    }
  }).then(function (data) {
    callback(data["get" + options.method]);
  });
}

/**
 * Exporting methods.
 */

module.exports = {
  getAirportByIcao: getAirportByIcao,
  getAirportName: getAirportName,
  getAirportCoordinates: getAirportCoordinates,
  getAirportData: getAirportData
};