import { prisma } from "@/lib/db"; import { yearFromPath } from "@/lib/library-path"; // Owned albums (LibraryItem) enriched with the release-group MBID + year from the matching // MonitoredRelease (joined on artist/album), so the Library grid can show cover art + tracks. export async function GET() { const [items, releases] = await Promise.all([ prisma.libraryItem.findMany({ orderBy: { importedAt: "desc" } }), prisma.monitoredRelease.findMany({ select: { id: true, artistName: true, album: true, rgMbid: true, firstReleaseDate: true, primaryType: true, artistMbid: true }, }), ]); const key = (artist: string, album: string) => `${artist}${album}`; const byKey = new Map(releases.map((r) => [key(r.artistName, r.album), r])); return Response.json({ albums: items.map((li) => { const mr = byKey.get(key(li.artist, li.album)); return { id: li.id, artist: li.artist, album: li.album, format: li.format, qualityClass: li.qualityClass, importedAt: li.importedAt, rgMbid: li.rgMbid ?? mr?.rgMbid ?? null, // prefer the resolved release year; fall back to the "(YYYY)" in the folder name so // manually-imported / metadata-edited albums (no matching release) still show a year year: mr?.firstReleaseDate?.slice(0, 4) ?? yearFromPath(li.path), primaryType: mr?.primaryType ?? null, artistMbid: li.artistMbid ?? mr?.artistMbid ?? null, monitoredReleaseId: mr?.id ?? null, trackNames: li.trackNames, }; }), }); }