Added a handler for fetching names of versioned docs by originals.

This commit is contained in:
Scott Erickson 2014-04-28 14:31:11 -07:00
parent 0346039683
commit 32ba4a8f75
2 changed files with 32 additions and 0 deletions
server

View file

@ -101,6 +101,37 @@ module.exports = class Handler
return @setWatching(req, res, args[0]) if req.route.method is 'put' and args[1] is 'watch'
return @sendNotFoundError(res)
getNamesByIDs: (req, res) ->
ids = req.query.ids or req.body.ids
if @modelClass.schema.uses_coco_versions
return @getNamesByOriginals(req, res)
@getPropertiesFromMultipleDocuments res, User, "name", ids
getNamesByOriginals: (req, res) ->
ids = req.query.ids or req.body.ids
ids = ids.split(',') if _.isString ids
ids = _.uniq ids
project = {name:1}
sort = {'version.major':-1, 'version.minor':-1}
makeFunc = (id) =>
(callback) =>
criteria = {original:mongoose.Types.ObjectId(id)}
@modelClass.findOne(criteria, project).sort(sort).exec (err, document) ->
return done(err) if err
callback(null, document?.toObject() or {})
funcs = {}
for id in ids
return errors.badInput(res, "Given an invalid id: #{id}") unless Handler.isID(id)
funcs[id] = makeFunc(id)
async.parallel funcs, (err, results) ->
return errors.serverError err if err
res.send results
res.end()
getPatchesFor: (req, res, id) ->
query = { 'target.original': mongoose.Types.ObjectId(id), status: req.query.status or 'pending' }
Patch.find(query).sort('-created').exec (err, patches) =>

View file

@ -35,6 +35,7 @@ module.exports.setup = (app) ->
return handler.versions(req, res, parts[1]) if parts[2] is 'versions'
return handler.files(req, res, parts[1]) if parts[2] is 'files'
return handler.search(req, res) if req.route.method is 'get' and parts[1] is 'search'
return handler.getNamesByIDs(req, res) if req.route.method in ['get', 'post'] and parts[1] is 'names'
return handler.getByRelationship(req, res, parts[1..]...) if parts.length > 2
return handler.getById(req, res, parts[1]) if req.route.method is 'get' and parts[1]?
return handler.patch(req, res, parts[1]) if req.route.method is 'patch' and parts[1]?