'use strict';
var _ = require('lodash'),
    Promise = require('bluebird');

module.exports = ['data', function (data) {
  var filters = function (query) {
    var where = data.utils.rangeFilter('Year', query.start, query.end);
    return _.assign(where, data.utils.inFilter('Make', query.makes));
  };

  return {
    '/:operation?': {
      get: function (req) {
        return new Promise(function (resolve, reject) {
          var op = data.utils.operation(req.params),
              pagination = data.utils.pagination(req.query);

          switch (op) {
            case 'count':
              data.Recall.count(filters(req.query)).exec(function (err, count) {
                if (err) {
                  reject(err);
                  return;
                }
                resolve([200, undefined, { count: count }]);
              });
              break;
            default:
              var query = data.Recall.find(filters(req.query));
              if (pagination) {
                query.skip(pagination.from).limit(pagination.limit);
              }
              query.exec(function (err, recalls) {
                if (err) {
                  reject(err);
                  return;
                }
                resolve([200, undefined, recalls]);
              });
              break;
          }
        });
      }
    }
  };
}];
'use strict';
var _ = require('lodash'),
    Promise = require('bluebird');

module.exports = ['data', function (data) {
  var filters = function (query) {
    var where = data.utils.rangeFilter('Year', query.start, query.end);
    return _.assign(where, data.utils.inFilter('Make', query.makes));
  };

  return {
    '/:operation?': {
      get: function (req) {
        return new Promise(function (resolve, reject) {
          var op = data.utils.operation(req.params),
              pagination = data.utils.pagination(req.query);

          switch (op) {
            case 'count':
              data.Recall.count(filters(req.query)).exec(function (err, count) {
                if (err) {
                  reject(err);
                  return;
                }
                resolve([200, undefined, { count: count }]);
              });
              break;
            default:
              var query = data.Recall.find(filters(req.query)).sort({ Year: 1, Make: 1, Model: 1 });
              if (pagination) {
                query.skip(pagination.from).limit(pagination.limit);
              }
              query.exec(function (err, recalls) {
                if (err) {
                  reject(err);
                  return;
                }
                resolve([200, undefined, recalls]);
              });
              break;
          }
        });
      }
    }
  };
}];
Add sort order to query results
Add sort order to query results

* Hard coded the sort order for now.
