feat(discovery): POST /api/discover/[id] follow/want/dismiss actions
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { browseReleaseGroups } from "@/lib/musicbrainz";
|
||||
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return Response.json({ error: "invalid JSON" }, { status: 400 });
|
||||
}
|
||||
const { action } = (body ?? {}) as { action?: unknown };
|
||||
if (action !== "follow" && action !== "want" && action !== "dismiss") {
|
||||
return Response.json({ error: "action must be follow, want, or dismiss" }, { status: 400 });
|
||||
}
|
||||
|
||||
const s = await prisma.discoverySuggestion.findUnique({ where: { id } });
|
||||
if (!s) return Response.json({ error: "not found" }, { status: 404 });
|
||||
|
||||
if (action === "dismiss") {
|
||||
await prisma.discoverySuggestion.update({ where: { id }, data: { status: "dismissed" } });
|
||||
return Response.json({ id, status: "dismissed" });
|
||||
}
|
||||
|
||||
if (action === "follow") {
|
||||
if (s.kind !== "artist") {
|
||||
return Response.json({ error: "follow applies to artist suggestions" }, { status: 400 });
|
||||
}
|
||||
const existing = await prisma.watchedArtist.findUnique({ where: { mbid: s.artistMbid } });
|
||||
if (!existing) {
|
||||
let releases;
|
||||
try {
|
||||
releases = await browseReleaseGroups(s.artistMbid);
|
||||
} catch {
|
||||
return Response.json({ error: "MusicBrainz unavailable" }, { status: 502 });
|
||||
}
|
||||
const created = await prisma.watchedArtist.create({
|
||||
data: { mbid: s.artistMbid, name: s.artistName },
|
||||
});
|
||||
if (releases.length > 0) {
|
||||
await prisma.monitoredRelease.createMany({
|
||||
data: releases.map((r) => ({
|
||||
watchedArtistId: created.id,
|
||||
artistMbid: s.artistMbid,
|
||||
artistName: s.artistName,
|
||||
rgMbid: r.rgMbid,
|
||||
album: r.title,
|
||||
primaryType: r.primaryType,
|
||||
secondaryTypes: r.secondaryTypes,
|
||||
firstReleaseDate: r.firstReleaseDate,
|
||||
monitored: false,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
await prisma.discoverySuggestion.update({ where: { id }, data: { status: "followed" } });
|
||||
return Response.json({ id, status: "followed" });
|
||||
}
|
||||
|
||||
// action === "want"
|
||||
if (s.kind !== "album" || !s.rgMbid) {
|
||||
return Response.json({ error: "want applies to album suggestions" }, { status: 400 });
|
||||
}
|
||||
await prisma.monitoredRelease.upsert({
|
||||
where: { rgMbid: s.rgMbid },
|
||||
create: {
|
||||
artistMbid: s.artistMbid,
|
||||
artistName: s.artistName,
|
||||
rgMbid: s.rgMbid,
|
||||
album: s.album ?? "",
|
||||
primaryType: s.primaryType,
|
||||
secondaryTypes: s.secondaryTypes,
|
||||
firstReleaseDate: s.firstReleaseDate,
|
||||
monitored: true,
|
||||
},
|
||||
update: { monitored: true },
|
||||
});
|
||||
await prisma.discoverySuggestion.update({ where: { id }, data: { status: "wanted" } });
|
||||
return Response.json({ id, status: "wanted" });
|
||||
}
|
||||
Reference in New Issue
Block a user