var express = require('express');
var router = express.Router();
var HttpError = require('./errors').HttpError;

/*
 * GET /
 * [page = 0]
 * [key = ""]: on empty, it will search all company
 * Show 25 results per page
 */
router.get('/search', function(req, res, next) {
    var search = req.query.key || "";
    var page = req.query.page || 0;
    var q;

    if (search == "") {
        q = {};
    } else {
        q = {name: new RegExp("^" + search)};
    }

    var collection = req.db.collection('companies');

    collection.find(q).skip(25 * page).limit(25).toArray().then(function(results) {
        res.send(results);
    }).catch(function(err) {
        next(new HttpError("Internal Server Error", 500));
    });
});

module.exports = router;

var express = require('express');
var router = express.Router();
var HttpError = require('./errors').HttpError;

/*
 * GET /
 * [page = 0]
 * [key = ""]: on empty, it will search all company
 * Show 25 results per page
 */
router.get('/search', function(req, res, next) {
    var search = req.query.key || "";
    var page = req.query.page || 0;
    var q;

    if (search == "") {
        q = {};
    } else {
        q = {
            $or: [
                {name: new RegExp("^" + search)},
                {id: search},
            ]
        };
    }

    var collection = req.db.collection('companies');

    collection.find(q).skip(25 * page).limit(25).toArray().then(function(results) {
        res.send(results);
    }).catch(function(err) {
        next(new HttpError("Internal Server Error", 500));
    });
});

module.exports = router;

Enable searching the company id
Enable searching the company id
