2020-03-02 20:50:33 +01:00
|
|
|
import models from "../models";
|
|
|
|
|
|
|
|
class Stations {
|
2020-03-06 15:49:16 +01:00
|
|
|
/**
|
|
|
|
* Méthode permettant de récuper la liste des stations selon des critères
|
|
|
|
* @param {Object} req
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
2020-03-02 20:50:33 +01:00
|
|
|
static getAll(req, callback) {
|
|
|
|
const { radius, lon, lat, start, limit } = req.query;
|
2020-03-06 15:49:16 +01:00
|
|
|
|
|
|
|
if (!lon || !lat) {
|
|
|
|
callback(new Error("Missing lat, lon or radius"));
|
|
|
|
} else {
|
|
|
|
const query = {
|
|
|
|
location: {
|
|
|
|
$near: {
|
|
|
|
$maxDistance: Number(radius),
|
|
|
|
$geometry: {
|
|
|
|
type: "Point",
|
|
|
|
coordinates: [Number(lon), Number(lat)]
|
|
|
|
}
|
2020-03-02 20:50:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 15:49:16 +01:00
|
|
|
};
|
2020-03-02 20:50:33 +01:00
|
|
|
|
2020-03-06 15:49:16 +01:00
|
|
|
const skip = parseInt(start || 0, 10);
|
|
|
|
const _limit = parseInt(limit && limit <= 50 ? limit : 20, 10);
|
2020-03-02 20:50:33 +01:00
|
|
|
|
2020-03-06 15:49:16 +01:00
|
|
|
models.Stations.count(query)
|
|
|
|
.then(count => {
|
|
|
|
models.Stations.find(query)
|
|
|
|
.skip(skip)
|
|
|
|
.limit(_limit)
|
|
|
|
.then(items => {
|
|
|
|
callback(null, { total: count, items });
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(callback);
|
|
|
|
}
|
2020-03-02 20:50:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Stations;
|