1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/admin-api/addon.js
Ivar Conradi Østhus 17c8fe7710 feat: Introduce addon framework
fixes: #587

Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2021-02-05 15:20:00 +01:00

87 lines
2.4 KiB
JavaScript

'use strict';
const Controller = require('../controller');
const extractUser = require('../../extract-user');
const { handleErrors } = require('./util');
const {
CREATE_ADDON,
UPDATE_ADDON,
DELETE_ADDON,
} = require('../../permissions');
class AddonController extends Controller {
constructor(config, { addonService }) {
super(config);
this.logger = config.getLogger('/admin-api/addon.js');
this.addonService = addonService;
this.get('/', this.getAddons);
this.post('/', this.createAddon, CREATE_ADDON);
this.get('/:id', this.getAddon);
this.put('/:id', this.updateAddon, UPDATE_ADDON);
this.delete('/:id', this.deleteAddon, DELETE_ADDON);
}
async getAddons(req, res) {
try {
const addons = await this.addonService.getAddons();
const providers = await this.addonService.getProviderDefinition();
res.json({ addons, providers });
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async getAddon(req, res) {
const { id } = req.params;
try {
const addon = await this.addonService.getAddon(id);
res.json(addon);
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async updateAddon(req, res) {
const { id } = req.params;
const createdBy = extractUser(req);
const data = req.body;
try {
const addon = await this.addonService.updateAddon(
id,
data,
createdBy,
);
res.status(200).json(addon);
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async createAddon(req, res) {
const createdBy = extractUser(req);
const data = req.body;
try {
const addon = await this.addonService.createAddon(data, createdBy);
res.status(201).json(addon);
} catch (error) {
handleErrors(res, this.logger, error);
}
}
async deleteAddon(req, res) {
const { id } = req.params;
const username = extractUser(req);
try {
await this.addonService.removeAddon(id, username);
res.status(200).end();
} catch (error) {
handleErrors(res, this.logger, error);
}
}
}
module.exports = AddonController;