add legislation REST endpoints, mount at /legislation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 16:48:41 +01:00
parent 03b704e483
commit 231eb6f6bc
3 changed files with 63 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
import { Hono } from "hono"
import { castVoteSchema } from "./schema"
import {
castVote,
getLegislation,
getLegislationText,
getUpcomingLegislation,
getUserVote,
} from "./service"
export const legislationRouter = new Hono()
// static routes FIRST — before /:id param routes
legislationRouter.get("/upcoming", async (c) => {
const items = await getUpcomingLegislation()
return c.json(items)
})
legislationRouter.get("/:id", async (c) => {
const id = Number(c.req.param("id"))
if (Number.isNaN(id)) return c.json({ error: "invalid id" }, 400)
const legislation = await getLegislation(id)
if (!legislation) return c.json({ error: "not found" }, 404)
return c.json(legislation)
})
legislationRouter.get("/:id/text", async (c) => {
const id = Number(c.req.param("id"))
if (Number.isNaN(id)) return c.json({ error: "invalid id" }, 400)
const text = await getLegislationText(id)
if (text === null) return c.json({ error: "not found" }, 404)
return c.json({ text })
})
legislationRouter.post("/:id/vote", async (c) => {
const id = Number(c.req.param("id"))
if (Number.isNaN(id)) return c.json({ error: "invalid id" }, 400)
const body = await c.req.json()
const parsed = castVoteSchema.safeParse(body)
if (!parsed.success) return c.json({ error: parsed.error.flatten() }, 400)
await castVote(id, parsed.data)
return c.json({ ok: true }, 201)
})
legislationRouter.get("/:id/vote/:deviceId", async (c) => {
const id = Number(c.req.param("id"))
const deviceId = c.req.param("deviceId")
if (Number.isNaN(id)) return c.json({ error: "invalid id" }, 400)
const vote = await getUserVote(id, deviceId)
if (!vote) return c.json({ error: "no vote found" }, 404)
return c.json(vote)
})