generated from azures04/Base-REST-API
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).
88 lines
2.9 KiB
JavaScript
88 lines
2.9 KiB
JavaScript
const { pool } = require("../modules/database")
|
|
const { DefaultError } = require("../errors/errors")
|
|
|
|
// lignes d'une station (dédupliquées, tous quais confondus)
|
|
async function findLinesByStation(stationId) {
|
|
try {
|
|
const sql = `
|
|
SELECT DISTINCT l.*
|
|
FROM \`lines\` l
|
|
JOIN platform_lines pl ON pl.lineId = l.id
|
|
JOIN platforms p ON p.id = pl.platformId
|
|
WHERE p.stationId = ?
|
|
`
|
|
return await pool.query(sql, [stationId])
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function findLinesByPlatform(platformId) {
|
|
try {
|
|
const sql = `
|
|
SELECT l.*, pl.directionId, pl.orderInLine
|
|
FROM \`lines\` l
|
|
JOIN platform_lines pl ON pl.lineId = l.id
|
|
WHERE pl.platformId = ?
|
|
`
|
|
return await pool.query(sql, [platformId])
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
// parcours ordonné d'une ligne dans un sens donné
|
|
async function findStationsByLine(lineId, directionId = 0) {
|
|
try {
|
|
const sql = `
|
|
SELECT s.*, p.id AS platformId, p.name AS platformName, pl.orderInLine
|
|
FROM stations s
|
|
JOIN platforms p ON p.stationId = s.id
|
|
JOIN platform_lines pl ON pl.platformId = p.id
|
|
WHERE pl.lineId = ? AND pl.directionId = ?
|
|
ORDER BY pl.orderInLine
|
|
`
|
|
return await pool.query(sql, [lineId, directionId])
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function addLineToPlatform(platformId, lineId, directionId, orderInLine) {
|
|
try {
|
|
const sql = "INSERT INTO platform_lines (platformId, lineId, directionId, orderInLine) VALUES (?, ?, ?, ?)"
|
|
const rows = await pool.query(sql, [platformId, lineId, directionId, orderInLine])
|
|
return rows.insertId
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function removeLineFromPlatform(platformId, lineId, directionId) {
|
|
try {
|
|
const sql = "DELETE FROM platform_lines WHERE platformId = ? AND lineId = ? AND directionId = ?"
|
|
const rows = await pool.query(sql, [platformId, lineId, directionId])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function updateOrder(platformLineId, orderInLine) {
|
|
try {
|
|
const sql = "UPDATE platform_lines SET orderInLine = ? WHERE id = ?"
|
|
const rows = await pool.query(sql, [orderInLine, platformLineId])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
findLinesByStation,
|
|
findLinesByPlatform,
|
|
findStationsByLine,
|
|
addLineToPlatform,
|
|
removeLineFromPlatform,
|
|
updateOrder
|
|
} |