refactor
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user