summaryrefslogtreecommitdiff
path: root/app/routes/api-artists.tsx
blob: 930311d6dc9277acfb30733c4553f7c1602efb4d (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import {
  createArtist,
  getArtistById,
  getArtistLinks,
  getIpAddress,
  listArtists,
  toSlug,
  updateArtist,
} from "~/lib/db.server";

export function loader({ request }: LoaderFunctionArgs) {
  const url = new URL(request.url);
  const id = url.searchParams.get("id");
  if (id) {
    const artist = getArtistById(id);
    if (!artist) return Response.json({ error: "Not found" }, { status: 404 });
    const links = getArtistLinks(artist.id);
    return Response.json({ ...artist, links });
  }
  return Response.json(listArtists());
}

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

  if (request.method === "PATCH") {
    const id = body.id as string | undefined;
    if (!id) return Response.json({ error: "id is required" }, { status: 400 });
    const artist = getArtistById(id);
    if (!artist) return Response.json({ error: "Not found" }, { status: 404 });

    const currentLinks = getArtistLinks(artist.id);
    const patchLinks = (body.links as { label: string; url: string }[] | undefined) ?? [];
    const appendLinks = body.append_links !== 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;

    updateArtist(artist.id, {
      slug: (body.slug as string | undefined) ?? artist.slug,
      name: (body.name as string | undefined) ?? artist.name,
      links: newLinks,
      message: (body.message as string | undefined) || "API update",
      ip_address: getIpAddress(request),
    });

    return Response.json(getArtistById(artist.id));
  }

  if (request.method !== "POST") {
    return Response.json({ error: "Method not allowed" }, { status: 405 });
  }

  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 artist = createArtist({
      id,
      slug,
      name,
      links: (body.links as { label: string; url: string }[]) || [],
      message: (body.message as string) || "API import",
      ip_address: getIpAddress(request),
    });
    return Response.json(artist, { status: 201 });
  } catch (e) {
    if (e instanceof Error && e.message.includes("UNIQUE constraint failed: artists.slug")) {
      return Response.json({ error: "slug already in use" }, { status: 409 });
    }
    throw e;
  }
}