This commit is contained in:
taDuc
2026-04-20 23:27:38 +07:00
parent 7804ef842b
commit 6105a4c8da
4 changed files with 276 additions and 78 deletions

View File

@@ -1,5 +1,6 @@
const express = require("express");
const db = require("../db/polygons");
const { normalizeEntityContract } = require("../types/contracts");
const router = express.Router();
@@ -29,7 +30,7 @@ router.get("/", (req, res) => {
`;
const rows = db.prepare(sql).all(...params);
res.json(rows.map(normalizeEntityRow));
res.json(rows.map(normalizeEntityContract));
});
router.get("/search", (req, res) => {
@@ -55,7 +56,7 @@ router.get("/search", (req, res) => {
LIMIT ?
`).all(pattern, limit);
res.json(rows.map(normalizeEntityRow));
res.json(rows.map(normalizeEntityContract));
});
router.get("/:id", (req, res) => {
@@ -75,25 +76,11 @@ router.get("/:id", (req, res) => {
return res.status(404).json({ error: "Entity not found" });
}
res.json(normalizeEntityRow(row));
res.json(normalizeEntityContract(row));
});
module.exports = router;
function normalizeEntityRow(row) {
return {
id: row.id,
name: row.name,
slug: row.slug,
description: row.description,
type_id: row.type_id,
status: row.status,
created_at: row.created_at,
updated_at: row.updated_at,
geometry_count: Number(row.geometry_count || 0),
};
}
function normalizeLimit(value, fallback = 25, max = 100) {
const num = Number(value);
if (!Number.isFinite(num)) return fallback;