From 1c0df5a6eadf20d3dce490b5c5c87a3ee750fe34 Mon Sep 17 00:00:00 2001 From: yyamashita Date: Thu, 14 May 2026 23:14:49 +0900 Subject: Add JSON REST API: GET /api/events, /api/events/:id, /api/venues, /api/openapi.json Co-Authored-By: Claude Sonnet 4.6 --- app/routes/api.events._index.ts | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 app/routes/api.events._index.ts (limited to 'app/routes/api.events._index.ts') diff --git a/app/routes/api.events._index.ts b/app/routes/api.events._index.ts new file mode 100644 index 0000000..cc5a288 --- /dev/null +++ b/app/routes/api.events._index.ts @@ -0,0 +1,47 @@ +/** + * GET /api/events + * + * Query params: + * date_from YYYY-MM-DD (default: today) + * date_to YYYY-MM-DD + * venue_id string + * keyword string (title or artist partial match) + * capacity_range small | medium | large + * area string + * limit 1–100 (default 60) + * offset integer (default 0) + */ +import type { Route } from "./+types/api.events._index"; +import { queryEvents } from "~/lib/db.server"; + +export async function loader({ request }: Route.LoaderArgs) { + const url = new URL(request.url); + const p = url.searchParams; + + const rawLimit = parseInt(p.get("limit") ?? "60", 10); + const limit = Number.isNaN(rawLimit) ? 60 : Math.min(Math.max(rawLimit, 1), 100); + const rawOffset = parseInt(p.get("offset") ?? "0", 10); + const offset = Number.isNaN(rawOffset) ? 0 : Math.max(rawOffset, 0); + + const capacityRaw = p.get("capacity_range"); + const capacity_range = + capacityRaw === "small" || capacityRaw === "medium" || capacityRaw === "large" + ? capacityRaw + : undefined; + + const events = queryEvents({ + date_from: p.get("date_from") ?? undefined, + date_to: p.get("date_to") ?? undefined, + venue_id: p.get("venue_id") ?? undefined, + keyword: p.get("keyword") ?? undefined, + area: p.get("area") ?? undefined, + capacity_range, + limit, + offset, + }); + + return Response.json( + { events, meta: { limit, offset, count: events.length } }, + { headers: { "Cache-Control": "public, max-age=300" } } + ); +} -- cgit v1.2.3