Region/district/city bounds routes
This commit is contained in:
@ -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
37
ems/src/utils/postgres.ts
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user