mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 09:35:39 -05:00
Add GET /db/classroom by member and GET /db/classroom/:id/members
This commit is contained in:
parent
9577236263
commit
6de6479f3b
1 changed files with 18 additions and 0 deletions
|
@ -5,6 +5,7 @@ Classroom = require './Classroom'
|
|||
User = require '../users/User'
|
||||
sendwithus = require '../sendwithus'
|
||||
utils = require '../lib/utils'
|
||||
UserHandler = require '../users/user_handler'
|
||||
|
||||
ClassroomHandler = class ClassroomHandler extends Handler
|
||||
modelClass: Classroom
|
||||
|
@ -35,8 +36,19 @@ ClassroomHandler = class ClassroomHandler extends Handler
|
|||
method = req.method.toLowerCase()
|
||||
return @inviteStudents(req, res, args[0]) if args[1] is 'invite-members'
|
||||
return @joinClassroomAPI(req, res, args[0]) if method is 'post' and args[1] is 'members'
|
||||
return @getMembersAPI(req, res, args[0]) if args[1] is 'members'
|
||||
super(arguments...)
|
||||
|
||||
getMembersAPI: (req, res, classroomID) ->
|
||||
Classroom.findById classroomID, (err, classroom) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
return @sendNotFoundError(res) unless classroom
|
||||
memberIDs = classroom.get('members') ? []
|
||||
User.find {_id: {$in: memberIDs}}, (err, users) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
cleandocs = (UserHandler.formatEntity(req, doc) for doc in users)
|
||||
@sendSuccess(res, cleandocs)
|
||||
|
||||
joinClassroomAPI: (req, res, classroomID) ->
|
||||
return @sendBadInputError(res, 'Need an object with a code') unless req.body?.code
|
||||
Classroom.findOne {code: req.body.code}, (err, classroom) =>
|
||||
|
@ -84,6 +96,12 @@ ClassroomHandler = class ClassroomHandler extends Handler
|
|||
Classroom.find {ownerID: mongoose.Types.ObjectId(ownerID)}, (err, classrooms) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
return @sendSuccess(res, (@formatEntity(req, classroom) for classroom in classrooms))
|
||||
else if memberID = req.query.memberID
|
||||
return @sendForbiddenError(res) unless req.user and (req.user.isAdmin() or memberID is req.user.id)
|
||||
return @sendBadInputError(res, 'Bad memberID') unless utils.isID memberID
|
||||
Classroom.find {members: mongoose.Types.ObjectId(memberID)}, (err, classrooms) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
return @sendSuccess(res, (@formatEntity(req, classroom) for classroom in classrooms))
|
||||
else
|
||||
super(arguments...)
|
||||
|
||||
|
|
Loading…
Reference in a new issue