forked from VinokurovVE/tests
Map testing, custom table based on Tanstack Table
This commit is contained in:
163
ems/src/index.ts
163
ems/src/index.ts
@ -9,6 +9,33 @@ import cors from 'cors'
|
||||
import { Coordinate } from './interfaces/map'
|
||||
import { generateTilesForZoomLevel } from './utils/tiles'
|
||||
import { query, validationResult } from 'express-validator'
|
||||
import { Connection, Request as TediousRequest } from 'tedious'
|
||||
|
||||
const tedious = new Connection({
|
||||
server: 'localhost',
|
||||
options: {
|
||||
trustServerCertificate: true,
|
||||
port: 1433,
|
||||
database: 'nGeneral'
|
||||
},
|
||||
authentication: {
|
||||
type: 'default',
|
||||
options: {
|
||||
userName: 'SA',
|
||||
password: 'oMhthmsvbYHc'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
tedious.on('connect', function (err) {
|
||||
if (err) {
|
||||
console.log('Error: ', err)
|
||||
}
|
||||
|
||||
console.log('Connected to General')
|
||||
})
|
||||
|
||||
tedious.connect()
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
const app = express()
|
||||
@ -154,6 +181,138 @@ app.get('/tile/:provider/:z/:x/:y', async (req: Request, res: Response) => {
|
||||
res.status(404).send('Tile is not generated or not provided')
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
||||
function tediousQuery(query: string) {
|
||||
// Read all rows from table
|
||||
const request = new TediousRequest(
|
||||
query,
|
||||
(err, rowCount) => {
|
||||
if (err) {
|
||||
console.log(`Executing ${query}, ${rowCount} rows.`);
|
||||
console.error(err.message);
|
||||
} else {
|
||||
console.log(`Executing ${query}, ${rowCount} rows.`);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const result: any = [];
|
||||
|
||||
request.on("row", (columns) => {
|
||||
const entry: any = {};
|
||||
columns.forEach((column: any) => {
|
||||
entry[column.metadata.colName] = column.value;
|
||||
});
|
||||
result.push(entry);
|
||||
});
|
||||
|
||||
request.on('error', error => reject(error));// some error happened, reject the promise
|
||||
request.on('requestCompleted', () => resolve(result)); // resolve the promise with the result rows.
|
||||
|
||||
tedious.execSql(request);
|
||||
});
|
||||
}
|
||||
|
||||
app.get('/general/cities/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { offset, limit, search, id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..Cities
|
||||
${id ? `WHERE id = '${id}'` : ''}
|
||||
${search ? `WHERE name LIKE '%${search || ''}%'` : ''}
|
||||
ORDER BY id
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/general/objects/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { offset, limit, city_id, id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..tObjects
|
||||
${city_id ? `WHERE id_city = ${city_id}` : ''}
|
||||
${id ? `WHERE id = '${id}'` : ''}
|
||||
ORDER BY id
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/gis/images/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { offset, limit, city_id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM New_Gis..images
|
||||
${city_id ? `WHERE city_id = ${city_id}` : ''}
|
||||
ORDER BY city_id
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// Get figures by year and city id
|
||||
app.get('/gis/figures/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { offset, limit, object_id, year, city_id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM New_Gis..figures f
|
||||
JOIN nGeneral..tObjects o ON f.object_id = o.id WHERE o.id_city = ${city_id} AND f.year = ${year}
|
||||
ORDER BY f.year
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
// Get lines by year and city id
|
||||
app.get('/gis/lines/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { offset, limit, object_id, year, city_id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM New_Gis..lines l
|
||||
JOIN nGeneral..tObjects o ON l.object_id = o.id WHERE o.id_city = ${city_id} AND l.year = ${year}
|
||||
ORDER BY l.year
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
Reference in New Issue
Block a user