diff --git a/src/dal/data/routePath.json b/src/dal/data/routePath.json index 1a7c8bf..d14cef6 100644 --- a/src/dal/data/routePath.json +++ b/src/dal/data/routePath.json @@ -6020,7 +6020,7 @@ ] }, { - "701": + "703": [ { "shape_id": "238255", diff --git a/src/dal/staticDal.js b/src/dal/staticDal.js index 6015b23..8696c5e 100644 --- a/src/dal/staticDal.js +++ b/src/dal/staticDal.js @@ -14,6 +14,11 @@ function readJSON(name) { } } +function toNumber(v) { + const n = Number(v); + return Number.isFinite(n) ? n : undefined; +} + export function getVehicles() { const raw = readJSON("vehicles.json"); @@ -26,11 +31,11 @@ export function getVehicles() { routeNum: v.routeNum, routeName: v.routeName, destination: v.destination, - bearing: v.bearing == null ? undefined : Number(v.bearing), - speed: v.speed == null ? undefined : Number(v.speed), + bearing: v.bearing == null ? undefined : toNumber(v.bearing), + speed: v.speed == null ? undefined : toNumber(v.speed), location: { - latitude: loc.latitude == null ? undefined : Number(loc.latitude), - longitude: loc.longitude == null ? undefined : Number(loc.longitude) + latitude: toNumber(loc.latitude), + longitude: toNumber(loc.longitude) } }; @@ -55,22 +60,40 @@ export function getRoutes() { export function getRouteById(routeId) { if (routeId == null) return null; const routes = getRoutes(); - + return routes.find(r => String(r.route_id) === String(routeId)) || null; } -export function getRoutePathsMap() { - const raw = readJSON("routepaths.json"); +function readRoutepathsRaw() { + const raw = readJSON("routePath.json"); - if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw; - return {}; + if (!Array.isArray(raw)) return []; + return raw; +} + +export function getRoutePathsMap() { + const raw = readRoutepathsRaw(); + const map = {}; + + for (const item of raw) { + if (!item || typeof item !== "object" || Array.isArray(item)) continue; + const keys = Object.keys(item); + + if (keys.length !== 1) continue; + const k = String(keys[0]); + + if (Array.isArray(item[k])) map[k] = item[k]; + } + + return map; } export function getRoutePath(routeId) { if (routeId == null) return null; const map = getRoutePathsMap(); + const key = String(routeId); - return Object.prototype.hasOwnProperty.call(map, routeId) ? map[routeId] : null; + return Object.prototype.hasOwnProperty.call(map, key) ? map[key] : null; } export function getStationsRaw() { @@ -78,7 +101,7 @@ export function getStationsRaw() { } export function getStations() { - const raw = getStationsRaw(); + const raw = getStationsRaw(); if (raw == null) return []; if (Array.isArray(raw)) { @@ -93,13 +116,29 @@ export function getStations() { } +function transformationStation(s) { + return { + stop_id: s.stop_id, + stop_code: s.stop_code, + stop_name: s.stop_name, + stop_desc: s.stop_desc, + location: { latitude: toNumber(s.stop_lat), longitude: toNumber(s.stop_lon) }, + stop_url: s.stop_url, + location_type: s.location_type, + parent_station: s.parent_station, + lines: s.lines + }; +} + export function getStopsByRoute(routeId) { if (routeId == null) return []; const raw = getStationsRaw(); - + if (raw == null) return []; if (typeof raw === "object" && !Array.isArray(raw)) { - return Array.isArray(raw[routeId]) ? raw[routeId] : []; + const arr = raw[routeId]; + if (!Array.isArray(arr)) return []; + return arr.map(transformationStation); } if (Array.isArray(raw)) { @@ -107,12 +146,12 @@ export function getStopsByRoute(routeId) { for (const item of raw) { if (typeof item === "object" && !Array.isArray(item) && Object.prototype.hasOwnProperty.call(item, routeId)) { const arr = item[routeId]; - if (Array.isArray(arr)) matchesFromArrayMaps.push(...arr); + if (Array.isArray(arr)) matchesFromArrayMaps.push(...arr.map(transformationStation)); } } if (matchesFromArrayMaps.length) return matchesFromArrayMaps; - return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId))); + return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId))).map(transformationStation); } return []; @@ -128,7 +167,7 @@ export function getStationById(id) { const arr = raw[key]; if (!Array.isArray(arr)) continue; const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)); - if (found) return found; + if (found) return transformationStation(found); } return null; } @@ -139,13 +178,14 @@ export function getStationById(id) { for (const item of raw) { for (const key of Object.keys(item)) { const arr = item[key]; - const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)); - if (found) return found; + const found = arr.find(s => String(s.stop_id) === String(id)); + if (found) return transformationStation(found); } } return null; } - return raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)) || null; + const found = raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)); + return found ? transformationStation(found) : null; } return null; diff --git a/src/routes/routes.js b/src/routes/routes.js index 489aca7..1dabdd3 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -5,7 +5,7 @@ const router = express.Router(); router.get("/routes", (req, res) => { const routes = dal.getRoutes(); - + res.json({meta: {returned: routes.length}, data: routes}); }); @@ -14,10 +14,9 @@ router.get("/routes/:routeId", (req, res) => { const route = dal.getRouteById(routeId); if (!route) return res.status(404).json({error: "Route Was Not Found"}); - const routePath = dal.getRoutePath(routeId); const stations = dal.getStopsByRoute(routeId); - - res.json({data: {route, routePath, stations}}); + + res.json({data: {route, stations}}); }); router.get("/route/:routeId", (req, res) => { @@ -25,10 +24,17 @@ router.get("/route/:routeId", (req, res) => { const route = dal.getRouteById(routeId); if (!route) return res.status(404).json({error: "Route Was Not Found"}); - const routePath = dal.getRoutePath(routeId); const stations = dal.getStopsByRoute(routeId); + + res.json({data: {route, stations}}); +}); - res.json({data: {route, routePath, stations}}); +router.get("/routepaths/:routeId", (req, res) => { + const routeId = req.params.routeId; + const rp = dal.getRoutePath(routeId); + + if (!rp) return res.status(404).json({error: "RoutePath Was Not Found"}); + res.json({meta: {routeId: String(routeId), returned: Array.isArray(rp) ? rp.length : 0}, data: rp}); }); router.get("/routes/:routeId/stations", (req, res) => {