49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
import express from "express";
|
|
import * as dal from "../dal/staticDal.js";
|
|
|
|
const router = express.Router();
|
|
|
|
router.get("/", (req, res) => {
|
|
let vehicles = dal.getVehicles();
|
|
|
|
const {routeNum, routeName, destination, minLat, maxLat, minLng, maxLng, limit} = req.query;
|
|
|
|
if (routeNum) vehicles = vehicles.filter(v => String(v.routeNum) === String(routeNum));
|
|
if (routeName) vehicles = vehicles.filter(v => v.routeName && v.routeName.toLowerCase().includes(String(routeName).toLowerCase()));
|
|
if (destination) vehicles = vehicles.filter(v => v.destination && v.destination.toLowerCase().includes(String(destination).toLowerCase()));
|
|
|
|
if (minLat || maxLat || minLng || maxLng) {
|
|
const minLatN = parseFloat(minLat ?? -90);
|
|
const maxLatN = parseFloat(maxLat ?? 90);
|
|
const minLngN = parseFloat(minLng ?? -180);
|
|
const maxLngN = parseFloat(maxLng ?? 180);
|
|
|
|
vehicles = vehicles.filter(v => {
|
|
const lat = parseFloat(v.location?.latitude);
|
|
const lng = parseFloat(v.location?.longitude);
|
|
|
|
return lat >= minLatN && lat <= maxLatN && lng >= minLngN && lng <= maxLngN;
|
|
});
|
|
|
|
}
|
|
|
|
let returned = vehicles.length;
|
|
if (limit) {
|
|
const lim = Math.max(1, Math.min(1000, parseInt(limit, 10) || 100));
|
|
|
|
vehicles = vehicles.slice(0, lim);
|
|
returned = vehicles.length;
|
|
}
|
|
|
|
res.json({meta: {returned}, data: vehicles});
|
|
});
|
|
|
|
router.get("/:id", (req, res) => {
|
|
const v = dal.getVehicleById(req.params.id);
|
|
if (!v) return res.status(404).json({error: "Vehicle Was Not Found"});
|
|
|
|
res.json({data: v});
|
|
});
|
|
|
|
export default router;
|