Full final production #6
File diff suppressed because it is too large
Load Diff
@@ -1,152 +0,0 @@
|
|||||||
import fs from "fs";
|
|
||||||
import path from "path";
|
|
||||||
|
|
||||||
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;
|
|
||||||
return JSON.parse(fs.readFileSync(p, "utf8"));
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getVehicles() {
|
|
||||||
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 raw = readJSON("routes.json");
|
|
||||||
|
|
||||||
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.route_id) === String(routeId)) || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getRoutePathsMap() {
|
|
||||||
const raw = readJSON("routepaths.json");
|
|
||||||
|
|
||||||
if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getRoutePath(routeId) {
|
|
||||||
if (routeId == null) return null;
|
|
||||||
const map = getRoutePathsMap();
|
|
||||||
|
|
||||||
return Object.prototype.hasOwnProperty.call(map, routeId) ? map[routeId] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getStationsRaw() {
|
|
||||||
return readJSON("stations.json") || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getStations() {
|
|
||||||
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 raw = getStationsRaw();
|
|
||||||
|
|
||||||
if (raw == null) return [];
|
|
||||||
if (typeof raw === "object" && !Array.isArray(raw)) {
|
|
||||||
return Array.isArray(raw[routeId]) ? raw[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(id) {
|
|
||||||
if (id == null) return null;
|
|
||||||
const raw = getStationsRaw();
|
|
||||||
if (raw == null) return null;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
import express from "express";
|
|
||||||
import * as dal from "../dal/staticDal.js";
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
router.get("/routes", (req, res) => {
|
|
||||||
const routes = dal.getRoutes();
|
|
||||||
|
|
||||||
res.json({meta: {returned: routes.length}, data: routes});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/routes/: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("/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);
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
res.json({meta: {returned: stations.length}, data: stations});
|
|
||||||
});
|
|
||||||
|
|
||||||
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"});
|
|
||||||
res.json({data: station});
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
Reference in New Issue
Block a user