mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 09:35:39 -05:00
Make POST /db/classroom/:id/members use update to avoid race conditions
This commit is contained in:
parent
6de6479f3b
commit
f57cf3a83d
2 changed files with 8 additions and 4 deletions
|
@ -57,10 +57,11 @@ ClassroomHandler = class ClassroomHandler extends Handler
|
||||||
members = _.clone(classroom.get('members'))
|
members = _.clone(classroom.get('members'))
|
||||||
if _.any(members, (memberID) -> memberID.equals(req.user.get('_id')))
|
if _.any(members, (memberID) -> memberID.equals(req.user.get('_id')))
|
||||||
return @sendSuccess(res, @formatEntity(req, classroom))
|
return @sendSuccess(res, @formatEntity(req, classroom))
|
||||||
|
update = { $push: { members : req.user.get('_id')}}
|
||||||
|
classroom.update update, (err) =>
|
||||||
|
return @sendDatabaseError(res, err) if err
|
||||||
members.push req.user.get('_id')
|
members.push req.user.get('_id')
|
||||||
classroom.set('members', members)
|
classroom.set('members', members)
|
||||||
classroom.save (err, classroom) =>
|
|
||||||
return @sendDatabaseError(res, err) if err
|
|
||||||
return @sendSuccess(res, @formatEntity(req, classroom))
|
return @sendSuccess(res, @formatEntity(req, classroom))
|
||||||
|
|
||||||
formatEntity: (req, doc) ->
|
formatEntity: (req, doc) ->
|
||||||
|
|
|
@ -123,12 +123,15 @@ describe 'POST /db/classroom/:id/members', ->
|
||||||
data = { name: 'Classroom 5' }
|
data = { name: 'Classroom 5' }
|
||||||
request.post {uri: classroomsURL, json: data }, (err, res, body) ->
|
request.post {uri: classroomsURL, json: data }, (err, res, body) ->
|
||||||
classroomCode = body.code
|
classroomCode = body.code
|
||||||
|
classroomID = body._id
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
loginNewUser (user2) ->
|
loginNewUser (user2) ->
|
||||||
url = getURL("/db/classroom/~/members")
|
url = getURL("/db/classroom/~/members")
|
||||||
data = { code: classroomCode }
|
data = { code: classroomCode }
|
||||||
request.post { uri: url, json: data }, (err, res, body) ->
|
request.post { uri: url, json: data }, (err, res, body) ->
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
|
Classroom.findById classroomID, (err, classroom) ->
|
||||||
|
expect(classroom.get('members').length).toBe(1)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue