Skip to content

Commit

Permalink
update response methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertosouza committed Jun 16, 2016
1 parent b106b20 commit 75cada1
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 507 deletions.
7 changes: 4 additions & 3 deletions lib/responses/JSONF.js
Expand Up @@ -12,9 +12,8 @@ var jsonF = {
* @return {Object} JS object to send with res.send
*/
jsonFormater: function jsonFormater(req, res) {
if (!res.locals.data) res.locals.data = {};

if (!res.locals.model) {
if (!res.locals.data) res.locals.data = {};
// set messages
res.locals.data.messages = res.locals.messages;
return res.send(res.locals.data);
Expand All @@ -29,7 +28,9 @@ var jsonF = {
}

// check field privacity access
req.we.db.checkRecordsPrivacity(res.locals.data);
if (res.locals.data) {
req.we.db.checkRecordsPrivacity(res.locals.data);
}

response.meta = res.locals.metadata;

Expand Down
68 changes: 22 additions & 46 deletions lib/responses/methods/index.js
Expand Up @@ -132,6 +132,11 @@ module.exports = {
we.freeResponseMemory(req, res);
});
},
/**
* Deleted response
*
* redirect for html responses
*/
deleted: function deletedResponse() {
var res = this.res;
var req = this.req;
Expand All @@ -153,7 +158,7 @@ module.exports = {
}
}

res.send();
res.format(req.we.responses.formaters);

we.freeResponseMemory(req, res);
});
Expand Down Expand Up @@ -182,13 +187,14 @@ module.exports = {
return res.end();
}

req.we.responses.formaters.html(req, res);
res.format(req.we.responses.formaters);

req.we.freeResponseMemory(req, res);
},
forbidden: function forbiddenResponse(data) {
var res = this.res;
var req = this.req;
var __ = res.locals.__ || req.we.i18n.__;

if (typeof data == 'string') {
res.addMessage('error', {
Expand All @@ -198,32 +204,16 @@ module.exports = {
data = null;
}

if (!data) data = {};

res.status(403);

res.locals.title = res.locals.__('response.forbidden.title');
res.locals.title = __('response.forbidden.title');

if (req.accepts('html')) {
res.locals.layoutName = 'fullwidth';
res.locals.template = '403';
return res.view(data);
}

if (!res.locals.model) {
return res.send({
messages: res.locals.messages
});
}

var response = {};
response[res.locals.model] = data;
response.meta = res.locals.metadata;

// set messages
response.messages = res.locals.messages;

res.send(response);
res.format(req.we.responses.formaters);

req.we.freeResponseMemory(this, res);
},
Expand All @@ -239,7 +229,7 @@ module.exports = {
data = null;
}

if (!data) data = {};
res.locals.data = null;

if (req.we.env == 'dev') {
console.trace('404', req.path);
Expand All @@ -254,12 +244,9 @@ module.exports = {
if (req.accepts('html')) {
res.locals.layoutName = 'fullwidth';
res.locals.template = '404';
return res.view(data);
}

data.messages = res.locals.messages;

res.send(data);
res.format(req.we.responses.formaters);

req.we.freeResponseMemory(req, res);
},
Expand All @@ -277,19 +264,20 @@ module.exports = {

res.locals.title = __('response.serveError.title');

if (!data) data = {};
if (!data) {
data = {};
} else if (typeof data == 'string') {
res.addMessage('error', data);
}

if (req.accepts('html')) {
res.locals.template = '500';
res.locals.layoutName = 'fullwidth';
return res.view(data);
}

// set messages
data.messages = res.locals.messages;

res.send(data);

// send the response
res.format(req.we.responses.formaters);
// helper for unset variables
this.req.we.freeResponseMemory(this, res);
},
/**
Expand All @@ -312,23 +300,12 @@ module.exports = {
}

if (req.accepts('html')) {
// if is html
if (!res.locals.template) res.locals.template = '400';
return res.view(data);
}

if (!res.locals.model) {
// set messages
data.messages = res.locals.messages;
return res.send(data);
}

var response = {};
response[res.locals.model] = data;
response.meta = res.locals.metadata;
// set messages
response.messages = res.locals.messages;
// send the response
res.send(response);
res.format(req.we.responses.formaters);

req.we.freeResponseMemory(req, res);
},
Expand All @@ -340,7 +317,6 @@ module.exports = {
queryError: function queryError(err) {
var res = this.res;
var req = this.req;
var we = req.we;
var isQueryError = true;

if (err) {
Expand Down
23 changes: 12 additions & 11 deletions src/responses/JSONF.js
Expand Up @@ -10,33 +10,34 @@ let jsonF = {
* @return {Object} JS object to send with res.send
*/
jsonFormater: function jsonFormater (req, res) {
if (!res.locals.data) res.locals.data = {};

if (!res.locals.model) {
if (!res.locals.data) res.locals.data = {}
// set messages
res.locals.data.messages = res.locals.messages;
return res.send(res.locals.data);
res.locals.data.messages = res.locals.messages
return res.send(res.locals.data)
}

var response = {};
let response = {}

if (req.we.config.sendNestedModels) {
response[res.locals.model] = res.locals.data;
response[res.locals.model] = res.locals.data
} else {
response[res.locals.model] = parseRecord(req, res, res.locals.data);
response[res.locals.model] = parseRecord(req, res, res.locals.data)
}

// check field privacity access
req.we.db.checkRecordsPrivacity(res.locals.data);
if (res.locals.data) {
req.we.db.checkRecordsPrivacity(res.locals.data)
}

response.meta = res.locals.metadata;
response.meta = res.locals.metadata

if (!isEmpty( res.locals.messages) ) {
// set messages
response.messages = res.locals.messages;
response.messages = res.locals.messages
}

res.send(response);
res.send(response)
}
}

Expand Down

0 comments on commit 75cada1

Please sign in to comment.