Make catalog method a regular middleware factory

This commit is contained in:
Daniel García Aubert 2017-11-17 18:28:37 +01:00
parent 5d6ccc07fd
commit 804c6645fa

View File

@ -18,13 +18,39 @@ AnalysesController.prototype.register = function(app) {
cors(),
userMiddleware,
this.prepareContext,
this.catalog.bind(this),
this.catalog(),
this.prepareResponse(),
this.setCacheControlHeader(),
this.sendResponse()
);
};
AnalysesController.prototype.catalog = function () {
return function catalogMiddleware(req, res, next) {
const { user } = res.locals;
step(
function catalogQuery() {
var pg = new PSQL(dbParamsFromReqParams(res.locals));
getMetadata(user, pg, this);
},
function prepareResponse(err, catalogWithTables) {
if (err) {
if (err.message.match(/permission\sdenied/)) {
err = new Error('Unauthorized');
err.http_status = 401;
}
return next(err);
}
res.locals.catalogWithTables = catalogWithTables;
next();
}
);
};
};
AnalysesController.prototype.prepareResponse = function () {
return function prepareResponseMiddleware (req, res, next) {
const { catalogWithTables } = res.locals;
@ -83,30 +109,6 @@ AnalysesController.prototype.sendResponse = function() {
};
};
AnalysesController.prototype.catalog = function (req, res, next) {
var username = res.locals.user;
step(
function catalogQuery() {
var pg = new PSQL(dbParamsFromReqParams(res.locals));
getMetadata(username, pg, this);
},
function prepareResponse(err, catalogWithTables) {
if (err) {
if (err.message.match(/permission\sdenied/)) {
err = new Error('Unauthorized');
err.http_status = 401;
}
return next(err);
}
res.locals.catalogWithTables = catalogWithTables;
next();
}
);
};
var catalogQueryTpl = dot.template(
'SELECT analysis_def->>\'type\' as type, * FROM cdb_analysis_catalog WHERE username = \'{{=it._username}}\''
);