[TASK] Remove POST from /profile API.

Also, the commit before contained a lot of code
that was completely wrong.
This commit is contained in:
Jan Philipp Timme 2013-09-17 16:12:18 +02:00
parent 87f08fc8e5
commit 254274da56
2 changed files with 31 additions and 88 deletions

1
NOTES
View File

@ -58,7 +58,6 @@ DELETE /user {} {success}
GET /profile {} {success, profile: {%PROFILE_DOCUMENT}} GET /profile {} {success, profile: {%PROFILE_DOCUMENT}}
PUT /profile {profile} {success} PUT /profile {profile} {success}
POST /profile {profile} {success}
DELETE /profile {} {success} DELETE /profile {} {success}
GET /session {} {success, login} GET /session {} {success, login}

View File

@ -18,55 +18,13 @@ var constructor = function(db) {
//overwrite existing profile data //overwrite existing profile data
if(req.method == "PUT") { if(req.method == "PUT") {
var params = req.body; var params = req.body;
if(tools.reqParamsGiven(["data"], params) == false) { if(tools.reqParamsGiven(["profile"], params) == false) {
res.send(200, JSON.stringify({ res.send(200, JSON.stringify({
"success": false, "success": false,
"error": "This method needs a data parameter!" "error": "This method needs a profile parameter!"
})); }));
return; 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 = { var changeset = {
"data": req.body.profile "data": req.body.profile
}; };
@ -85,56 +43,42 @@ var constructor = function(db) {
}); });
} }
if(req.method == "DELETE") { if(req.method == "GET") {
//check if user document exists db.get(req.session.data.user.profile, function(err, doc) {
db.get(req.session.data.user._id, function (err, doc) { if(err) {
if(err && err.error == "not_found") {
console.log(err); console.log(err);
res.send(200, JSON.stringify({ res.send(200, JSON.stringify({
"success": false, "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
})); }));
return;
} }
var userDocument = doc; });
db.remove(userDocument._id, userDocument._rev, function(err, result) { }
if(req.method == "DELETE") {
var changeset = {
"data": {}
};
db.merge(req.session.data.user.profile, changeset, function(err, result) {
if(err) { if(err) {
console.log(err); console.log(err);
res.send(200, JSON.stringify({ res.send(200, JSON.stringify({
"success": false, "success": false,
"error": "Could not delete user document!" "error": "Something went wrong updating the profile document!"
})); }));
} else { } 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({ res.send(200, JSON.stringify({
"success": true "success": true
})); }));
} }
}); });
} }
});
}
});
});
}
}; };
return handler; return handler;