ffffng/server/resources/nodeResource.js

98 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-05-12 20:08:19 +02:00
'use strict';
angular.module('ffffng').factory('NodeResource', function (
Constraints,
Validator,
NodeService,
_,
Strings,
Resources,
2014-05-12 20:08:19 +02:00
ErrorTypes
) {
var nodeFields = ['hostname', 'key', 'email', 'nickname', 'mac', 'coords', 'monitoring'];
2014-05-12 20:08:19 +02:00
function getValidNodeData(reqData) {
var node = {};
_.each(nodeFields, function (field) {
var value = Strings.normalizeString(reqData[field]);
if (field === 'mac') {
value = Strings.normalizeMac(value);
}
node[field] = value;
});
return node;
}
var isValidNode = Validator.forConstraints(Constraints.node);
var isValidToken = Validator.forConstraint(Constraints.token);
return {
create: function (req, res) {
var data = Resources.getData(req);
2014-05-12 20:08:19 +02:00
var node = getValidNodeData(data);
if (!isValidNode(node)) {
return Resources.error(res, {data: 'Invalid node data.', type: ErrorTypes.badRequest});
2014-05-12 20:08:19 +02:00
}
return NodeService.createNode(node, function (err, token, node) {
if (err) {
return Resources.error(res, err);
2014-05-12 20:08:19 +02:00
}
return Resources.success(res, {token: token, node: node});
2014-05-12 20:08:19 +02:00
});
},
update: function (req, res) {
var data = Resources.getData(req);
2014-05-12 20:08:19 +02:00
var token = Strings.normalizeString(data.token);
if (!isValidToken(token)) {
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
2014-05-12 20:08:19 +02:00
}
var node = getValidNodeData(data);
if (!isValidNode(node)) {
return Resources.error(res, {data: 'Invalid node data.', type: ErrorTypes.badRequest});
2014-05-12 20:08:19 +02:00
}
return NodeService.updateNode(token, node, function (err, token, node) {
if (err) {
return Resources.error(res, err);
2014-05-12 20:08:19 +02:00
}
return Resources.success(res, {token: token, node: node});
2014-05-12 20:08:19 +02:00
});
},
2016-05-16 18:27:03 +02:00
delete: function (req, res) {
var data = Resources.getData(req);
2016-05-16 18:27:03 +02:00
var token = Strings.normalizeString(data.token);
if (!isValidToken(token)) {
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
2016-05-16 18:27:03 +02:00
}
return NodeService.deleteNode(token, function (err) {
if (err) {
return Resources.error(res, err);
2016-05-16 18:27:03 +02:00
}
return Resources.success(res, {});
2016-05-16 18:27:03 +02:00
});
},
2014-05-12 20:08:19 +02:00
get: function (req, res) {
var token = Strings.normalizeString(Resources.getData(req).token);
2014-05-12 20:08:19 +02:00
if (!isValidToken(token)) {
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
2014-05-12 20:08:19 +02:00
}
return NodeService.getNodeDataByToken(token, function (err, node) {
2014-05-12 20:08:19 +02:00
if (err) {
return Resources.error(res, err);
2014-05-12 20:08:19 +02:00
}
return Resources.success(res, node);
2014-05-12 20:08:19 +02:00
});
}
};
});