Commit 07163a47 authored by Tristan Cavelier's avatar Tristan Cavelier

local storage post generates uuid if id is not provided

parent 79fd7311
...@@ -41,6 +41,26 @@ jIO.addStorageType('local', function (spec, my) { ...@@ -41,6 +41,26 @@ jIO.addStorageType('local', function (spec, my) {
priv.application_name; priv.application_name;
// ==================== Tools ==================== // ==================== Tools ====================
/**
* Generate a new uuid
* @method generateUuid
* @return {string} The new uuid
*/
priv.generateUuid = function () {
var S4 = function () {
/* 65536 */
var i, string = Math.floor(
Math.random() * 0x10000
).toString(16);
for (i = string.length; i < 4; i += 1) {
string = '0' + string;
}
return string;
};
return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() +
S4() + S4();
};
/** /**
* Update [doc] the document object and remove [doc] keys * Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting * which are not in [new_doc]. It only changes [doc] keys not starting
...@@ -108,25 +128,18 @@ jIO.addStorageType('local', function (spec, my) { ...@@ -108,25 +128,18 @@ jIO.addStorageType('local', function (spec, my) {
*/ */
that.post = function (command) { that.post = function (command) {
setTimeout(function () { setTimeout(function () {
var doc = command.getDocId(); var doc, doc_id = command.getDocId();
if (!(typeof doc === "string" && doc !== "")) { if (!doc_id) {
that.error({ doc_id = priv.generateUuid();
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return;
} }
doc = localstorage.getItem(priv.localpath + "/" + doc); doc = localstorage.getItem(priv.localpath + "/" + doc_id);
if (doc === null) { if (doc === null) {
// the document does not exist // the document does not exist
localstorage.setItem(priv.localpath + "/" + command.getDocId(), localstorage.setItem(priv.localpath + "/" + doc_id,
command.cloneDoc()); command.cloneDoc());
that.success({ that.success({
"ok": true, "ok": true,
"id": command.getDocId() "id": doc_id
}); });
} else { } else {
// the document already exists // the document already exists
......
...@@ -768,8 +768,14 @@ test ("Post", function(){ ...@@ -768,8 +768,14 @@ test ("Post", function(){
}); });
// post without id // post without id
o.spy (o, "status", 405, "Post without id"); o.spy (o, "jobstatus", "done", "Post without id");
o.jio.post({}, o.f); o.jio.post({}, function (err, response) {
var uuid;
o.f(err, response);
uuid = (err || response).id;
ok(isUuid(uuid), "Uuid should look like " +
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx : " + uuid);
});
o.tick(o); o.tick(o);
// post non empty document // post non empty document
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment