From e771542961459f0fb43ebfb2d43c11a89b572775 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 22 Aug 2011 13:11:34 +0100 Subject: [PATCH] added deleteGroup API function --- node/db/GroupManager.js | 89 ++++++++++++++++++++++++++++++++++++++ node/handler/APIHandler.js | 2 +- static/tests.html | 1 + 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js index 995e6c93..1598e72e 100644 --- a/node/db/GroupManager.js +++ b/node/db/GroupManager.js @@ -21,6 +21,95 @@ var db = require("./DB").db; var async = require("async"); var padManager = require("./PadManager"); +var sessionManager = require("./SessionManager"); + +exports.deleteGroup = function(groupID, callback) +{ + var group; + + async.series([ + //ensure group exists + function (callback) + { + //try to get the group entry + db.get("group:" + groupID, function (err, _group) + { + //error + if(err) + { + callback(err); + } + //group does not exist + else if(_group == null) + { + callback({stop: "groupID does not exist"}); + } + //group exists, everything is fine + else + { + group = _group; + callback(); + } + }); + }, + //iterate trough all pads of this groups and delete them + function(callback) + { + //collect all padIDs in an array, that allows us to use async.forEach + var padIDs = []; + for(var i in group.pads) + { + padIDs.push(i); + } + + //loop trough all pads and delete them + async.forEach(padIDs, function(padID, callback) + { + padManager.getPad(padID, function(err, pad) + { + if(err) {callback(err); return} + + pad.remove(callback); + }); + }, callback); + }, + //iterate trough group2sessions and delete all sessions + function(callback) + { + //try to get the group entry + db.get("group2sessions:" + groupID, function (err, group2sessions) + { + if(err) {callback(err); return} + + //skip if there is no group2sessions entry + if(group2sessions == null) {callback(); return} + + //collect all sessions in an array, that allows us to use async.forEach + var sessions = []; + for(var i in group2sessions.sessionsIDs) + { + sessions.push(i); + } + + //loop trough all sessions and delete them + async.forEach(sessions, function(session, callback) + { + sessionManager.deleteSession(session, callback); + }, callback); + }); + }, + //remove group and group2sessions entry + function(callback) + { + db.remove("group2sessions:" + groupID); + db.remove("group:" + groupID); + callback(); + } + ], function(err) + { + callback(err); + }); +} exports.doesGroupExist = function(groupID, callback) { diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index 7292c62c..34d63f18 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -37,7 +37,7 @@ catch(e) var functions = { "createGroup" : [], "createGroupIfNotExistsFor" : ["groupMapper"], -// "deleteGroup" : ["groupID"], + "deleteGroup" : ["groupID"], "listPads" : ["groupID"], "createPad" : ["padID", "text"], "createGroupPad" : ["groupID", "padName", "text"], diff --git a/static/tests.html b/static/tests.html index f15f4567..bd5bc578 100644 --- a/static/tests.html +++ b/static/tests.html @@ -140,6 +140,7 @@
createGroup()
+
deleteGroup(groupID)
createGroupIfNotExistsFor(groupMapper)
listPads(groupID)
createPad(padID,text)