Merge pull request #899 from CartoDB/refactor-named-maps-admin
Named Maps Admin: Extract middlewares form controller's context
This commit is contained in:
commit
f49d7478d7
@ -26,9 +26,10 @@ NamedMapsAdminController.prototype.register = function (app) {
|
|||||||
userMiddleware(),
|
userMiddleware(),
|
||||||
localsMiddleware(),
|
localsMiddleware(),
|
||||||
credentialsMiddleware(),
|
credentialsMiddleware(),
|
||||||
this.checkContentType('POST', 'POST TEMPLATE'),
|
checkContentType({ action: 'POST', label: 'POST TEMPLATE' }),
|
||||||
this.authorizedByAPIKey('create', 'POST TEMPLATE'),
|
authorizedByAPIKey({ authApi: this.authApi, action: 'create', label: 'POST TEMPLATE' }),
|
||||||
this.create()
|
createTemplate({ templateMaps: this.templateMaps }),
|
||||||
|
sendResponse()
|
||||||
);
|
);
|
||||||
|
|
||||||
app.put(
|
app.put(
|
||||||
@ -37,9 +38,10 @@ NamedMapsAdminController.prototype.register = function (app) {
|
|||||||
userMiddleware(),
|
userMiddleware(),
|
||||||
localsMiddleware(),
|
localsMiddleware(),
|
||||||
credentialsMiddleware(),
|
credentialsMiddleware(),
|
||||||
this.checkContentType('PUT', 'PUT TEMPLATE'),
|
checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }),
|
||||||
this.authorizedByAPIKey('update', 'PUT TEMPLATE'),
|
authorizedByAPIKey({ authApi: this.authApi, action: 'update', label: 'PUT TEMPLATE' }),
|
||||||
this.update()
|
updateTemplate({ templateMaps: this.templateMaps }),
|
||||||
|
sendResponse()
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get(
|
app.get(
|
||||||
@ -48,8 +50,9 @@ NamedMapsAdminController.prototype.register = function (app) {
|
|||||||
userMiddleware(),
|
userMiddleware(),
|
||||||
localsMiddleware(),
|
localsMiddleware(),
|
||||||
credentialsMiddleware(),
|
credentialsMiddleware(),
|
||||||
this.authorizedByAPIKey('get', 'GET TEMPLATE'),
|
authorizedByAPIKey({ authApi: this.authApi, action: 'get', label: 'GET TEMPLATE' }),
|
||||||
this.retrieve()
|
retrieveTemplate({ templateMaps: this.templateMaps }),
|
||||||
|
sendResponse()
|
||||||
);
|
);
|
||||||
|
|
||||||
app.delete(
|
app.delete(
|
||||||
@ -58,8 +61,9 @@ NamedMapsAdminController.prototype.register = function (app) {
|
|||||||
userMiddleware(),
|
userMiddleware(),
|
||||||
localsMiddleware(),
|
localsMiddleware(),
|
||||||
credentialsMiddleware(),
|
credentialsMiddleware(),
|
||||||
this.authorizedByAPIKey('delete', 'DELETE TEMPLATE'),
|
authorizedByAPIKey({ authApi: this.authApi, action: 'delete', label: 'DELETE TEMPLATE' }),
|
||||||
this.destroy()
|
destroyTemplate({ templateMaps: this.templateMaps }),
|
||||||
|
sendResponse()
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get(
|
app.get(
|
||||||
@ -68,8 +72,9 @@ NamedMapsAdminController.prototype.register = function (app) {
|
|||||||
userMiddleware(),
|
userMiddleware(),
|
||||||
localsMiddleware(),
|
localsMiddleware(),
|
||||||
credentialsMiddleware(),
|
credentialsMiddleware(),
|
||||||
this.authorizedByAPIKey('list', 'GET TEMPLATE LIST'),
|
authorizedByAPIKey({ authApi: this.authApi, action: 'list', label: 'GET TEMPLATE LIST' }),
|
||||||
this.list()
|
listTemplates({ templateMaps: this.templateMaps }),
|
||||||
|
sendResponse()
|
||||||
);
|
);
|
||||||
|
|
||||||
app.options(
|
app.options(
|
||||||
@ -78,10 +83,23 @@ NamedMapsAdminController.prototype.register = function (app) {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.authorizedByAPIKey = function (action, label) {
|
function checkContentType ({ action, label }) {
|
||||||
|
return function checkContentTypeMiddleware (req, res, next) {
|
||||||
|
if (!req.is('application/json')) {
|
||||||
|
const error = new Error(`template ${action} data must be of type application/json`);
|
||||||
|
error.label = label;
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function authorizedByAPIKey ({ authApi, action, label }) {
|
||||||
return function authorizedByAPIKeyMiddleware (req, res, next) {
|
return function authorizedByAPIKeyMiddleware (req, res, next) {
|
||||||
const { user } = res.locals;
|
const { user } = res.locals;
|
||||||
this.authApi.authorizedByAPIKey(user, res, (err, authenticated) => {
|
|
||||||
|
authApi.authorizedByAPIKey(user, res, (err, authenticated) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@ -95,65 +113,52 @@ NamedMapsAdminController.prototype.authorizedByAPIKey = function (action, label)
|
|||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}.bind(this);
|
|
||||||
};
|
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.checkContentType = function (action, label) {
|
|
||||||
return function checkContentTypeMiddleware (req, res, next) {
|
|
||||||
if (!req.is('application/json')) {
|
|
||||||
const error = new Error(`template ${action} data must be of type application/json`);
|
|
||||||
error.label = label;
|
|
||||||
return next(error);
|
|
||||||
}
|
|
||||||
next();
|
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.create = function () {
|
function createTemplate ({ templateMaps }) {
|
||||||
return function createTemplateMiddleware (req, res, next) {
|
return function createTemplateMiddleware (req, res, next) {
|
||||||
const { user } = res.locals;
|
const { user } = res.locals;
|
||||||
const template = req.body;
|
const template = req.body;
|
||||||
|
|
||||||
this.templateMaps.addTemplate(user, template, (err, templateId) => {
|
templateMaps.addTemplate(user, template, (err, templateId) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200);
|
res.body = { template_id: templateId };
|
||||||
|
|
||||||
const method = req.query.callback ? 'jsonp' : 'json';
|
next();
|
||||||
res[method]({ template_id: templateId });
|
|
||||||
});
|
});
|
||||||
}.bind(this);
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.update = function () {
|
function updateTemplate ({ templateMaps }) {
|
||||||
return function updateTemplateMiddleware (req, res, next) {
|
return function updateTemplateMiddleware (req, res, next) {
|
||||||
const { user } = res.locals;
|
const { user } = res.locals;
|
||||||
const template = req.body;
|
const template = req.body;
|
||||||
const templateId = templateName(req.params.template_id);
|
const templateId = templateName(req.params.template_id);
|
||||||
|
|
||||||
this.templateMaps.updTemplate(user, templateId, template, (err) => {
|
templateMaps.updTemplate(user, templateId, template, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200);
|
res.body = { template_id: templateId };
|
||||||
|
|
||||||
const method = req.query.callback ? 'jsonp' : 'json';
|
next();
|
||||||
res[method]({ template_id: templateId });
|
|
||||||
});
|
});
|
||||||
}.bind(this);
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.retrieve = function () {
|
function retrieveTemplate ({ templateMaps }) {
|
||||||
return function retrieveTemplateMiddleware (req, res, next) {
|
return function retrieveTemplateMiddleware (req, res, next) {
|
||||||
req.profiler.start('windshaft-cartodb.get_template');
|
req.profiler.start('windshaft-cartodb.get_template');
|
||||||
|
|
||||||
const { user } = res.locals;
|
const { user } = res.locals;
|
||||||
const templateId = templateName(req.params.template_id);
|
const templateId = templateName(req.params.template_id);
|
||||||
|
|
||||||
this.templateMaps.getTemplate(user, templateId, (err, template) => {
|
templateMaps.getTemplate(user, templateId, (err, template) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@ -167,49 +172,56 @@ NamedMapsAdminController.prototype.retrieve = function () {
|
|||||||
// so we remove it before returning to the user
|
// so we remove it before returning to the user
|
||||||
delete template.auth_id;
|
delete template.auth_id;
|
||||||
|
|
||||||
res.status(200);
|
res.body = { template };
|
||||||
|
|
||||||
const method = req.query.callback ? 'jsonp' : 'json';
|
next();
|
||||||
res[method]({ template });
|
|
||||||
});
|
});
|
||||||
}.bind(this);
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.destroy = function () {
|
function destroyTemplate ({ templateMaps }) {
|
||||||
return function destroyTemplateMiddleware (req, res, next) {
|
return function destroyTemplateMiddleware (req, res, next) {
|
||||||
req.profiler.start('windshaft-cartodb.delete_template');
|
req.profiler.start('windshaft-cartodb.delete_template');
|
||||||
|
|
||||||
const { user } = res.locals;
|
const { user } = res.locals;
|
||||||
const templateId = templateName(req.params.template_id);
|
const templateId = templateName(req.params.template_id);
|
||||||
|
|
||||||
this.templateMaps.delTemplate(user, templateId, (err/* , tpl_val */) => {
|
templateMaps.delTemplate(user, templateId, (err/* , tpl_val */) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(204);
|
res.statusCode = 204;
|
||||||
|
res.body = '';
|
||||||
|
|
||||||
const method = req.query.callback ? 'jsonp' : 'json';
|
next();
|
||||||
res[method]('');
|
|
||||||
});
|
});
|
||||||
}.bind(this);
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
NamedMapsAdminController.prototype.list = function () {
|
function listTemplates ({ templateMaps }) {
|
||||||
return function listTemplatesMiddleware (req, res, next) {
|
return function listTemplatesMiddleware (req, res, next) {
|
||||||
req.profiler.start('windshaft-cartodb.get_template_list');
|
req.profiler.start('windshaft-cartodb.get_template_list');
|
||||||
|
|
||||||
const { user } = res.locals;
|
const { user } = res.locals;
|
||||||
|
|
||||||
this.templateMaps.listTemplates(user, (err, templateIds) => {
|
templateMaps.listTemplates(user, (err, templateIds) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200);
|
res.body = { template_ids: templateIds };
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendResponse () {
|
||||||
|
return function sendResponseMiddleware (req, res) {
|
||||||
|
res.status(res.statusCode || 200);
|
||||||
|
|
||||||
const method = req.query.callback ? 'jsonp' : 'json';
|
const method = req.query.callback ? 'jsonp' : 'json';
|
||||||
res[method]({ template_ids: templateIds });
|
res[method](res.body);
|
||||||
});
|
};
|
||||||
}.bind(this);
|
}
|
||||||
};
|
|
||||||
|
Loading…
Reference in New Issue
Block a user