2016-05-18 22:50:06 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-06 17:31:34 +02:00
|
|
|
angular.module('ffffng').factory('Resources', function (_, Constraints, Validator, ErrorTypes, Logger) {
|
2016-06-07 14:08:04 +02:00
|
|
|
function respond(res, httpCode, data, type) {
|
|
|
|
switch (type) {
|
|
|
|
case 'html':
|
|
|
|
res.writeHead(httpCode, {'Content-Type': 'text/html'});
|
|
|
|
res.end(data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
res.writeHead(httpCode, {'Content-Type': 'application/json'});
|
|
|
|
res.end(JSON.stringify(data));
|
|
|
|
break;
|
|
|
|
}
|
2016-05-18 22:50:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 12:08:50 +02:00
|
|
|
function orderByClause(restParams, defaultSortField, allowedSortFields) {
|
|
|
|
var sortField = _.includes(allowedSortFields, restParams._sortField) ? restParams._sortField : undefined;
|
|
|
|
if (!sortField) {
|
|
|
|
sortField = defaultSortField;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: 'ORDER BY ' + sortField + ' ' + (restParams._sortDir === 'ASC' ? 'ASC' : 'DESC'),
|
|
|
|
params: []
|
2017-05-06 17:31:34 +02:00
|
|
|
};
|
2016-06-11 12:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function limitOffsetClause(restParams) {
|
|
|
|
var page = restParams._page;
|
|
|
|
var perPage = restParams._perPage;
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: 'LIMIT ? OFFSET ?',
|
|
|
|
params: [perPage, ((page - 1) * perPage)]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeForLikePattern(str) {
|
|
|
|
return str
|
|
|
|
.replace(/\\/g, '\\\\')
|
|
|
|
.replace(/%/g, '\\%')
|
2017-05-06 17:31:34 +02:00
|
|
|
.replace(/_/g, '\\_');
|
2016-06-11 12:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function filterCondition(restParams, filterFields) {
|
|
|
|
if (_.isEmpty(filterFields)) {
|
|
|
|
return {
|
|
|
|
query: '1 = 1',
|
|
|
|
params: []
|
2017-05-06 17:31:34 +02:00
|
|
|
};
|
2016-06-11 12:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var query = _.join(
|
|
|
|
_.map(filterFields, function (field) {
|
|
|
|
return 'LOWER(' + field + ') LIKE ?';
|
|
|
|
}),
|
|
|
|
' OR '
|
|
|
|
);
|
|
|
|
|
|
|
|
query += ' ESCAPE \'\\\'';
|
|
|
|
|
|
|
|
var search = '%' + (_.isString(restParams.q) ? escapeForLikePattern(_.toLower(restParams.q.trim())) : '') + '%';
|
|
|
|
var params = _.times(filterFields.length, _.constant(search));
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: query,
|
|
|
|
params: params
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-23 11:30:41 +02:00
|
|
|
function getConstrainedValues(data, constraints) {
|
|
|
|
var values = {};
|
|
|
|
_.each(_.keys(constraints), function (key) {
|
|
|
|
var value = data[key];
|
2017-05-06 17:31:34 +02:00
|
|
|
values[key] =
|
|
|
|
_.isUndefined(value) && !_.isUndefined(constraints[key].default) ? constraints[key].default : value;
|
2016-07-23 11:30:41 +02:00
|
|
|
});
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2016-05-18 22:50:06 +02:00
|
|
|
return {
|
|
|
|
getData: function (req) {
|
|
|
|
return _.extend({}, req.body, req.params, req.query);
|
|
|
|
},
|
|
|
|
|
2016-07-23 11:30:41 +02:00
|
|
|
getValidRestParams: function(type, subtype, req, callback) {
|
2016-06-07 10:50:15 +02:00
|
|
|
var constraints = Constraints.rest[type];
|
|
|
|
if (!_.isPlainObject(constraints)) {
|
|
|
|
Logger.tag('validation', 'rest').error('Unknown REST resource type: {}', type);
|
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
|
2016-07-23 11:30:41 +02:00
|
|
|
var filterConstraints = {};
|
|
|
|
if (subtype) {
|
|
|
|
filterConstraints = Constraints[subtype + 'Filters'];
|
|
|
|
if (!_.isPlainObject(filterConstraints)) {
|
|
|
|
Logger.tag('validation', 'rest').error('Unknown REST resource subtype: {}', subtype);
|
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
var data = this.getData(req);
|
|
|
|
|
2016-07-23 11:30:41 +02:00
|
|
|
var restParams = getConstrainedValues(data, constraints);
|
|
|
|
var filterParams = getConstrainedValues(data, filterConstraints);
|
2016-06-07 10:50:15 +02:00
|
|
|
|
|
|
|
var areValidParams = Validator.forConstraints(constraints);
|
2016-07-23 11:30:41 +02:00
|
|
|
var areValidFilters = Validator.forConstraints(filterConstraints);
|
|
|
|
if (!areValidParams(restParams) || !areValidFilters(filterParams)) {
|
2016-06-07 10:50:15 +02:00
|
|
|
return callback({data: 'Invalid REST parameters.', type: ErrorTypes.badRequest});
|
|
|
|
}
|
|
|
|
|
2016-07-23 11:30:41 +02:00
|
|
|
restParams.filters = filterParams;
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
callback(null, restParams);
|
|
|
|
},
|
|
|
|
|
2016-06-07 12:39:52 +02:00
|
|
|
filter: function (entities, allowedFilterFields, restParams) {
|
|
|
|
var query = restParams.q;
|
2016-07-23 11:30:41 +02:00
|
|
|
if (query) {
|
|
|
|
query = _.toLower(query.trim());
|
2016-06-07 12:39:52 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 11:30:41 +02:00
|
|
|
function queryMatches(entity) {
|
|
|
|
if (!query) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-07 12:39:52 +02:00
|
|
|
return _.some(allowedFilterFields, function (field) {
|
|
|
|
var value = entity[field];
|
2016-06-07 12:45:23 +02:00
|
|
|
if (_.isNumber(value)) {
|
|
|
|
value = value.toString();
|
|
|
|
}
|
|
|
|
|
2016-06-07 12:39:52 +02:00
|
|
|
if (!_.isString(value) || _.isEmpty(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _.toLower(value);
|
|
|
|
if (field === 'mac') {
|
|
|
|
return _.includes(value.replace(/:/g, ''), query.replace(/:/g, ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.includes(value, query);
|
2016-07-23 11:30:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var filters = restParams.filters;
|
|
|
|
|
|
|
|
function filtersMatch(entity) {
|
|
|
|
if (_.isEmpty(filters)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.every(filters, function (value, key) {
|
|
|
|
if (_.isUndefined(value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_.startsWith(key, 'has')) {
|
|
|
|
var entityKey = key.substr(3, 1).toLowerCase() + key.substr(4);
|
|
|
|
return _.isEmpty(entity[entityKey]).toString() !== value;
|
|
|
|
}
|
|
|
|
return entity[key] === value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.filter(entities, function (entity) {
|
|
|
|
return queryMatches(entity) && filtersMatch(entity);
|
2016-06-07 12:39:52 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-06-07 11:58:29 +02:00
|
|
|
sort: function (entities, allowedSortFields, restParams) {
|
2016-06-07 12:39:52 +02:00
|
|
|
var sortField = _.includes(allowedSortFields, restParams._sortField) ? restParams._sortField : undefined;
|
2016-06-07 11:58:29 +02:00
|
|
|
if (!sortField) {
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sorted = _.sortBy(entities, [sortField]);
|
|
|
|
|
|
|
|
return restParams._sortDir === 'ASC' ? sorted : _.reverse(sorted);
|
|
|
|
},
|
|
|
|
|
|
|
|
getPageEntities: function (entities, restParams) {
|
|
|
|
var page = restParams._page;
|
|
|
|
var perPage = restParams._perPage;
|
|
|
|
|
|
|
|
return entities.slice((page - 1) * perPage, page * perPage);
|
|
|
|
},
|
|
|
|
|
2016-06-26 13:06:58 +02:00
|
|
|
whereCondition: filterCondition,
|
|
|
|
|
2016-06-11 12:08:50 +02:00
|
|
|
filterClause: function (restParams, defaultSortField, allowedSortFields, filterFields) {
|
|
|
|
var orderBy = orderByClause(
|
|
|
|
restParams,
|
|
|
|
defaultSortField,
|
|
|
|
allowedSortFields
|
|
|
|
);
|
|
|
|
var limitOffset = limitOffsetClause(restParams);
|
|
|
|
|
|
|
|
var filter = filterCondition(
|
|
|
|
restParams,
|
|
|
|
filterFields
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
query: filter.query + ' ' + orderBy.query + ' ' + limitOffset.query,
|
|
|
|
params: _.concat(filter.params, orderBy.params, limitOffset.params)
|
2017-05-06 17:31:34 +02:00
|
|
|
};
|
2016-06-11 12:08:50 +02:00
|
|
|
},
|
|
|
|
|
2016-05-18 22:50:06 +02:00
|
|
|
success: function (res, data) {
|
2016-06-07 14:08:04 +02:00
|
|
|
respond(res, 200, data, 'json');
|
|
|
|
},
|
|
|
|
|
|
|
|
successHtml: function (res, html) {
|
|
|
|
respond(res, 200, html, 'html');
|
2016-05-18 22:50:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
error: function (res, err) {
|
2016-06-07 14:08:04 +02:00
|
|
|
respond(res, err.type.code, err.data, 'json');
|
2016-05-18 22:50:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|