// Constructor
class locations {
constructor(logger) {
// always initialize all instance properties
this._logger = logger;
}
/**
* Returns list of all locations for given organization
* @param {integer} orgId organization id for which locations shall be returned
* @param {function} callback null or rows
*/
getListOfLocationsForOrganizationWithId(orgId, callback) {
this._logger.info('Reading locations for organization with id ' + orgId);
let query = "SELECT * FROM `locations` WHERE organizationId = ?";
global.coreDb.all(query, [
orgId
], (err, rows) => {
if (err) {
return null;
} else {
return callback(rows);
}
});
}
/**
* Returns list of all locations in core database
* @param {function} callback null or rows
*/
getListOfLocations(callback) {
this._logger.info('Reading all locations');
let query = "SELECT locations.*, " +
"organizations.name AS organizationname " +
"FROM `locations` " +
"INNER JOIN organizations ON locations.organizationid = organizations.id";
global.coreDb.all(query, [
], (err, rows) => {
if (err) {
return null;
} else {
return callback(rows);
}
});
}
/**
* Loads location from database
* @param {integer} id id of location to be loaded
* @param {function} callback null or row
*/
getLocationWithId(id, callback) {
let query = "SELECT * FROM `locations` WHERE id = ?";
global.coreDb.all(query, [
id
], (err, rows) => {
if (err) {
return callback(null);
} else {
return callback(rows);
}
});
}
/**
* Adds a new location to core database
* @param {req.body} formBody Form fields holding the form data for new location
* @param {function} callback true or false
*/
addLocation(formBody, callback) {
this._logger.info('adding new location');
let name = formBody.name;
let street = formBody.street;
let zip = formBody.zip;
let city = formBody.city;
let state = formBody.state;
let country = formBody.country;
let latitude = formBody.latitude;
let longitude = formBody.longitude;
let organizationid = formBody.organizationid;
let query = 'INSERT INTO `locations` (name, street, zip, city, country, state, latitude, longitude, organizationId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
global.coreDb.run(query,
[
name,
street,
zip,
city,
country,
state,
latitude,
longitude,
organizationid
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
callback(false);
} else {
callback(true);
}
});
}
/**
* updates an existing location in core database
* @param {integer} locationId Id of location object which shall be updated
* @param {req.body} formBody Form fields holding the new form data for the location to be updated
* @param {function} callback true or false
*/
updateLocation(locationId, formBody, callback) {
let name = formBody.name;
let street = formBody.street;
let zip = formBody.zip;
let city = formBody.city;
let state = formBody.state;
let country = formBody.country;
let latitude = formBody.latitude;
let longitude = formBody.longitude;
let organizationid = formBody.organizationid;
let query = "UPDATE `locations` SET `name` = ?, " +
"`street` = ?, " +
"`zip` = ?, " +
"`city` = ?, " +
"`state` = ?, " +
"`country` = ?, " +
"`latitude` = ?, " +
"`longitude` = ?, " +
"`organizationid` = ? " +
" WHERE `locations`.`id` = ?";
global.coreDb.run(query,
[
name,
street,
zip,
city,
state,
country,
latitude,
longitude,
organizationid,
locationId
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
callback(false);
} else {
callback(true);
}
});
}
/**
* Deletes a location object from database
* @param {integer} locationId id of location to be deleted from core database
* @param {function} callback true or false
*
* TBD: Check if any object relies on this object!
*/
deleteLocation(locationId, callback) {
let query = "DELETE FROM `locations` WHERE id = ?";
global.coreDb.run(query,
[
locationId
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
callback(false);
} else {
callback(true);
}
});
}
}
// export the class
module.exports = locations;