2014-11-25 15:28:08 +01:00
|
|
|
var eventStore = require('./eventStore');
|
|
|
|
var eventType = require('./eventType');
|
2014-11-17 22:39:04 +01:00
|
|
|
var strategyDb = require('./strategyDb');
|
|
|
|
|
2014-10-30 21:10:48 +01:00
|
|
|
module.exports = function (app) {
|
|
|
|
|
|
|
|
app.get('/strategies', function (req, res) {
|
2014-11-17 22:39:04 +01:00
|
|
|
strategyDb.getStrategies().then(function (strategies) {
|
|
|
|
res.json({strategies: strategies});
|
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/strategies/:name', function (req, res) {
|
2014-11-25 15:28:08 +01:00
|
|
|
strategyDb.getStrategy(req.params.name)
|
|
|
|
.then(function (strategy) { res.json(strategy); })
|
|
|
|
.catch(function () { res.json(404, {error: 'Could not find strategy'}); });
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2014-12-08 20:56:22 +01:00
|
|
|
app.delete('/strategies/:name', function (req, res) {
|
|
|
|
eventStore.create({
|
|
|
|
type: eventType.strategyDeleted,
|
|
|
|
createdBy: req.connection.remoteAddress,
|
|
|
|
data: {
|
|
|
|
name: req.params.name
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () { res.status(200).end(); })
|
|
|
|
.catch(function () { res.status(500).end(); });
|
|
|
|
});
|
|
|
|
|
2014-10-30 21:10:48 +01:00
|
|
|
app.post('/strategies', function (req, res) {
|
2014-11-25 15:28:08 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
|
|
|
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
|
|
|
|
|
|
|
var errors = req.validationErrors();
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
res.status(400).json(errors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newStrategy = req.body;
|
|
|
|
|
|
|
|
var handleStrategyExists = function() {
|
|
|
|
var errors = [{msg: "A strategy named " + newStrategy.name + " already exists."}];
|
|
|
|
res.status(403).json(errors);
|
|
|
|
};
|
|
|
|
|
|
|
|
var handleCreateStrategy = function() {
|
|
|
|
eventStore.create({
|
|
|
|
type: eventType.strategyCreated,
|
|
|
|
createdBy: req.connection.remoteAddress,
|
|
|
|
data: newStrategy
|
|
|
|
})
|
|
|
|
.then(function () { res.status(201).end(); })
|
|
|
|
.catch(function () { res.status(500).end(); });
|
|
|
|
};
|
|
|
|
|
|
|
|
strategyDb.getStrategy(newStrategy.name)
|
|
|
|
.then(handleStrategyExists)
|
|
|
|
.catch(handleCreateStrategy);
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|