2016-02-22 18:08:58 -05:00
|
|
|
errors = require '../commons/errors'
|
|
|
|
_ = require 'lodash'
|
|
|
|
Promise = require 'bluebird'
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
|
|
|
getLimitFromReq: (req, options) ->
|
|
|
|
options = _.extend({
|
|
|
|
max: 1000
|
|
|
|
default: 100
|
2016-03-30 16:57:19 -04:00
|
|
|
param: 'limit'
|
2016-02-22 18:08:58 -05:00
|
|
|
}, options)
|
|
|
|
|
|
|
|
limit = options.default
|
|
|
|
|
2016-03-30 16:57:19 -04:00
|
|
|
if req.query[options.param]
|
|
|
|
limit = parseInt(req.query[options.param])
|
2016-02-22 18:08:58 -05:00
|
|
|
valid = tv4.validate(limit, {
|
|
|
|
type: 'integer'
|
|
|
|
maximum: options.max
|
|
|
|
minimum: 1
|
|
|
|
})
|
|
|
|
if not valid
|
2016-03-30 16:57:19 -04:00
|
|
|
throw new errors.UnprocessableEntity("Invalid #{options.param} parameter.")
|
2016-02-22 18:08:58 -05:00
|
|
|
|
|
|
|
return limit
|
|
|
|
|
|
|
|
|
|
|
|
getSkipFromReq: (req, options) ->
|
|
|
|
options = _.extend({
|
|
|
|
max: 1000000
|
|
|
|
default: 0
|
2016-03-30 16:57:19 -04:00
|
|
|
param: 'skip'
|
2016-02-22 18:08:58 -05:00
|
|
|
}, options)
|
|
|
|
|
|
|
|
skip = options.default
|
|
|
|
|
2016-03-30 16:57:19 -04:00
|
|
|
if req.query[options.param]
|
|
|
|
skip = parseInt(req.query[options.param])
|
2016-02-22 18:08:58 -05:00
|
|
|
valid = tv4.validate(skip, {
|
|
|
|
type: 'integer'
|
|
|
|
maximum: options.max
|
|
|
|
minimum: 0
|
|
|
|
})
|
|
|
|
if not valid
|
2016-03-30 16:57:19 -04:00
|
|
|
throw new errors.UnprocessableEntity("Invalid #{options.param} parameter.")
|
2016-02-22 18:08:58 -05:00
|
|
|
|
|
|
|
return skip
|
|
|
|
|
|
|
|
|
|
|
|
getProjectFromReq: (req, options) ->
|
|
|
|
options = _.extend({}, options)
|
|
|
|
return null unless req.query.project
|
2016-08-08 12:35:42 -04:00
|
|
|
result = {}
|
|
|
|
project = req.query.project
|
|
|
|
if project is 'true'
|
|
|
|
result = {original: 1, name: 1, version: 1, description: 1, slug: 1, kind: 1, created: 1, permissions: 1}
|
2016-02-22 18:08:58 -05:00
|
|
|
else
|
2016-08-08 12:35:42 -04:00
|
|
|
if _.isString(project)
|
|
|
|
project = project.split(',')
|
|
|
|
for field in project
|
|
|
|
result[field] = 1
|
2016-02-22 18:08:58 -05:00
|
|
|
|
2016-08-08 12:35:42 -04:00
|
|
|
return result
|