Region/district/city bounds routes

This commit is contained in:
cracklesparkle
2025-01-14 14:32:41 +09:00
parent 2bf657e8ed
commit e6b3dc05d3
6 changed files with 132 additions and 46 deletions

View File

@ -23,7 +23,7 @@
"ioredis": "^5.4.1",
"md5": "^2.3.0",
"multer": "^1.4.5-lts.1",
"pg": "^8.13.0",
"pg": "^8.13.1",
"pump": "^3.0.0",
"sharp": "^0.33.5",
"tedious": "^18.6.1"
@ -35,6 +35,7 @@
"@types/md5": "^2.3.5",
"@types/multer": "^1.4.12",
"@types/node": "^22.4.1",
"@types/pg": "^8.11.10",
"@types/pump": "^1.1.3",
"@types/redis": "^4.0.11",
"nodemon": "^3.1.4",

View File

@ -1,8 +1,73 @@
import express, { Request, Response } from 'express';
import { tediousQuery } from '../../utils/tedious';
import { GeneralDB, GisDB } from '../../constants/db';
import { pgQuery } from '../../utils/postgres';
const router = express.Router()
router.get('/bounds/:entity_type', 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)
}
})
router.get('/images/all', async (req: Request, res: Response) => {
try {
const { offset, limit, city_id } = req.query

37
ems/src/utils/postgres.ts Normal file
View File

@ -0,0 +1,37 @@
import { Pool } from 'pg';
import 'dotenv/config';
// Environment variables for database configuration
const PG_HOST = process.env.PG_HOST || 'localhost';
const PG_PORT = Number(process.env.PG_PORT) || 5432;
const PG_USER = process.env.PG_USER || 'postgres';
const PG_PASSWORD = process.env.PG_PASSWORD || '';
const PG_DB = process.env.PG_DB || 'postgres';
// Create a connection pool
const pool = new Pool({
host: PG_HOST,
port: PG_PORT,
user: PG_USER,
password: PG_PASSWORD,
database: PG_DB,
});
export async function pgQuery(query: string, params: any[] = []) {
try {
// Get a client from the pool
const client = await pool.connect();
try {
// Execute the query with parameters
const result = await client.query(query, params);
return result.rows; // Return only the rows
} finally {
// Release the client back to the pool
client.release();
}
} catch (err) {
// Log error and rethrow it
console.error(`Error executing query: ${query}`, err);
throw err;
}
}