diff options
| author | yyamashita <yyamashita@mosquit.one> | 2026-05-15 00:22:37 +0900 |
|---|---|---|
| committer | yyamashita <yyamashita@mosquit.one> | 2026-05-15 00:22:37 +0900 |
| commit | 4ab34c8f0f9b4e1f636a26012f2a75863b471c51 (patch) | |
| tree | afae0d4cf96657d5c55a1d576e365f4d6b177c46 /app/routes/api-artists.tsx | |
| parent | eb02f582324aa9b57d287a7b5b809974f4954f98 (diff) | |
Fix build: merge detail/update into existing API routes
Parametric resource routes cause a React Router prod build error
(commonjs--resolver server-only module conflict). Merged the GET?id=
and PATCH logic into the existing api-bands.tsx and api-artists.tsx
instead of creating separate :uuid route files.
- GET /api/bands?id=UUID → band detail with links/members
- PATCH /api/bands → partial update (append_links, append_members)
- Same pattern for /api/artists
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'app/routes/api-artists.tsx')
| -rw-r--r-- | app/routes/api-artists.tsx | 57 |
1 files changed, 49 insertions, 8 deletions
diff --git a/app/routes/api-artists.tsx b/app/routes/api-artists.tsx index 6f83bcc..930311d 100644 --- a/app/routes/api-artists.tsx +++ b/app/routes/api-artists.tsx @@ -1,15 +1,27 @@ -import type { ActionFunctionArgs } from "react-router"; -import { createArtist, getIpAddress, listArtists, toSlug } from "~/lib/db.server"; +import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router"; +import { + createArtist, + getArtistById, + getArtistLinks, + getIpAddress, + listArtists, + toSlug, + updateArtist, +} from "~/lib/db.server"; -export function loader() { +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) { - if (request.method !== "POST") { - return Response.json({ error: "Method not allowed" }, { status: 405 }); - } - let body: Record<string, unknown>; try { body = await request.json(); @@ -17,6 +29,36 @@ export async function action({ request }: ActionFunctionArgs) { 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 }); @@ -41,4 +83,3 @@ export async function action({ request }: ActionFunctionArgs) { throw e; } } - |
