1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

chore(modernize): Admin FeatureController

This commit is contained in:
ivaosthu 2018-11-30 11:11:12 +01:00 committed by Ivar Conradi Østhus
parent 4a98d718cf
commit e285f39bcb
3 changed files with 117 additions and 90 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { Router } = require('express'); const Controller = require('../controller');
const joi = require('joi'); const joi = require('joi');
const logger = require('../../logger')('/admin-api/feature.js'); const logger = require('../../logger')('/admin-api/feature.js');
@ -71,28 +71,54 @@ function validateStrategy(featureToggle) {
const version = 1; const version = 1;
module.exports.router = function(config) { class FeatureController extends Controller {
const { featureToggleStore, eventStore } = config.stores; constructor({ featureToggleStore, eventStore }) {
const router = Router(); super();
this.featureToggleStore = featureToggleStore;
this.eventStore = eventStore;
router.get('/', (req, res) => { this.get('/', this.getAllToggles);
featureToggleStore this.post('/', this.createToggle);
.getFeatures() this.get('/:featureName', this.getToggle);
.then(features => res.json({ version, features })); this.put('/:featureName', this.updateToggle);
}); this.delete('/:featureName', this.deleteToggle);
this.post('/validate', this.validate);
this.post('/:featureName/toggle', this.toggle);
}
router.get('/:featureName', (req, res) => { async getAllToggles(req, res) {
featureToggleStore const features = await this.featureToggleStore.getFeatures();
.getFeature(req.params.featureName) res.json({ version, features });
.then(feature => res.json(feature).end()) }
.catch(() =>
res.status(404).json({ error: 'Could not find feature' }) async getToggle(req, res) {
try {
const featureName = req.params.featureName;
const feature = await this.featureToggleStore.getFeature(
featureName
); );
}); res.json(feature).end();
} catch (err) {
res.status(404).json({ error: 'Could not find feature' });
}
}
function validateUniqueName(req) { async validate(req, res) {
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must be URL friendly').isUrlFriendlyName();
try {
await validateRequest(req);
await this.validateUniqueName(req);
res.status(201).end();
} catch (error) {
handleErrors(req, res, error);
}
}
validateUniqueName(req) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
featureToggleStore this.featureToggleStore
.hasFeature(req.body.name) .hasFeature(req.body.name)
.then(definition => { .then(definition => {
const msg = definition.archived const msg = definition.archived
@ -104,94 +130,87 @@ module.exports.router = function(config) {
}); });
} }
router.post('/validate', (req, res) => { async createToggle(req, res) {
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must be URL friendly').isUrlFriendlyName();
validateRequest(req)
.then(validateUniqueName)
.then(() => res.status(201).end())
.catch(error => handleErrors(req, res, error));
});
router.post('/', (req, res) => {
req.checkBody('name', 'Name is required').notEmpty(); req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must be URL friendly').isUrlFriendlyName(); req.checkBody('name', 'Name must be URL friendly').isUrlFriendlyName();
const userName = extractUser(req); const userName = extractUser(req);
validateRequest(req) try {
.then(validateUniqueName) await validateRequest(req);
.then(_req => _req.body) await this.validateUniqueName(req);
.then(validateStrategy) const featureToggle = await validateStrategy(req.body);
.then(featureToggle => await this.eventStore.store({
eventStore.store({ type: FEATURE_CREATED,
type: FEATURE_CREATED, createdBy: userName,
createdBy: userName, data: featureToggle,
data: featureToggle, });
}) res.status(201).end();
) } catch (error) {
.then(() => res.status(201).end()) handleErrors(req, res, error);
.catch(error => handleErrors(req, res, error)); }
}); }
router.put('/:featureName', (req, res) => { async updateToggle(req, res) {
const featureName = req.params.featureName; const featureName = req.params.featureName;
const userName = extractUser(req); const userName = extractUser(req);
const updatedFeature = req.body; const updatedFeature = req.body;
updatedFeature.name = featureName; updatedFeature.name = featureName;
featureToggleStore try {
.getFeature(featureName) await this.featureToggleStore.getFeature(featureName);
.then(() => validateStrategy(updatedFeature)) await validateStrategy(updatedFeature);
.then(() => await this.eventStore.store({
eventStore.store({ type: FEATURE_UPDATED,
type: FEATURE_UPDATED, createdBy: userName,
createdBy: userName, data: updatedFeature,
data: updatedFeature, });
}) res.status(200).end();
) } catch (error) {
.then(() => res.status(200).end()) handleErrors(req, res, error);
.catch(error => handleErrors(req, res, error)); }
}); }
router.post('/:featureName/toggle', (req, res) => { async toggle(req, res) {
const featureName = req.params.featureName; const featureName = req.params.featureName;
const userName = extractUser(req); const userName = extractUser(req);
featureToggleStore try {
.getFeature(featureName) const feature = await this.featureToggleStore.getFeature(
.then(feature => { featureName
feature.enabled = !feature.enabled; );
return eventStore.store({
type: FEATURE_UPDATED,
createdBy: userName,
data: feature,
});
})
.then(() => res.status(200).end())
.catch(error => handleErrors(req, res, error));
});
router.delete('/:featureName', (req, res) => { feature.enabled = !feature.enabled;
await this.eventStore.store({
type: FEATURE_UPDATED,
createdBy: userName,
data: feature,
});
res.status(200).end();
} catch (error) {
handleErrors(req, res, error);
}
}
async deleteToggle(req, res) {
const featureName = req.params.featureName; const featureName = req.params.featureName;
const userName = extractUser(req); const userName = extractUser(req);
featureToggleStore try {
.getFeature(featureName) await this.featureToggleStore.getFeature(featureName);
.then(() => await this.eventStore.store({
eventStore.store({ type: FEATURE_ARCHIVED,
type: FEATURE_ARCHIVED, createdBy: userName,
createdBy: userName, data: {
data: { name: featureName,
name: featureName, },
}, });
}) res.status(200).end();
) } catch (error) {
.then(() => res.status(200).end()) handleErrors(req, res, error);
.catch(error => handleErrors(req, res, error)); }
}); }
}
return router; module.exports = FeatureController;
};

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Controller = require('../controller'); const Controller = require('../controller');
const features = require('./feature.js'); const FeatureController = require('./feature.js');
const ArchiveController = require('./archive.js'); const ArchiveController = require('./archive.js');
const EventController = require('./event.js'); const EventController = require('./event.js');
const strategies = require('./strategy'); const strategies = require('./strategy');
@ -16,7 +16,7 @@ class AdminApi extends Controller {
const stores = config.stores; const stores = config.stores;
this.app.get('/', this.index); this.app.get('/', this.index);
this.app.use('/features', features.router(config)); this.app.use('/features', new FeatureController(stores).router());
this.app.use('/archive', new ArchiveController(stores).router()); this.app.use('/archive', new ArchiveController(stores).router());
this.app.use('/strategies', strategies.router(config)); this.app.use('/strategies', strategies.router(config));
this.app.use('/events', new EventController(stores).router()); this.app.use('/events', new EventController(stores).router());

View File

@ -18,6 +18,14 @@ class Controller {
this.app.post(path, handler.bind(this)); this.app.post(path, handler.bind(this));
} }
put(path, handler) {
this.app.put(path, handler.bind(this));
}
delete(path, handler) {
this.app.delete(path, handler.bind(this));
}
router() { router() {
return this.app; return this.app;
} }