More rest methods

This commit is contained in:
vjrj 2018-06-12 08:05:44 +02:00
parent 5e5ecb69b8
commit ec0edf4475

View file

@ -8,6 +8,9 @@ import { check } from 'meteor/check';
import SiteSettings from '/imports/api/SiteSettings/SiteSettings'; import SiteSettings from '/imports/api/SiteSettings/SiteSettings';
import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires'; import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires';
import { countRealFires } from '/imports/api/ActiveFires/server/countFires'; import { countRealFires } from '/imports/api/ActiveFires/server/countFires';
import { whichAreFalsePositives, firesUnion } from '/imports/api/FalsePositives/server/publications';
import FalsePositives from '/imports/api/FalsePositives/FalsePositives';
import Industries from '/imports/api/Industries/Industries';
Meteor.startup(() => { Meteor.startup(() => {
const uptime = new Date(); const uptime = new Date();
@ -79,18 +82,14 @@ Meteor.startup(() => {
} }
}); });
// Maps to: /api/v1/fires-in/:lat/:lng/:km function getFires(route, full) {
// 100 km max const lat = Number(route.urlParams.lat);
// Ex: http://127.0.0.1:3000/api/v1/fires-in/token/38.736946/-9.142685/100 const lng = Number(route.urlParams.lng);
apiV1.addRoute('fires-in/:token/:lat/:lng/:km', { authRequired: false }, { const km = Number(route.urlParams.km);
get: function get() { const { token } = route.urlParams;
const lat = Number(this.urlParams.lat);
const lng = Number(this.urlParams.lng);
const km = Number(this.urlParams.km);
const { token } = this.urlParams;
check(lng, NumberBetween(-180, 180)); check(lng, NumberBetween(-180, 180));
check(lat, NumberBetween(-90, 90)); check(lat, NumberBetween(-90, 90));
check(km, NumberBetween(0, 100)); check(km, NumberBetween(0, Meteor.isDevelopment ? 1000 : 100));
check(token, String); check(token, String);
if (token !== Meteor.settings.private.internalApiToken) { if (token !== Meteor.settings.private.internalApiToken) {
@ -111,9 +110,43 @@ Meteor.startup(() => {
$minDistance: 0 $minDistance: 0
} }
} }
}, {
fields: {
lat: 1,
lon: 1,
when: 1,
scan: 1
}
}); });
if (!full) {
return { total: fires.count(), real: countRealFires(fires) }; return { total: fires.count(), real: countRealFires(fires) };
} }
// TODO only get real
const firesA = fires.fetch();
if (firesA.length > 0) {
const union = firesUnion(fires);
const falsePos = whichAreFalsePositives(FalsePositives, union);
const industries = whichAreFalsePositives(Industries, union);
return { fires: firesA, falsePos: falsePos.fetch(), industries: industries.fetch() };
}
return { fires: [] };
}
// Maps to: /api/v1/fires-in/:lat/:lng/:km
// 100 km max
// Ex: http://127.0.0.1:3000/api/v1/fires-in/token/38.736946/-9.142685/100
apiV1.addRoute('fires-in/:token/:lat/:lng/:km', { authRequired: false }, {
get: function get() {
return getFires(this, false);
}
});
apiV1.addRoute('fires-in-full/:token/:lat/:lng/:km', { authRequired: false }, {
get: function get() {
return getFires(this, true);
}
}); });
}); });