forked from VinokurovVE/tests
154 lines
4.0 KiB
TypeScript
154 lines
4.0 KiB
TypeScript
import express, { Request, Response } from 'express';
|
|
import { tediousQuery } from '../../utils/tedious';
|
|
const router = express.Router()
|
|
|
|
router.get('/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)
|
|
}
|
|
})
|
|
|
|
router.get('/types/all', async (req: Request, res: Response) => {
|
|
try {
|
|
const result = await tediousQuery(
|
|
`
|
|
SELECT * FROM nGeneral..tTypes
|
|
ORDER BY id
|
|
`
|
|
)
|
|
res.status(200).json(result)
|
|
} catch (err) {
|
|
res.status(500)
|
|
}
|
|
})
|
|
|
|
router.get('/objects/all', async (req: Request, res: Response) => {
|
|
try {
|
|
const { offset, limit, city_id } = req.query
|
|
|
|
const result = await tediousQuery(
|
|
`
|
|
SELECT * FROM nGeneral..vObjects
|
|
${city_id ? `WHERE id_city = ${city_id}` : ''}
|
|
ORDER BY object_id
|
|
OFFSET ${Number(offset) || 0} ROWS
|
|
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
|
`
|
|
)
|
|
res.status(200).json(result)
|
|
} catch (err) {
|
|
res.status(500)
|
|
}
|
|
})
|
|
|
|
router.get('/objects/list', async (req: Request, res: Response) => {
|
|
try {
|
|
const { city_id, year, planning } = req.query
|
|
|
|
const result = await tediousQuery(
|
|
`
|
|
SELECT
|
|
tTypes.id AS id,
|
|
tTypes.name AS name,
|
|
COUNT(vObjects.type) AS count
|
|
FROM
|
|
vObjects
|
|
JOIN
|
|
tTypes ON vObjects.type = tTypes.id
|
|
WHERE
|
|
vObjects.id_city = ${city_id} AND vObjects.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
|
|
ELSE NULL
|
|
END
|
|
) = ${planning}
|
|
GROUP BY
|
|
tTypes.id,
|
|
tTypes.name;
|
|
`
|
|
)
|
|
res.status(200).json(result)
|
|
} catch (err) {
|
|
res.status(500)
|
|
}
|
|
})
|
|
|
|
router.get('/objects/:id([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})', async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const result = await tediousQuery(
|
|
`
|
|
SELECT * FROM nGeneral..vObjects
|
|
${id ? `WHERE object_id = '${id}'` : ''}
|
|
`
|
|
)
|
|
if (Array.isArray(result) && result.length > 0) {
|
|
res.status(200).json(result[0])
|
|
}
|
|
} catch (err) {
|
|
res.status(500)
|
|
}
|
|
})
|
|
|
|
router.get('/values/all', async (req: Request, res: Response) => {
|
|
try {
|
|
const { object_id } = req.query
|
|
|
|
if (!object_id) {
|
|
res.status(500)
|
|
}
|
|
|
|
const result = await tediousQuery(
|
|
`
|
|
SELECT * FROM nGeneral..tValues
|
|
WHERE id_object = '${object_id}'
|
|
`
|
|
)
|
|
|
|
res.status(200).json(result)
|
|
} catch (err) {
|
|
res.status(500)
|
|
}
|
|
})
|
|
|
|
router.get('/params/all', async (req: Request, res: Response) => {
|
|
try {
|
|
const { param_id } = req.query
|
|
|
|
if (!param_id) {
|
|
res.status(500)
|
|
}
|
|
|
|
const result = await tediousQuery(
|
|
`
|
|
SELECT * FROM nGeneral..tParameters
|
|
WHERE id = '${param_id}'
|
|
`
|
|
)
|
|
|
|
res.status(200).json(result)
|
|
} catch (err) {
|
|
res.status(500)
|
|
}
|
|
})
|
|
|
|
export default router |