generated from azures04/Base-REST-API
Add transport DDL, repos and transport service
Add database DDL and server-side data access/service layers for transport domain. New SQL DDL files create transport_types, lines, stations, platforms and platform_lines tables (with FKs, indexes, spatial POINT column, CHECK constraints, and triggers to populate location). New repositories implement queries and CRUD for lines, stations, platforms and platform_lines (including distance/nearest-platform and accessibility queries). A transportService aggregates repo functions and exposes high-level operations (CRUD, station/line lookups, platform-line management and nearest-platform validation).
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
const { pool } = require("../modules/database")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
|
||||
async function findPlatformsByStation(stationId) {
|
||||
try {
|
||||
const sql = "SELECT * FROM platforms WHERE stationId = ?"
|
||||
return await pool.query(sql, [stationId])
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function findPlatformById(platformId) {
|
||||
try {
|
||||
const sql = "SELECT * FROM platforms WHERE id = ?"
|
||||
const rows = await pool.query(sql, [platformId])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
// cœur de la validation : quai le plus proche desservant CETTE ligne
|
||||
async function findNearestPlatformForLine(lineId, latitude, longitude, radiusM) {
|
||||
try {
|
||||
const sql = `
|
||||
SELECT p.*, ST_Distance_Sphere(p.location, ST_SRID(POINT(?, ?), 4326)) AS distance
|
||||
FROM platforms p
|
||||
JOIN platform_lines pl ON pl.platformId = p.id
|
||||
WHERE pl.lineId = ?
|
||||
HAVING distance <= ?
|
||||
ORDER BY distance
|
||||
LIMIT 1
|
||||
`
|
||||
const rows = await pool.query(sql, [longitude, latitude, lineId, radiusM])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function createPlatform(stationId, name, direction, latitude, longitude, isPRM, hasElevator) {
|
||||
try {
|
||||
const sql = `
|
||||
INSERT INTO platforms (stationId, name, direction, latitude, longitude, isPRM, hasElevator)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
const rows = await pool.query(sql, [
|
||||
stationId, name, direction, latitude, longitude,
|
||||
longitude, latitude, isPRM, hasElevator
|
||||
])
|
||||
return rows.insertId
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function updatePlatformAccessibility(platformId, isPRM, hasElevator) {
|
||||
try {
|
||||
const sql = "UPDATE platforms SET isPRM = ?, hasElevator = ? WHERE id = ?"
|
||||
const rows = await pool.query(sql, [isPRM, hasElevator, platformId])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function deletePlatform(platformId) {
|
||||
try {
|
||||
const sql = "DELETE FROM platforms WHERE id = ?"
|
||||
const rows = await pool.query(sql, [platformId])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findPlatformsByStation,
|
||||
findPlatformById,
|
||||
findNearestPlatformForLine,
|
||||
createPlatform,
|
||||
updatePlatformAccessibility,
|
||||
deletePlatform
|
||||
}
|
||||
Reference in New Issue
Block a user