7d55580d87
Split the 5,441-line index.js into an 86-line bootstrap, thin domain
route modules, shared lib/ helpers, and a repo/ layer that isolates the
DEV_MOCK branches (repo/*.mock.js). Repo methods return {statusCode, body};
routes are one-line wrappers. Pin search_path to "transit.bus" and
document the /bus/ prefix with legacy root aliases (Deprecation header).
Parity verified: 57/57 byte-identical on the stub differential harness
plus 24/24 live read-only responses vs the original against real
PostgreSQL. The only intentional behavior change: mock reorder/delete
bus-line endpoints no longer require a live DB (previously 500 in
DEV_MOCK=1 without PG). DEV_MOCK gate and node --check clean.
149 lines
4.4 KiB
JavaScript
149 lines
4.4 KiB
JavaScript
const db = require("../db");
|
|
const { reportError } = require("../lib/report-error");
|
|
|
|
// Stop repository methods (PostgreSQL mode). Each returns { statusCode, body }
|
|
// mirroring the original handler's DB branch.
|
|
|
|
async function listStops(req) {
|
|
try {
|
|
const result = await db.query(`
|
|
SELECT id, name, latitude, longitude, created_at, updated_at
|
|
FROM stops
|
|
ORDER BY id ASC
|
|
`);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
body: {
|
|
stops: result.rows.map((row) => ({
|
|
id: row.id,
|
|
name: row.name,
|
|
latitude: parseFloat(row.latitude),
|
|
longitude: parseFloat(row.longitude),
|
|
createdAt: row.created_at,
|
|
updatedAt: row.updated_at,
|
|
})),
|
|
},
|
|
};
|
|
} catch (err) {
|
|
console.error(err);
|
|
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
|
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
|
}
|
|
}
|
|
|
|
async function createStop(req) {
|
|
try {
|
|
const { name, latitude, longitude } = req.body;
|
|
|
|
if (!name || latitude === undefined || longitude === undefined) {
|
|
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
|
}
|
|
|
|
const result = await db.query(
|
|
`
|
|
WITH next_id AS (
|
|
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM stops
|
|
)
|
|
INSERT INTO stops (id, name, latitude, longitude)
|
|
SELECT next_id.id, $1, $2, $3
|
|
FROM next_id
|
|
RETURNING id, name, latitude, longitude, created_at, updated_at
|
|
`,
|
|
[name, latitude, longitude],
|
|
);
|
|
|
|
const stop = result.rows[0];
|
|
return {
|
|
statusCode: 201,
|
|
body: {
|
|
stop: {
|
|
id: stop.id,
|
|
name: stop.name,
|
|
latitude: parseFloat(stop.latitude),
|
|
longitude: parseFloat(stop.longitude),
|
|
createdAt: stop.created_at,
|
|
updatedAt: stop.updated_at,
|
|
},
|
|
},
|
|
};
|
|
} catch (err) {
|
|
console.error(err);
|
|
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
|
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
|
}
|
|
}
|
|
|
|
async function deleteStop(req) {
|
|
try {
|
|
const { stopId } = req.params;
|
|
|
|
if (!stopId || !Number.isFinite(Number(stopId))) {
|
|
return { statusCode: 400, body: { error: "invalid_stop_id" } };
|
|
}
|
|
|
|
const result = await db.query(
|
|
`DELETE FROM stops WHERE id = $1 RETURNING id, name`,
|
|
[stopId],
|
|
);
|
|
|
|
if (result.rows.length === 0) {
|
|
return { statusCode: 404, body: { error: "stop_not_found" } };
|
|
}
|
|
|
|
return { statusCode: 200, body: { success: true, stop: result.rows[0] } };
|
|
} catch (err) {
|
|
console.error(err);
|
|
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
|
if (err.code === "23503") {
|
|
return { statusCode: 409, body: { error: "foreign_key_violation", message: "This stop is referenced by route_stops" } };
|
|
}
|
|
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
|
}
|
|
}
|
|
|
|
async function updateStop(req) {
|
|
try {
|
|
const { stopId } = req.params;
|
|
const { name, latitude, longitude } = req.body;
|
|
|
|
if (!stopId || !Number.isFinite(Number(stopId))) {
|
|
return { statusCode: 400, body: { error: "invalid_stop_id" } };
|
|
}
|
|
|
|
const result = await db.query(
|
|
`UPDATE stops
|
|
SET name = COALESCE($1, name),
|
|
latitude = COALESCE($2, latitude),
|
|
longitude = COALESCE($3, longitude),
|
|
updated_at = NOW()
|
|
WHERE id = $4
|
|
RETURNING id, name, latitude, longitude, created_at, updated_at`,
|
|
[name ?? null, latitude ?? null, longitude ?? null, stopId],
|
|
);
|
|
|
|
if (!result.rows[0])
|
|
return { statusCode: 404, body: { error: "stop_not_found" } };
|
|
|
|
const stop = result.rows[0];
|
|
return {
|
|
statusCode: 200,
|
|
body: {
|
|
stop: {
|
|
id: stop.id,
|
|
name: stop.name,
|
|
latitude: parseFloat(stop.latitude),
|
|
longitude: parseFloat(stop.longitude),
|
|
createdAt: stop.created_at,
|
|
updatedAt: stop.updated_at,
|
|
},
|
|
},
|
|
};
|
|
} catch (err) {
|
|
console.error(err);
|
|
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
|
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
|
}
|
|
}
|
|
|
|
module.exports = { listStops, createStop, updateStop, deleteStop }; |