1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import type { ActionFunctionArgs } from "react-router";
import { createBand, getIpAddress, listBands, toSlug, type MemberInput } from "~/lib/db.server";
export function loader() {
return Response.json(listBands());
}
export async function action({ request }: ActionFunctionArgs) {
if (request.method !== "POST") {
return Response.json({ error: "Method not allowed" }, { status: 405 });
}
let body: Record<string, unknown>;
try {
body = await request.json();
} catch {
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
}
const name = (body.name as string | undefined)?.trim();
if (!name) return Response.json({ error: "name is required" }, { status: 400 });
const slug = (body.slug as string | undefined)?.trim() || toSlug(name);
if (!slug) return Response.json({ error: "could not derive slug from name" }, { status: 400 });
const id = crypto.randomUUID();
try {
const band = createBand({
id,
slug,
name,
area: (body.area as string) || null,
description: (body.description as string) || null,
status: (body.status as string) || "active",
links: (body.links as { label: string; url: string }[]) || [],
members: (body.members as MemberInput[]) || [],
message: (body.message as string) || "API import",
ip_address: getIpAddress(request),
});
return Response.json(band, { status: 201 });
} catch (e) {
if (e instanceof Error && e.message.includes("UNIQUE constraint failed: bands.slug")) {
return Response.json({ error: "slug already in use" }, { status: 409 });
}
throw e;
}
}
export default function () {
return null;
}
|