From 254274da5648eb4a8d2f5f27f7a6cac6c3af7dd4 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 17 Sep 2013 16:12:18 +0200 Subject: [PATCH] [TASK] Remove POST from /profile API. Also, the commit before contained a lot of code that was completely wrong. --- NOTES | 1 - src/Profile.api.js | 118 ++++++++++++--------------------------------- 2 files changed, 31 insertions(+), 88 deletions(-) diff --git a/NOTES b/NOTES index d01e764..5a7f0da 100644 --- a/NOTES +++ b/NOTES @@ -58,7 +58,6 @@ DELETE /user {} {success} GET /profile {} {success, profile: {%PROFILE_DOCUMENT}} PUT /profile {profile} {success} -POST /profile {profile} {success} DELETE /profile {} {success} GET /session {} {success, login} diff --git a/src/Profile.api.js b/src/Profile.api.js index 53345f2..95f955c 100644 --- a/src/Profile.api.js +++ b/src/Profile.api.js @@ -18,55 +18,13 @@ var constructor = function(db) { //overwrite existing profile data if(req.method == "PUT") { var params = req.body; - if(tools.reqParamsGiven(["data"], params) == false) { + if(tools.reqParamsGiven(["profile"], params) == false) { res.send(200, JSON.stringify({ "success": false, - "error": "This method needs a data parameter!" + "error": "This method needs a profile parameter!" })); return; } - //check if user already exists - db.get(params.username, function (err, doc) { - if(!err || err.error != "not_found") { - res.send(200, JSON.stringify({ - "success": false, - "error": "Username already taken!" - })); - return; - } - scrypt.passwordHash(params.password, 10, function(err, pwHash) { - var userDoc = { - "_id": params.username, - "auth": pwHash, - "email": params.email, - "type": "user" - }; - db.save(userDoc._id, userDoc, function(err, result) { - if(err) { - console.log(err) - res.send(200, JSON.stringify({ - "success": false, - "error": "Could not create user document!" - })); - } else { - res.send(200, JSON.stringify({ - "success": true - })); - } - }); - }); - }); - } - - if(req.method == "GET") { - res.send(200, JSON.stringify({ - "success": true, - "profile": req.session.data.profile - })); - } - - if(req.method == "POST") { - var params = req.body; var changeset = { "data": req.body.profile }; @@ -85,54 +43,40 @@ var constructor = function(db) { }); } - if(req.method == "DELETE") { - //check if user document exists - db.get(req.session.data.user._id, function (err, doc) { - if(err && err.error == "not_found") { + if(req.method == "GET") { + db.get(req.session.data.user.profile, function(err, doc) { + if(err) { console.log(err); res.send(200, JSON.stringify({ "success": false, - "error": "User document does not exist!" + "error": "Something went wrong reading the profile document!" + })); + } else { + req.session.data.profile = doc.data; + res.send(200, JSON.stringify({ + "success": true, + "profile": req.session.data.profile + })); + } + }); + } + + if(req.method == "DELETE") { + var changeset = { + "data": {} + }; + db.merge(req.session.data.user.profile, changeset, function(err, result) { + if(err) { + console.log(err); + res.send(200, JSON.stringify({ + "success": false, + "error": "Something went wrong updating the profile document!" + })); + } else { + res.send(200, JSON.stringify({ + "success": true })); - return; } - var userDocument = doc; - db.remove(userDocument._id, userDocument._rev, function(err, result) { - if(err) { - console.log(err); - res.send(200, JSON.stringify({ - "success": false, - "error": "Could not delete user document!" - })); - } else { - db.get(userDocument.profile, function(err, doc) { - if(err) { - console.log(err); - res.send(200, JSON.stringify({ - "success": false, - "error": "Could not fetch profile document!" - })); - } else { - var profileDocument = doc; - db.remove(profileDocument._id, profileDocument._rev, function(err, result) { - if(err) { - console.log(err); - res.send(200, JSON.stringify({ - "success": false, - "error": "Could not delete profile document!" - })); - } else { - //kill session data, too - delete req.session; - res.send(200, JSON.stringify({ - "success": true - })); - } - }); - } - }); - } - }); }); } };