Extract finish function

This commit is contained in:
Raul Ochoa
2015-04-23 11:47:01 +02:00
parent e7d134d70c
commit fed29b3b50
+21 -60
View File
@@ -58,18 +58,7 @@ TemplateMapsController.prototype.create = function(req, res) {
assert.ifError(err);
return { template_id: tpl_id };
},
function finish(err, response){
if (err) {
response = { error: ''+err };
var statusCode = 400;
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
self.app.sendError(res, response, statusCode, 'POST TEMPLATE', err);
} else {
self.app.sendResponse(res, [response, 200]);
}
}
finishFn(self.app, res, 'POST TEMPLATE')
);
};
@@ -105,18 +94,7 @@ TemplateMapsController.prototype.update = function(req, res) {
return { template_id: tpl_id };
},
function finish(err, response){
if (err) {
var statusCode = 400;
response = { error: ''+err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
self.app.sendError(res, response, statusCode, 'PUT TEMPLATE', err);
} else {
self.app.sendResponse(res, [response, 200]);
}
}
finishFn(self.app, res, 'PUT TEMPLATE')
);
};
@@ -158,18 +136,7 @@ TemplateMapsController.prototype.retrieve = function(req, res) {
delete tpl_val.auth_id;
return { template: tpl_val };
},
function finish(err, response){
if (err){
var statusCode = 400;
response = { error: ''+err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
self.app.sendError(res, response, statusCode, 'GET TEMPLATE', err);
} else {
self.app.sendResponse(res, [response, 200]);
}
}
finishFn(self.app, res, 'GET TEMPLATE')
);
};
@@ -202,18 +169,7 @@ TemplateMapsController.prototype.destroy = function(req, res) {
if ( err ) throw err;
return { status: 'ok' };
},
function finish(err, response){
if (err){
var statusCode = 400;
response = { error: ''+err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
self.app.sendError(res, response, statusCode, 'DELETE TEMPLATE', err);
} else {
self.app.sendResponse(res, ['', 204]);
}
}
finishFn(self.app, res, 'DELETE TEMPLATE', ['', 204])
);
};
@@ -244,18 +200,7 @@ TemplateMapsController.prototype.list = function(req, res) {
assert.ifError(err);
return { template_ids: tpl_ids };
},
function finish(err, response){
var statusCode = 200;
if (err) {
response = { error: '' + err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
self.app.sendError(res, response, statusCode, 'GET TEMPLATE LIST', err);
} else {
self.app.sendResponse(res, [response, statusCode]);
}
}
finishFn(self.app, res, 'GET TEMPLATE LIST')
);
};
@@ -433,6 +378,22 @@ TemplateMapsController.prototype.setDBParams = function(cdbuser, params, callbac
);
};
function finishFn(app, res, description, okResponse) {
return function finish(err, response){
var statusCode = 200;
if (err) {
statusCode = 400;
response = { error: '' + err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
app.sendError(res, response, statusCode, description, err);
} else {
app.sendResponse(res, okResponse || [response, statusCode]);
}
};
}
function templateName(templateId) {
var templateIdTokens = templateId.split('@');
var name = templateIdTokens[0];