1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/admin-api/strategy.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

77 lines
2.2 KiB
JavaScript

'use strict';
const Controller = require('../controller');
const extractUser = require('../../extract-user');
const { handleErrors } = require('./util');
const {
DELETE_STRATEGY,
CREATE_STRATEGY,
UPDATE_STRATEGY,
} = require('../../permissions');
const version = 1;
class StrategyController extends Controller {
constructor(config, { strategyService }) {
super(config);
this.logger = config.getLogger('/admin-api/strategy.js');
this.strategyService = strategyService;
this.get('/', this.getAllStratgies);
this.get('/:name', this.getStrategy);
this.delete('/:name', this.removeStrategy, DELETE_STRATEGY);
this.post('/', this.createStrategy, CREATE_STRATEGY);
this.put('/:strategyName', this.updateStrategy, UPDATE_STRATEGY);
}
async getAllStratgies(req, res) {
const strategies = await this.strategyService.getStrategies();
res.json({ version, strategies });
}
async getStrategy(req, res) {
try {
const { name } = req.params;
const strategy = await this.strategyService.getStrategy(name);
res.json(strategy).end();
} catch (err) {
res.status(404).json({ error: 'Could not find strategy' });
}
}
async removeStrategy(req, res) {
const strategyName = req.params.name;
const userName = extractUser(req);
try {
await this.strategyService.removeStrategy(strategyName, userName);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async createStrategy(req, res) {
const userName = extractUser(req);
try {
await this.strategyService.createStrategy(req.body, userName);
res.status(201).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async updateStrategy(req, res) {
const userName = extractUser(req);
try {
await this.strategyService.updateStrategy(req.body, userName);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
}
module.exports = StrategyController;