fix(api): update songs endpoint to work with SpecialSong model

This commit is contained in:
Hördle Bot
2025-11-23 00:57:02 +01:00
parent 5944c14614
commit b27d5e49c9

View File

@@ -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);