Now main middlewares return a named function with the right context bound

This commit is contained in:
Daniel García Aubert
2017-12-29 16:15:48 +01:00
parent 64d601179d
commit a00c2b1eef
+81 -71
View File
@@ -22,7 +22,7 @@ NamedMapsAdminController.prototype.register = function (app) {
userMiddleware,
this.checkContentType('POST', 'POST TEMPLATE'),
this.authorizedByAPIKey('create', 'POST TEMPLATE'),
this.create.bind(this)
this.create()
);
app.put(
@@ -31,7 +31,7 @@ NamedMapsAdminController.prototype.register = function (app) {
userMiddleware,
this.checkContentType('PUT', 'PUT TEMPLATE'),
this.authorizedByAPIKey('update', 'PUT TEMPLATE'),
this.update.bind(this)
this.update()
);
app.get(
@@ -39,7 +39,7 @@ NamedMapsAdminController.prototype.register = function (app) {
cors(),
userMiddleware,
this.authorizedByAPIKey('get', 'GET TEMPLATE'),
this.retrieve.bind(this)
this.retrieve()
);
app.delete(
@@ -47,7 +47,7 @@ NamedMapsAdminController.prototype.register = function (app) {
cors(),
userMiddleware,
this.authorizedByAPIKey('delete', 'DELETE TEMPLATE'),
this.destroy.bind(this)
this.destroy()
);
app.get(
@@ -55,7 +55,7 @@ NamedMapsAdminController.prototype.register = function (app) {
cors(),
userMiddleware,
this.authorizedByAPIKey('list', 'GET TEMPLATE LIST'),
this.list.bind(this)
this.list()
);
app.options(
@@ -96,97 +96,107 @@ NamedMapsAdminController.prototype.checkContentType = function (action, label) {
};
};
NamedMapsAdminController.prototype.create = function(req, res, next) {
const cdbuser = res.locals.user;
const cfg = req.body;
NamedMapsAdminController.prototype.create = function () {
return function createTemplateMiddleware (req, res, next) {
const cdbuser = res.locals.user;
const cfg = req.body;
this.templateMaps.addTemplate(cdbuser, cfg, (err, tpl_id) => {
if (err) {
return next(err);
}
this.templateMaps.addTemplate(cdbuser, cfg, (err, tpl_id) => {
if (err) {
return next(err);
}
res.status(200);
res.status(200);
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template_id: tpl_id });
});
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template_id: tpl_id });
});
}.bind(this);
};
NamedMapsAdminController.prototype.update = function(req, res, next) {
const cdbuser = res.locals.user;
const template = req.body;
const tpl_id = templateName(req.params.template_id);
NamedMapsAdminController.prototype.update = function () {
return function updateTemplateMiddleware (req, res, next) {
const cdbuser = res.locals.user;
const template = req.body;
const tpl_id = templateName(req.params.template_id);
this.templateMaps.updTemplate(cdbuser, tpl_id, template, (err) => {
if (err) {
return next(err);
}
this.templateMaps.updTemplate(cdbuser, tpl_id, template, (err) => {
if (err) {
return next(err);
}
res.status(200);
res.status(200);
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template_id: tpl_id });
});
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template_id: tpl_id });
});
}.bind(this);
};
NamedMapsAdminController.prototype.retrieve = function(req, res, next) {
req.profiler.start('windshaft-cartodb.get_template');
NamedMapsAdminController.prototype.retrieve = function () {
return function updateTemplateMiddleware (req, res, next) {
req.profiler.start('windshaft-cartodb.get_template');
const cdbuser = res.locals.user;
const tpl_id = templateName(req.params.template_id);
const cdbuser = res.locals.user;
const tpl_id = templateName(req.params.template_id);
this.templateMaps.getTemplate(cdbuser, tpl_id, (err, tpl_val) => {
if (err) {
return next(err);
}
this.templateMaps.getTemplate(cdbuser, tpl_id, (err, tpl_val) => {
if (err) {
return next(err);
}
if (!tpl_val) {
const error = new Error(`Cannot find template '${tpl_id}' of user '${cdbuser}'`);
error.http_status = 404;
return next(error);
}
// auth_id was added by ourselves,
// so we remove it before returning to the user
delete tpl_val.auth_id;
if (!tpl_val) {
const error = new Error(`Cannot find template '${tpl_id}' of user '${cdbuser}'`);
error.http_status = 404;
return next(error);
}
// auth_id was added by ourselves,
// so we remove it before returning to the user
delete tpl_val.auth_id;
res.status(200);
res.status(200);
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template: tpl_val });
});
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template: tpl_val });
});
}.bind(this);
};
NamedMapsAdminController.prototype.destroy = function(req, res, next) {
req.profiler.start('windshaft-cartodb.delete_template');
NamedMapsAdminController.prototype.destroy = function () {
return function destroyTemplateMiddleware (req, res, next) {
req.profiler.start('windshaft-cartodb.delete_template');
const cdbuser = res.locals.user;
const tpl_id = templateName(req.params.template_id);
const cdbuser = res.locals.user;
const tpl_id = templateName(req.params.template_id);
this.templateMaps.delTemplate(cdbuser, tpl_id, (err/*, tpl_val*/) => {
if (err) {
return next(err);
}
this.templateMaps.delTemplate(cdbuser, tpl_id, (err/*, tpl_val*/) => {
if (err) {
return next(err);
}
res.status(204);
res.status(204);
const method = req.query.callback ? 'jsonp' : 'json';
res[method]('');
});
const method = req.query.callback ? 'jsonp' : 'json';
res[method]('');
});
}.bind(this);
};
NamedMapsAdminController.prototype.list = function(req, res, next) {
req.profiler.start('windshaft-cartodb.get_template_list');
NamedMapsAdminController.prototype.list = function () {
return function listTemplatesMiddleware (req, res, next) {
req.profiler.start('windshaft-cartodb.get_template_list');
const cdbuser = res.locals.user;
const cdbuser = res.locals.user;
this.templateMaps.listTemplates(cdbuser, (err, tpl_ids) => {
if (err) {
return next(err);
}
this.templateMaps.listTemplates(cdbuser, (err, tpl_ids) => {
if (err) {
return next(err);
}
res.status(200);
res.status(200);
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template_ids: tpl_ids });
});
const method = req.query.callback ? 'jsonp' : 'json';
res[method]({ template_ids: tpl_ids });
});
}.bind(this);
};