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, LoaderFunctionArgs } from "react-router";
import {
getArtistById,
getArtistLinks,
getIpAddress,
updateArtist,
} from "~/lib/db.server";
export function loader({ params }: LoaderFunctionArgs) {
const artist = getArtistById(params.uuid!);
if (!artist) return Response.json({ error: "Not found" }, { status: 404 });
const links = getArtistLinks(artist.id);
return Response.json({ ...artist, links });
}
export async function action({ request, params }: ActionFunctionArgs) {
if (request.method !== "PATCH") {
return Response.json({ error: "Method not allowed" }, { status: 405 });
}
const artist = getArtistById(params.uuid!);
if (!artist) 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 = 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));
}
export default function () { return null; }
|