summaryrefslogtreecommitdiff
path: root/app/routes/api.events._index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/routes/api.events._index.ts')
-rw-r--r--app/routes/api.events._index.ts47
1 files changed, 47 insertions, 0 deletions
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" } }
+ );
+}