summaryrefslogtreecommitdiff
path: root/app/routes/api-band-detail.tsx
blob: aa3b5bbd69db41b2eddda2ca7d96e024a92d5386 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import {
  getBandById,
  getBandLinks,
  getBandMembers,
  getIpAddress,
  toSlug,
  updateBand,
  type MemberInput,
} from "~/lib/db.server";

export function loader({ params }: LoaderFunctionArgs) {
  const band = getBandById(params.uuid!);
  if (!band) return Response.json({ error: "Not found" }, { status: 404 });
  const links = getBandLinks(band.id);
  const members = getBandMembers(band.id);
  return Response.json({ ...band, links, members });
}

export async function action({ request, params }: ActionFunctionArgs) {
  if (request.method !== "PATCH") {
    return Response.json({ error: "Method not allowed" }, { status: 405 });
  }

  const band = getBandById(params.uuid!);
  if (!band) return Response.json({ error: "Not found" }, { status: 404 });

  let body: Record<string, unknown>;
  try {
    body = await request.json();
  } catch {
    return Response.json({ error: "Invalid JSON body" }, { status: 400 });
  }

  const currentLinks = getBandLinks(band.id);
  const currentMembers = getBandMembers(band.id);

  const patchLinks = (body.links as { label: string; url: string }[] | undefined) ?? [];
  const patchMembers = (body.members as MemberInput[] | undefined) ?? [];
  const appendLinks = body.append_links !== false;
  const appendMembers = body.append_members !== false;

  const existingUrls = new Set(currentLinks.map((l) => l.url));
  const newLinks = appendLinks
    ? [...currentLinks.map((l) => ({ label: l.label, url: l.url })), ...patchLinks.filter((l) => !existingUrls.has(l.url))]
    : patchLinks;

  const existingArtistIds = new Set(currentMembers.map((m) => m.artist_id));
  const newMembers: MemberInput[] = appendMembers
    ? [
        ...currentMembers.map((m) => ({ artist_id: m.artist_id, role: m.role, since: m.since ?? "", until: m.until ?? "", note: m.note ?? "" })),
        ...patchMembers.filter((m) => !existingArtistIds.has(m.artist_id)),
      ]
    : patchMembers;

  updateBand(band.id, {
    slug: (body.slug as string | undefined) ?? band.slug,
    name: (body.name as string | undefined) ?? band.name,
    area: (body.area as string | undefined) ?? band.area,
    description: "description" in body ? (body.description as string | null) : band.description,
    status: (body.status as string | undefined) ?? band.status,
    links: newLinks,
    members: newMembers,
    message: (body.message as string | undefined) || "API update",
    ip_address: getIpAddress(request),
  });

  return Response.json(getBandById(band.id));
}