add some api

This commit is contained in:
yangit 2013-06-14 23:44:13 +08:00
parent 268e377dc2
commit 236a81d392

View file

@ -10,8 +10,10 @@ check = validator.check
sanitize = validator.sanitize
misc = require '../app/misc'
NO_TOKEN_OR_UID = err: "You must include a token and uid (user id) in your request"
NO_USER_FOUND = err: "No user found."
NO_TOKEN_OR_UID =
err: "You must include a token and uid (user id) in your request"
NO_USER_FOUND =
err: "No user found."
# ---------- /api/v1 API ------------
# Every url added beneath router is prefaced by /api/v2
@ -47,27 +49,34 @@ POST new actions
router.post '/', auth, (req, res) ->
model = req.getModel()
actions = req.body
console.log util.inspect req.body
if _.isArray actions
actions.forEach (action)->
if action.op == "score"
if action.task.type=="daily" || action.task.type=="todo"
# flip completed state. Since checkbox is not binded to model unlike when you click through Derby website.
completed = model.get "tasks[#{action.task.id}].completed"
model.set("tasks[#{action.task.id}].completed", !completed)
misc.score(model, action.task.id, action.dir, true)
if action.op=="score"
misc.score(model, action.task, action.dir, true)
if action.op == "newTask"
req.user.set "tasks.#{req.task.id}", action.task
if action.op=="newTask"
req.user.set "tasks.#{req.task.id}", action.task
if action.op=="delTask"
model.del ("tasks."+action.task)
if action.op=="delTask"
model.del ("tasks."+action.task)
# this API is only working with string or number variables. It should return error if object given or object is at the path.
if action.op == "set"
oldValue = model.get(action.path);
if (typeof action.value == "number" || typeof action.value == "string")
if (typeof oldValue == "number" || typeof oldValue == "string")
model.get(action.path, action.value)
# this API is only working with string or number variables. It should return error if object given or object is at the path.
if action.op=="set"
oldValue = model.get(action.path);
if (typeof action.value=="number"||typeof action.value=="string")
if (typeof oldValue=="number"||typeof oldValue=="string")
model.get(action.path,action.value)
console.log util.inspect req.body
res.json 200, req.userObj
#emulate slow\buggy API, TODO, REMOVE THIS PIECE OF CODE!
setTimeout (->
res.json 200, req.userObj
console.log "Reply sent")
, 1000
module.exports = router