From b27d5e49c9fddbed6630831d7ab9a41477fd59b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=B6rdle=20Bot?= Date: Sun, 23 Nov 2025 00:57:02 +0100 Subject: [PATCH] fix(api): update songs endpoint to work with SpecialSong model --- app/api/songs/route.ts | 55 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/app/api/songs/route.ts b/app/api/songs/route.ts index 356998d..97e2567 100644 --- a/app/api/songs/route.ts +++ b/app/api/songs/route.ts @@ -12,11 +12,15 @@ export async function GET() { include: { puzzles: true, genres: true, - specials: true, + specials: { + include: { + special: true + } + }, }, }); - // Map to include activation count + // Map to include activation count and flatten specials const songsWithActivations = songs.map(song => ({ id: song.id, title: song.title, @@ -27,7 +31,7 @@ export async function GET() { activations: song.puzzles.length, puzzles: song.puzzles, genres: song.genres, - specials: song.specials, + specials: song.specials.map(ss => ss.special), })); return NextResponse.json(songsWithActivations); @@ -178,16 +182,51 @@ export async function PUT(request: Request) { }; } - if (specialIds) { - data.specials = { - set: specialIds.map((sId: number) => ({ id: sId })) - }; + // Handle SpecialSong relations separately + if (specialIds !== undefined) { + // First, get current special assignments + const currentSpecials = await prisma.specialSong.findMany({ + where: { songId: Number(id) } + }); + + const currentSpecialIds = currentSpecials.map(ss => ss.specialId); + const newSpecialIds = specialIds as number[]; + + // Delete removed specials + const toDelete = currentSpecialIds.filter(sid => !newSpecialIds.includes(sid)); + if (toDelete.length > 0) { + await prisma.specialSong.deleteMany({ + where: { + songId: Number(id), + specialId: { in: toDelete } + } + }); + } + + // Add new specials + const toAdd = newSpecialIds.filter(sid => !currentSpecialIds.includes(sid)); + if (toAdd.length > 0) { + await prisma.specialSong.createMany({ + data: toAdd.map(specialId => ({ + songId: Number(id), + specialId, + startTime: 0 + })) + }); + } } const updatedSong = await prisma.song.update({ where: { id: Number(id) }, data, - include: { genres: true, specials: true } + include: { + genres: true, + specials: { + include: { + special: true + } + } + } }); return NextResponse.json(updatedSong);