Skip to content

Commit

Permalink
Merge pull request HabitRPG#707 from switz/api-4
Browse files Browse the repository at this point in the history
@lefnire => Array API
  • Loading branch information
lefnire committed Mar 22, 2013
2 parents 2a89ba6 + 7591846 commit 381bd6e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/server/api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ router.delete '/user/task/:id', auth, validateTask, (req, res) ->

res.send 204

###
POST /user/tasks
###
router.post '/user/tasks', auth, (req, res) ->
for idx, task of req.body
if task.id
if task.del
req.user.del "tasks.#{task.id}"
task = deleted: true
else
req.user.set "tasks.#{task.id}", task
else
model = req.getModel()
type = task.type || 'habit'
model.ref '_user', req.user
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
model.at("_#{type}List").push task
req.body[idx] = task

res.json 201, req.body


###
POST /user/task/
###
Expand Down
38 changes: 38 additions & 0 deletions test/api.mocha.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,41 @@ describe 'API', ->
query.fetch (err, user) ->
expect(user.get("tasks.#{tid}.completed")).to.be true
done()

it 'POST /api/v1/user/task (array)', (done) ->
habitId = currentUser.habitIds[0]
dailyId = currentUser.dailyIds[0]
arr = [{
id: habitId
text: 'hello'
notes: 'note'
},{
text: 'new task'
notes: 'notes!'
},{
id: dailyId
del: true
}]

request.post("#{baseURL}/user/tasks")
.set('Accept', 'application/json')
.set('X-API-User', currentUser.id)
.set('X-API-Key', currentUser.apiToken)
.send(arr)
.end (res) ->
expect(res.body.err).to.be undefined
expect(res.statusCode).to.be 201
expect(res.body[0]).to.eql {id: habitId,text: 'hello',notes: 'note'}
expect(res.body[1].id).to.be.a 'string'
expect(res.body[1].text).to.be 'new task'
expect(res.body[1].notes).to.be 'notes!'
expect(res.body[2]).to.eql deleted: true

query = model.query('users').withIdAndToken(currentUser.id, currentUser.apiToken)
query.fetch (err, user) ->
expect(user.get("tasks.#{habitId}")).to.eql {id: habitId,text: 'hello',notes: 'note'}
expect(user.get("tasks.#{dailyId}")).to.be undefined
expect(user.get("tasks.#{res.body[1].id}")).to.eql id: res.body[1].id, text: 'new task', notes: 'notes!'
done()


0 comments on commit 381bd6e

Please sign in to comment.