2014-11-17 22:39:04 +01:00
|
|
|
var eventStore = require('./eventStore');
|
|
|
|
var eventType = require('./eventType');
|
|
|
|
var logger = require('./logger');
|
|
|
|
var knex = require('./dbPool');
|
|
|
|
var STRATEGY_COLUMNS = ['name', 'description', 'parameters_template'];
|
|
|
|
|
|
|
|
eventStore.on(eventType.strategyCreated, function (event) {
|
|
|
|
knex('strategies')
|
|
|
|
.insert(eventToRow(event))
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error('Could not insert strategy, error was: ', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-08 20:56:22 +01:00
|
|
|
eventStore.on(eventType.strategyDeleted, function (event) {
|
|
|
|
knex('strategies')
|
|
|
|
.where('name', event.data.name)
|
|
|
|
.del()
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error('Could not delete strategy, error was: ', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-17 22:39:04 +01:00
|
|
|
function getStrategies() {
|
|
|
|
return knex
|
|
|
|
.select(STRATEGY_COLUMNS)
|
|
|
|
.from('strategies')
|
2014-11-29 11:51:57 +01:00
|
|
|
.orderBy('created_at', 'asc')
|
2014-11-17 22:39:04 +01:00
|
|
|
.map(rowToStrategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStrategy(name) {
|
|
|
|
return knex
|
|
|
|
.first(STRATEGY_COLUMNS)
|
|
|
|
.from('strategies')
|
|
|
|
.where({name: name})
|
|
|
|
.then(rowToStrategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rowToStrategy(row) {
|
|
|
|
if (!row) {
|
|
|
|
throw new Error('invalid row');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
description: row.description,
|
|
|
|
enabled: row.enabled > 0,
|
|
|
|
parametersTemplate: row.parameters_template // jshint ignore: line
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function eventToRow(event) {
|
|
|
|
return {
|
|
|
|
name: event.data.name,
|
|
|
|
description: event.data.description,
|
|
|
|
parameters_template: event.data.parametersTemplate // jshint ignore: line
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getStrategies: getStrategies,
|
|
|
|
getStrategy: getStrategy
|
|
|
|
};
|
|
|
|
|