Unify get catalog and tables middlewares

This commit is contained in:
Daniel García Aubert 2017-11-19 12:37:09 +01:00
parent 4df46fe5ea
commit 37111f396d

View File

@ -17,8 +17,8 @@ AnalysesController.prototype.register = function (app) {
userMiddleware,
this.prepareContext,
this.createPGClient(),
this.getCatalog(),
this.getTables(),
this.getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }),
this.getDataFromQuery({ queryTemplate: tablesQueryTpl, key: 'tables' }),
this.prepareResponse(),
this.setCacheControlHeader(),
this.sendResponse(),
@ -33,36 +33,19 @@ AnalysesController.prototype.createPGClient = function () {
};
};
AnalysesController.prototype.getCatalog = function () {
AnalysesController.prototype.getDataFromQuery = function ({ queryTemplate, key }) {
const readOnlyTransactionOn = true;
return function getCatalogMiddleware(req, res, next) {
const { pg, user } = res.locals;
const catalogQuery = catalogQueryTpl({ _username: user });
const readOnlyTransactionOn = true;
const sql = queryTemplate({ _username: user });
pg.query(catalogQuery, (err, resultSet = {}) => {
pg.query(sql, (err, resultSet = {}) => {
if (err) {
return next(err);
}
res.locals.catalog = resultSet.rows || [];
next();
}, readOnlyTransactionOn);
};
};
AnalysesController.prototype.getTables = function () {
return function getTablesMiddleware(req, res, next) {
const { pg, user } = res.locals;
const tablesQuery = tablesQueryTpl({ _username: user });
const readOnlyTransactionOn = true;
pg.query(tablesQuery, (err, resultSet = {}) => {
if (err) {
return next(err);
}
res.locals.tables = resultSet.rows || [];
res.locals[key] = resultSet.rows || [];
next();
}, readOnlyTransactionOn);