refactoring copy controller middlewares

This commit is contained in:
Simon Martín 2018-05-22 11:56:50 +02:00
parent 433bd01c27
commit f01191472b

View File

@ -34,8 +34,8 @@ CopyController.prototype.route = function (app) {
authorizationMiddleware(this.metadataBackend), authorizationMiddleware(this.metadataBackend),
connectionParamsMiddleware(this.userDatabaseService), connectionParamsMiddleware(this.userDatabaseService),
timeoutLimitsMiddleware(this.metadataBackend), timeoutLimitsMiddleware(this.metadataBackend),
this.handleCopyFrom.bind(this), handleCopyFrom(),
this.responseCopyFrom.bind(this), responseCopyFrom(),
errorMiddleware() errorMiddleware()
]; ];
}; };
@ -106,7 +106,8 @@ function handleCopyTo (statsClient) {
}; };
} }
CopyController.prototype.handleCopyFrom = function (req, res, next) { function handleCopyFrom () {
return function handleCopyFromMiddleware (req, res, next) {
const { sql } = req.query; const { sql } = req.query;
if (!sql) { if (!sql) {
@ -158,10 +159,11 @@ CopyController.prototype.handleCopyFrom = function (req, res, next) {
} catch (err) { } catch (err) {
next(err); next(err);
} }
};
}
}; function responseCopyFrom () {
return function responseCopyFromMiddleware (req, res, next) {
CopyController.prototype.responseCopyFrom = function (req, res, next) {
if (!res.body || !res.body.total_rows) { if (!res.body || !res.body.total_rows) {
return next(new Error("No rows copied")); return next(new Error("No rows copied"));
} }
@ -180,6 +182,7 @@ CopyController.prototype.responseCopyFrom = function (req, res, next) {
} }
res.send(res.body); res.send(res.body);
}; };
}
module.exports = CopyController; module.exports = CopyController;