From eaf76f94a1e2c1a1a59217bee87034e9b5298415 Mon Sep 17 00:00:00 2001 From: Edward-1100 Date: Mon, 24 Nov 2025 18:32:45 -0700 Subject: [PATCH] Updated routes --- src/dal/staticDal.js | 146 ++++++++++++++++++++++++++++--------------- src/routes/routes.js | 37 +++++++---- 2 files changed, 120 insertions(+), 63 deletions(-) diff --git a/src/dal/staticDal.js b/src/dal/staticDal.js index a3028a9..6015b23 100644 --- a/src/dal/staticDal.js +++ b/src/dal/staticDal.js @@ -6,101 +6,147 @@ const dataDir = path.join(process.cwd(), "src", "dal", "data"); function readJSON(name) { try { const p = path.join(dataDir, name); + if (!fs.existsSync(p)) return null; - const raw = fs.readFileSync(p, "utf8"); - return JSON.parse(raw); - } catch (err) { + return JSON.parse(fs.readFileSync(p, "utf8")); + } catch { return null; } } export function getVehicles() { - return readJSON("vehicles.json") || []; + const raw = readJSON("vehicles.json"); + + if (!Array.isArray(raw)) return []; + return raw.map(v => { + const loc = v.location || {}; + + return { + vehicleId: v.vehicleId, + 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), + location: { + latitude: loc.latitude == null ? undefined : Number(loc.latitude), + longitude: loc.longitude == null ? undefined : Number(loc.longitude) + } + }; + + }); + } export function getVehicleById(id) { if (id == null) return null; const vehicles = getVehicles(); - + return vehicles.find(v => String(v.vehicleId) === String(id)) || null; } export function getRoutes() { - const explicit = readJSON("routes.json"); - if (Array.isArray(explicit) && explicit.length) return explicit; + const raw = readJSON("routes.json"); - const vehicles = getVehicles(); - const map = new Map(); - vehicles.forEach(v => { - const key = String(v.routeNum ?? ""); - if (!map.has(key)) { - map.set(key, { - routeId: key, - routeName: v.routeName ?? null, - startTime: null, - endTime: null, - trains: [] - }); - } - - map.get(key).trains.push(v.vehicleId); - - }); - - return Array.from(map.values()); + if (!Array.isArray(raw)) return []; + return raw; } export function getRouteById(routeId) { if (routeId == null) return null; const routes = getRoutes(); - - return routes.find(r => String(r.routeId) === String(routeId)) || null; + + return routes.find(r => String(r.route_id) === String(routeId)) || null; } export function getRoutePathsMap() { const raw = readJSON("routepaths.json"); - return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {}; + if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw; + return {}; } export function getRoutePath(routeId) { if (routeId == null) return null; const map = getRoutePathsMap(); - const keys = Object.keys(map || {}); - const foundKey = keys.find(k => String(k) === String(routeId)); - return foundKey ? map[foundKey] : null; + return Object.prototype.hasOwnProperty.call(map, routeId) ? map[routeId] : null; +} + +export function getStationsRaw() { + return readJSON("stations.json") || null; } export function getStations() { - return readJSON("stations.json") || []; + const raw = getStationsRaw(); + + if (raw == null) return []; + if (Array.isArray(raw)) { + const isArrayOfMaps = raw.every(item => typeof item === "object" && !Array.isArray(item) && Object.values(item).every(v => Array.isArray(v))); + if (isArrayOfMaps) return raw.flatMap(item => Object.values(item).flat()); + return raw; + } + if (typeof raw === "object") { + return Object.values(raw).flat(); + } + return []; + } export function getStopsByRoute(routeId) { if (routeId == null) return []; - const stations = getStations(); + const raw = getStationsRaw(); - if (!Array.isArray(stations)) return []; - return stations.filter(s => { - const lines = s.lines ?? s.lines_arr ?? s.line_ids ?? null; - if (!Array.isArray(lines)) return false; + if (raw == null) return []; + if (typeof raw === "object" && !Array.isArray(raw)) { + return Array.isArray(raw[routeId]) ? raw[routeId] : []; + } - return lines.map(String).includes(String(routeId)); - }); + if (Array.isArray(raw)) { + const matchesFromArrayMaps = []; + 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 (matchesFromArrayMaps.length) return matchesFromArrayMaps; + return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId))); + } + + return []; } -export function getStationById(stationId) { - if (stationId == null) return null; - const stations = getStations(); +export function getStationById(id) { + if (id == null) return null; + const raw = getStationsRaw(); + if (raw == null) return null; - if (!Array.isArray(stations)) return null; - return stations.find(s => { - if (s.stop_id && String(s.stop_id) === String(stationId)) return true; - if (s.stationId && String(s.stationId) === String(stationId)) return true; - if (s.id && String(s.id) === String(stationId)) return true; + if (typeof raw === "object" && !Array.isArray(raw)) { + for (const key of Object.keys(raw)) { + 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; + } + return null; + } - return false; - }) || null; + if (Array.isArray(raw)) { + const isArrayOfMaps = raw.every(item => typeof item === "object" && !Array.isArray(item) && Object.values(item).every(v => Array.isArray(v))); + if (isArrayOfMaps) { + 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; + } + } + return null; + } + return raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)) || null; + } + return null; } diff --git a/src/routes/routes.js b/src/routes/routes.js index b855494..489aca7 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -11,32 +11,43 @@ router.get("/routes", (req, res) => { router.get("/routes/:routeId", (req, res) => { const routeId = req.params.routeId; - let route = dal.getRouteById(routeId) ?? null; + const route = dal.getRouteById(routeId); - if (route === null) { - const all = dal.getRoutes(); - route = all.find(r => { - const rid = r.route_id ?? r.routeId ?? r.route; - return rid != null && String(rid) === String(routeId); - }) ?? null; - } + 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}}); +}); - const routePath = dal.getRoutePath(routeId) ?? null; - let stations = dal.getStopsByRoute(routeId) ?? []; +router.get("/route/:routeId", (req, res) => { + const routeId = req.params.routeId; + 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}}); }); router.get("/routes/:routeId/stations", (req, res) => { const routeId = req.params.routeId; - const stations = dal.getStopsByRoute(routeId) ?? []; + const stations = dal.getStopsByRoute(routeId); + + res.json({meta: {routeId: String(routeId), returned: stations.length}, data: stations}); +}); + +router.get("/stops/:routeId", (req, res) => { + const routeId = req.params.routeId; + const stations = dal.getStopsByRoute(routeId); res.json({meta: {routeId: String(routeId), returned: stations.length}, data: stations}); }); router.get("/stations", (req, res) => { const route = req.query.route; - const stations = route ? (dal.getStopsByRoute(route) ?? []) : (dal.getStations() ?? []); + const stations = route ? dal.getStopsByRoute(route) : dal.getStations(); res.json({meta: {returned: stations.length}, data: stations}); }); @@ -44,8 +55,8 @@ router.get("/stations", (req, res) => { router.get("/station/:stationId", (req, res) => { const stationId = req.params.stationId; const station = dal.getStationById(stationId); - if (!station) return res.status(404).json({error: "Station Was Not Found"}); + if (!station) return res.status(404).json({error: "Station Was Not Found"}); res.json({data: station}); });