import models from "../models";

class Stations {
  static getAll(req, callback) {
    const { radius, lon, lat, start, limit } = req.query;
    const query = {
      location: {
        $near: {
          $maxDistance: Number(radius),
          $geometry: {
            type: "Point",
            coordinates: [Number(lon), Number(lat)]
          }
        }
      }
    };

    const skip = start || 0;
    const _limit = limit && limit <= 50 ? limit : 20;

    models.Stations.count(query)
      .then(count => {
        models.Stations.find(query)
          .skip(parseInt(skip, 10))
          .limit(parseInt(_limit, 10))
          .then(items => {
            callback(null, { total: count, items });
          });
      })
      .catch(callback);
  }
}

export default Stations;
