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).
74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
const { pool } = require("../modules/database")
|
|
const { DefaultError } = require("../errors/errors")
|
|
|
|
async function findAllLines() {
|
|
try {
|
|
const sql = "SELECT * FROM \`lines\`"
|
|
const rows = await pool.query(sql)
|
|
return rows
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function findLineById(lineId) {
|
|
try {
|
|
const sql = "SELECT * FROM \`lines\` WHERE id = ?"
|
|
const rows = await pool.query(sql, [lineId])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function findLinesByTransportType(transportTypeId) {
|
|
try {
|
|
const sql = "SELECT * FROM \`lines\` WHERE transportTypeId = ?"
|
|
const rows = await pool.query(sql, [transportTypeId])
|
|
return rows
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function createLine(code, name, textColorHex, backColorHex, transportTypeId) {
|
|
try {
|
|
const sql = "INSERT INTO \`lines\` (code, name, textColorHex, backColorHex, transportTypeId) VALUES (?, ?, ?, ?, ?)"
|
|
const rows = await pool.query(sql, [code, name, textColorHex, backColorHex, transportTypeId])
|
|
return rows.insertId
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function updateLine(lineId, updateData) {
|
|
try {
|
|
const fields = Object.keys(updateData)
|
|
const values = Object.values(updateData)
|
|
const setClause = fields.map(f => `${f} = ?`).join(", ")
|
|
const sql = `UPDATE \`lines\` SET ${setClause} WHERE id = ?`
|
|
const rows = await pool.query(sql, [...values, lineId])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function deleteLine(lineId) {
|
|
try {
|
|
const sql = "DELETE FROM \`lines\` WHERE id = ?"
|
|
const rows = await pool.query(sql, [lineId])
|
|
return rows.affectedRows > 0
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
findAllLines,
|
|
findLineById,
|
|
findLinesByTransportType,
|
|
createLine,
|
|
updateLine,
|
|
deleteLine
|
|
} |