node.js - Conditionally updating / deleting a document in backend. Should I? Do I assign a PUT, POST, or DELETE http method? -
the below function "collection model".
given conditions, want either delete or update document.
should use put or delete?
i separate them , check validation / conditions on client, seems less dry.
i'll have add additional lines of code both client , server achieve same thing.
collectionschema.statics.kill = function(o, cb) { return collection .findasync(o) .then(function(existing) { if (existing.length && (existing[0].vote !== 0 || existing[0].favorite == true)) { return collection .findbyidandupdate(o, { collected: !existing[0].collected }, { new: true }) .then(function(updated) { console.log('updated ', updated) return updated; }).catch(function(err) { return err; }) } else if (existing.length && existing[0].vote == 0 && existing[0].favorite == false) { return collection .removeasync({ _id: existing[0]._id }) .then(function(deleted) { return { message: 'deleted collection' } }).catch(function(err) { return err; }) } }).catch(function(err) { return err; }) };
i think best option use simple rest model (a put
update document , delete
remove document).
then, client, call either update or remove method depending on conditions, say. improve concept, can send negative response client concerning delete
event if document can't removed.
Comments
Post a Comment