2014-11-14 15:06:53 +01:00
|
|
|
var eventStore = require('./eventStore');
|
|
|
|
var eventType = require('./eventType');
|
|
|
|
var logger = require('./logger');
|
|
|
|
var knex = require('./dbPool');
|
|
|
|
var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
|
2014-10-23 09:50:23 +02:00
|
|
|
|
|
|
|
eventStore.on(eventType.featureCreated, function (event) {
|
2014-11-14 15:06:53 +01:00
|
|
|
knex('features')
|
|
|
|
.insert(eventToRow(event))
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error('Could not insert feature, error was: ', err);
|
2014-10-23 13:38:02 +02:00
|
|
|
});
|
2014-11-14 15:06:53 +01:00
|
|
|
});
|
2014-10-23 09:50:23 +02:00
|
|
|
|
2014-10-23 16:00:51 +02:00
|
|
|
eventStore.on(eventType.featureUpdated, function (event) {
|
2014-11-14 15:06:53 +01:00
|
|
|
knex('features')
|
|
|
|
.where({name: event.data.name})
|
|
|
|
.update(eventToRow(event))
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error('Could not update feature, error was: ', err);
|
|
|
|
});
|
|
|
|
});
|
2014-10-23 16:00:51 +02:00
|
|
|
|
2014-10-23 09:50:23 +02:00
|
|
|
function getFeatures() {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex
|
|
|
|
.select(FEATURE_COLUMNS)
|
|
|
|
.from('features')
|
2014-12-10 17:13:26 +01:00
|
|
|
.orderBy('name', 'asc')
|
2014-11-14 16:40:13 +01:00
|
|
|
.map(rowToFeature);
|
2014-10-23 13:51:37 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 15:17:04 +02:00
|
|
|
function getFeature(name) {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex
|
2014-11-14 16:58:05 +01:00
|
|
|
.first(FEATURE_COLUMNS)
|
2014-11-14 15:06:53 +01:00
|
|
|
.from('features')
|
|
|
|
.where({name: name})
|
2014-11-14 16:58:05 +01:00
|
|
|
.then(rowToFeature);
|
2014-10-23 15:17:04 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:06:53 +01:00
|
|
|
function rowToFeature(row) {
|
2014-11-14 16:58:05 +01:00
|
|
|
if (!row) {
|
|
|
|
throw new Error('invalid row');
|
|
|
|
}
|
|
|
|
|
2014-10-23 13:51:37 +02:00
|
|
|
return {
|
|
|
|
name: row.name,
|
2014-11-10 16:21:22 +01:00
|
|
|
description: row.description,
|
2014-10-23 13:51:37 +02:00
|
|
|
enabled: row.enabled > 0,
|
2014-11-14 15:06:53 +01:00
|
|
|
strategy: row.strategy_name, // jshint ignore: line
|
2014-10-23 13:51:37 +02:00
|
|
|
parameters: row.parameters
|
|
|
|
};
|
2014-10-23 09:50:23 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:06:53 +01:00
|
|
|
function eventToRow(event) {
|
|
|
|
return {
|
|
|
|
name: event.data.name,
|
|
|
|
description: event.data.description,
|
|
|
|
enabled: event.data.enabled ? 1 : 0,
|
|
|
|
strategy_name: event.data.strategy, // jshint ignore: line
|
|
|
|
parameters: event.data.parameters
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:50:23 +02:00
|
|
|
module.exports = {
|
2014-10-23 15:17:04 +02:00
|
|
|
getFeatures: getFeatures,
|
|
|
|
getFeature: getFeature
|
2014-10-23 09:50:23 +02:00
|
|
|
};
|
|
|
|
|