This commit is contained in:
cracklesparkle
2025-01-30 12:36:39 +09:00
parent e6b3dc05d3
commit 0788a401ca
43 changed files with 3710 additions and 1724 deletions

143
ems/src/api/db/index.ts Normal file
View File

@ -0,0 +1,143 @@
import express, { Request, Response } from 'express';
import { pgQuery } from '../../utils/postgres';
const router = express.Router()
router.get('/rows/:table_name', async (req: Request, res: Response) => {
try {
const { table_name } = req.params
const { offset, limit } = req.query
const result = await pgQuery(
`
SELECT * FROM ${table_name}
OFFSET ${offset || 0} LIMIT ${limit || 10}
`
)
if (Array.isArray(result)) {
if (result.length > 0) {
res.status(200).json(result)
} else {
res.status(404).json('not found')
}
} else {
res.status(404).json('not found')
}
} catch (err) {
res.status(500)
}
})
router.get('/columns/:table_name', async (req: Request, res: Response) => {
try {
const { table_name } = req.params
const result = await pgQuery(
`
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = '${table_name}'
`
)
if (Array.isArray(result)) {
if (result.length > 0) {
res.status(200).json(result)
} else {
res.status(404).json('not found')
}
} else {
res.status(404).json('not found')
}
} catch (err) {
res.status(500)
}
})
router.get('/tables', async (req: Request, res: Response) => {
try {
const result = await pgQuery(
`
SELECT tablename FROM pg_tables WHERE schemaname = 'public';
`
)
if (Array.isArray(result)) {
if (result.length > 0) {
res.status(200).json(result)
} else {
res.status(404).json('not found')
}
} else {
res.status(404).json('not found')
}
} catch (err) {
res.status(500)
}
})
router.get('/figures/import', async (req: Request, res: Response) => {
try {
const { entity_type } = req.params
const result = await pgQuery(
`
SELECT * FROM bounds
WHERE entity_type = $1
`,
[entity_type]
)
if (Array.isArray(result)) {
if (result.length > 0) {
const geometries = result.map((bound: { id: number, entity_id: number, entity_type: string, geometry: JSON, published_at: string, deleted_at: string | null }) => {
return {
...bound.geometry,
properties: {
id: bound.id,
entity_id: bound.entity_id,
entity_type: bound.entity_type
}
}
})
res.status(200).json(geometries)
} else {
res.status(404).json('not found')
}
} else {
res.status(404).json('not found')
}
} catch (err) {
res.status(500)
}
})
router.get('/bounds/:entity_type/:entity_id', async (req: Request, res: Response) => {
try {
const { entity_type, entity_id } = req.params
const result = await pgQuery(
`
SELECT * FROM bounds
WHERE entity_type = $1
AND entity_id = $2
`,
[entity_type, entity_id]
)
if (Array.isArray(result)) {
if (result.length > 0) {
res.status(200).json(result[0].geometry)
} else {
res.status(404).json('not found')
}
} else {
res.status(404).json('not found')
}
} catch (err) {
res.status(500)
}
})
export default router

View File

@ -171,27 +171,34 @@ router.get('/objects/list', async (req: Request, res: Response) => {
const result = await tediousQuery(
`
SELECT
tTypes.id AS id,
tTypes.name AS name,
COUNT(vObjects.type) AS count
${GeneralDB}..tTypes.id AS id,
${GeneralDB}..tTypes.name AS name,
COUNT(vo.type) AS count,
tr.r,
tr.g,
tr.b
FROM
vObjects
${GeneralDB}..vObjects vo
JOIN
tTypes ON vObjects.type = tTypes.id
${GeneralDB}..tTypes ON vo.type = ${GeneralDB}..tTypes.id
LEFT JOIN ${GisDB}..TypeRoles tr ON tr.id = ${GeneralDB}..tTypes.id
WHERE
vObjects.id_city = ${city_id} AND vObjects.year = ${year}
vo.id_city = ${city_id} AND vo.year = ${year}
AND
(
CASE
WHEN TRY_CAST(vObjects.planning AS BIT) IS NOT NULL THEN TRY_CAST(vObjects.planning AS BIT)
WHEN vObjects.planning = 'TRUE' THEN 1
WHEN vObjects.planning = 'FALSE' THEN 0
WHEN TRY_CAST(vo.planning AS BIT) IS NOT NULL THEN TRY_CAST(vo.planning AS BIT)
WHEN vo.planning = 'TRUE' THEN 1
WHEN vo.planning = 'FALSE' THEN 0
ELSE NULL
END
) = ${planning}
GROUP BY
tTypes.id,
tTypes.name;
${GeneralDB}..tTypes.id,
${GeneralDB}..tTypes.name,
tr.r,
tr.g,
tr.b;
`
)
res.status(200).json(result)

View File

@ -4,6 +4,19 @@ import { GeneralDB, GisDB } from '../../constants/db';
import { pgQuery } from '../../utils/postgres';
const router = express.Router()
router.get('/type-roles', async (req: Request, res: Response) => {
try {
const result = await tediousQuery(
`
SELECT * FROM ${GisDB}..TypeRoles;
`
)
res.status(200).json(result)
} catch (err) {
res.status(500)
}
})
router.get('/bounds/:entity_type', async (req: Request, res: Response) => {
try {
const { entity_type } = req.params

View File

@ -10,7 +10,7 @@ const router = express.Router()
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, '..', 'public', 'temp'))
cb(null, path.join(__dirname, '..', '..', '..', 'public', 'temp'))
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname))

View File

@ -1,6 +1,7 @@
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import dbRouter from './api/db'
import generalRouter from './api/general'
import gisRouter from './api/gis'
import nodesRouter from './api/nodes'
@ -17,6 +18,7 @@ app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/db', dbRouter)
app.use('/general', generalRouter)
app.use('/gis', gisRouter)
app.use('/nodes', nodesRouter)