From 4670cbc60adb192feb6050ca7cfafbb79a67f5a0 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 8 Aug 2011 17:35:40 +0100 Subject: [PATCH] added the listPads and createGroupPad --- node/db/API.js | 50 ++----------------- node/db/GroupManager.js | 99 ++++++++++++++++++++++++++++++++++++++ node/db/PadManager.js | 24 ++++++++- node/handler/APIHandler.js | 4 +- 4 files changed, 128 insertions(+), 49 deletions(-) diff --git a/node/db/API.js b/node/db/API.js index c8f1b9ca..37ed330b 100644 --- a/node/db/API.js +++ b/node/db/API.js @@ -67,10 +67,7 @@ Example returns: {code: 0, message:"ok", data: {padIDs : ["3$test", "3$test2"]} {code: 1, message:"There is no group for this groupID", data: null} */ -exports.listPads = function(groupID, callback) -{ - -} +exports.listPads = groupManager.listPads; /** createGroupPad(groupID, padName [, text]) creates a new pad in this group @@ -81,10 +78,7 @@ Example returns: {code: 1, message:"pad does already exist", data: null} {code: 1, message:"There is no group for this groupID", data: null} */ -exports.createGroupPad = function(groupID, padName, text, callback) -{ - -} +exports.createGroupPad = groupManager.createGroupPad; /**********************/ /**AUTHOR FUNCTIONS****/ @@ -310,15 +304,7 @@ Example returns: {code: 1, message:"text too long", data: null} */ exports.setText = function(padID, text, callback) -{ - //check the text - var textCheck = checkPadText(text); - if(textCheck != null) - { - callback(textCheck); - return; - } - +{ //get the pad getPadSafe(padID, true, function(err, pad) { @@ -372,18 +358,7 @@ Example returns: {code: 1, message:"pad does already exist", data: null} */ exports.createPad = function(padID, text, callback) -{ - if(text) - { - //check the text - var textCheck = checkPadText(text); - if(textCheck != null) - { - callback(textCheck); - return; - } - } - +{ //ensure there is no $ in the padID if(padID.indexOf("$") != -1) { @@ -550,23 +525,6 @@ function is_int(value) return (parseFloat(value) == parseInt(value)) && !isNaN(value) } -function checkPadText(text) -{ - //check if text is a string - if(typeof text != "string") - { - return {stop: "text is not a string"}; - } - - //check if text is less than 100k chars - if(text.length > 100000) - { - return {stop: "text must be less than 100k chars"}; - } - - return null; -} - //gets a pad safe function getPadSafe(padID, shouldExist, text, callback) { diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js index 2fcd31c8..10e73ed1 100644 --- a/node/db/GroupManager.js +++ b/node/db/GroupManager.js @@ -20,6 +20,7 @@ var db = require("./DB").db; var async = require("async"); +var padManager = require("./PadManager"); exports.doesGroupExist = function(groupID, callback) { @@ -102,6 +103,7 @@ exports.getMappedGroup4 = function(groupMapper, callback) //create the mapper entry for this group db.set("mapper2group:"+groupMapper, responseObj.groupID); + callback(null, responseObj); }); } @@ -113,5 +115,102 @@ exports.getMappedGroup4 = function(groupMapper, callback) }); } +exports.createGroupPad = function(groupID, padName, text, callback) +{ + //create the padID + var padID = groupID + "$" + padName; + async.series([ + //ensure group exists + function (callback) + { + exports.doesGroupExist(groupID, function(err, exists) + { + //error + if(err) + { + callback(err); + } + //group does not exist + else if(exists == false) + { + callback({stop: "groupID does not exist"}); + } + //group exists, everything is fine + else + { + callback(); + } + }); + }, + //ensure pad does not exists + function (callback) + { + padManager.doesPadExists(padID, function(err, exists) + { + //error + if(err) + { + callback(err); + } + //pad exists already + else if(exists == true) + { + callback({stop: "padName does already exist"}); + } + //pad does not exist, everything is fine + else + { + callback(); + } + }); + }, + //create the pad + function (callback) + { + padManager.getPad(padID, text, function(err) + { + callback(err); + }); + }, + //create an entry in the group for this pad + function (callback) + { + db.setSub("group:" + groupID, ["pads", padID], 1); + callback(); + } + ], function(err) + { + callback(err, {padID: padID}); + }); + //check if groupID exists + //check if pad already exists + //create the pad + //create the subentry in the padobject +} + +exports.listPads = function(groupID, callback) +{ + exports.doesGroupExist(groupID, function(err, exists) + { + //error + if(err) + { + callback(err); + } + //group does not exist + else if(exists == false) + { + callback({stop: "groupID does not exist"}); + } + //group exists, let's get the pads + else + { + db.getSub("group:" + groupID, ["pads"], function(err, pads) + { + callback(err, {padIDs: pads}); + }); + } + }); +} diff --git a/node/db/PadManager.js b/node/db/PadManager.js index c1b69bd5..f0ce8818 100644 --- a/node/db/PadManager.js +++ b/node/db/PadManager.js @@ -33,8 +33,12 @@ globalPads = []; */ exports.getPad = function(id, text, callback) { + //check if this is a valid padId if(!exports.isValidPadId(id)) - throw new Error(id + " is not a valid padId"); + { + callback({stop: id + " is not a valid padId"}); + return; + } //make text an optional parameter if(typeof text == "function") @@ -43,6 +47,24 @@ exports.getPad = function(id, text, callback) text = null; } + //check if this is a valid text + if(text != null) + { + //check if text is a string + if(typeof text != "string") + { + callback({stop: "text is not a string"}); + return; + } + + //check if text is less than 100k chars + if(text.length > 100000) + { + callback({stop: "text must be less than 100k chars"}); + return; + } + } + var pad = globalPads[id]; //return pad if its already loaded diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index f91e92ab..2d7d9ae6 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -38,9 +38,9 @@ var functions = { "createGroup" : [], "getMappedGroup4" : ["groupMapper"], // "deleteGroup" : ["groupID"], -// "listPads" : ["groupID"], + "listPads" : ["groupID"], "createPad" : ["padID", "text"], -// "createGroupPad" : ["groupID", "padName", "text"], + "createGroupPad" : ["groupID", "padName", "text"], // "createAuthor" : ["name"], // "getMappedAuthor4" : ["authorMapper" , "name"], // "createSession" : ["groupID", "authorID", "validUntil"],