Array to /user/tasks

This commit is contained in:
Daniel Saewitz 2013-03-22 16:52:33 -04:00
parent b56bc6b1d9
commit 355dde07f4
2 changed files with 61 additions and 1 deletions

View file

@ -74,7 +74,7 @@ router.get '/user/task/:id', auth, (req, res) ->
###
validateTask = (req, res, next) ->
task = {}
newTask = { type, text, notes, value, up, down, completed } = req.body
newTask = { type, text, notes, value, up, down, completed } = req.task || req.body
# If we're updating, get the task from the user
if req.method is 'PUT' or req.method is 'DELETE'
@ -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/
###

View file

@ -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()