Permalink
Browse files

Add sort and filter parameters to /instances/list API endpoint

  • Loading branch information...
TheKinrar committed Nov 4, 2017
1 parent 0373d3a commit 041b2b71afd8b9366743d733390e164ee179ff6f
Showing with 26 additions and 3 deletions.
  1. +25 −2 controllers/api/v1/instances/index.js
  2. +1 −1 helpers/APIUtils.js
@@ -47,7 +47,11 @@ router.get('/show', (req, res) => {
*
* @apiParam {Number{0-10000}} [count=20] Number of instances to get. **0 returns all instances**.
* @apiParam {Boolean} [include_dead=false] Include dead (down for at least two weeks) instances
* @apiParam {Boolean} [include_down=true] Include down instances
* @apiParam {Boolean} [include_closed=true] Include instances with closed registrations
* @apiParam {String} [min_id] Minimal ID of instances to retrieve. Use this to navigate through pages. The id of the first instance from next page is accessible through pagination.next_id.
* @apiParam {String="name","uptime","https_score","obs_score","users","statuses","connections"} [sort_by] Field to sort instances by. By default, instances are not sorted and their order is not guaranteed to be consistent.
* @apiParam {String="asc","desc"} [sort_order="asc"] Sort order, if *sort_by* is used.
*/
router.get('/list', (req, res) => {
let query;
@@ -66,6 +70,14 @@ router.get('/list', (req, res) => {
type: 'boolean',
optional: true,
def: false
}, include_down: {
type: 'boolean',
optional: true,
def: true
}, include_closed: {
type: 'boolean',
optional: true,
def: true
}, sort_by: {
type: 'string',
optional: true,
@@ -106,6 +118,16 @@ router.get('/list', (req, res) => {
$ne: true
};
if(!query.include_down)
q.up = {
$ne: false
};
if(!query.include_closed)
q.openRegistrations = {
$ne: false
};
if(query.min_id)
try {
q._id = {
@@ -121,10 +143,11 @@ router.get('/list', (req, res) => {
if(limited)
q_options.limit = query.count + 1;
if(query.sort_order) {
if(query.sort_by) {
q_options.sort = {};
q_options.sort[query.sort_by] = query.sort_order === 'asc' ? 1 : -1;
}
Promise
.all([DB.get('instances').count(q), DB.get('instances').find(q, q_options)])
.then(values => {
View
@@ -31,7 +31,7 @@ function checkQuery(template, query) {
}
if(key_template.values) {
if(!key_template.values.contains(key_value))
if(!key_template.values.includes(key_value))
throw new Error(`Parameter "${key}" value "${key_value}" is not allowed.`);
}

0 comments on commit 041b2b7

Please sign in to comment.