Added delete feature for nodes.

This commit is contained in:
baldo 2016-05-16 18:27:03 +02:00
parent 39e7af6238
commit 79aadc85c2
20 changed files with 300 additions and 20 deletions

View file

@ -80,6 +80,22 @@ angular.module('ffffng').factory('NodeResource', function (
});
},
delete: function (req, res) {
var data = getData(req);
var token = Strings.normalizeString(data.token);
if (!isValidToken(token)) {
return error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
}
return NodeService.deleteNode(token, function (err) {
if (err) {
return error(res, err);
}
return success(res, {});
});
},
get: function (req, res) {
var token = Strings.normalizeString(getData(req).token);
if (!isValidToken(token)) {

View file

@ -5,6 +5,7 @@ angular.module('ffffng').factory('Router', function (app, NodeResource) {
init: function () {
app.post('/api/node', NodeResource.create);
app.put('/api/node/:token', NodeResource.update);
app.delete('/api/node/:token', NodeResource.delete);
app.get('/api/node/:token', NodeResource.get);
}
};

View file

@ -101,6 +101,23 @@ angular.module('ffffng')
return callback(null, token, node);
}
function deleteNodeFile(token, callback) {
var files = findNodeFiles('*@*@*@' + token);
if (files.length !== 1) {
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
}
try {
fs.unlinkSync(files[0]);
}
catch (error) {
console.log(error);
return callback({data: 'Could not delete node.', type: ErrorTypes.internalError});
}
return callback(null);
}
function parseNodeFile(file, callback) {
var lines = fs.readFileSync(file).toString();
@ -140,6 +157,10 @@ angular.module('ffffng')
writeNodeFile(true, token, node, callback);
},
deleteNode: function (token, callback) {
deleteNodeFile(token, callback);
},
getNodeData: function (token, callback) {
var files = findNodeFiles('*@*@*@' + token);