diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/root.tsx | 10 | ||||
| -rw-r--r-- | app/routes.ts | 2 | ||||
| -rw-r--r-- | app/routes/api-artist-detail.tsx | 49 | ||||
| -rw-r--r-- | app/routes/api-band-detail.tsx | 69 |
4 files changed, 124 insertions, 6 deletions
diff --git a/app/root.tsx b/app/root.tsx index e30abe8..15cc68b 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -46,12 +46,10 @@ export function Layout({ children }: { children: React.ReactNode }) { export default function App() { return ( <> - <nav> - <div> - <Link to="/" className="logo">whois.band</Link> - <Link to="/bands">Bands</Link> - <Link to="/artists">Artists</Link> - </div> + <nav className="nav"> + <Link to="/" className="nav-brand">whois.band</Link> + <Link to="/bands">Bands</Link> + <Link to="/artists">Artists</Link> </nav> <Outlet /> </> diff --git a/app/routes.ts b/app/routes.ts index 0a2c028..518492f 100644 --- a/app/routes.ts +++ b/app/routes.ts @@ -3,7 +3,9 @@ import { type RouteConfig, index, route } from "@react-router/dev/routes"; export default [ index("routes/home.tsx"), route("/api/bands", "routes/api-bands.tsx"), + route("/api/bands/:uuid", "routes/api-band-detail.tsx"), route("/api/artists", "routes/api-artists.tsx"), + route("/api/artists/:uuid", "routes/api-artist-detail.tsx"), route("/api/export", "routes/api-export.tsx"), route("/api/import", "routes/api-import.tsx"), route("/bands", "routes/band-index.tsx"), diff --git a/app/routes/api-artist-detail.tsx b/app/routes/api-artist-detail.tsx new file mode 100644 index 0000000..1d874ad --- /dev/null +++ b/app/routes/api-artist-detail.tsx @@ -0,0 +1,49 @@ +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)); +} diff --git a/app/routes/api-band-detail.tsx b/app/routes/api-band-detail.tsx new file mode 100644 index 0000000..aa3b5bb --- /dev/null +++ b/app/routes/api-band-detail.tsx @@ -0,0 +1,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)); +} |
