65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import { PrismaClient, Special } from '@prisma/client';
|
|
import { NextResponse } from 'next/server';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export async function GET() {
|
|
const specials = await prisma.special.findMany({
|
|
orderBy: { name: 'asc' },
|
|
include: {
|
|
_count: {
|
|
select: { songs: true }
|
|
}
|
|
}
|
|
});
|
|
return NextResponse.json(specials);
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const { name, subtitle, maxAttempts = 7, unlockSteps = '[2,4,7,11,16,30,60]', launchDate, endDate, curator } = await request.json();
|
|
if (!name) {
|
|
return NextResponse.json({ error: 'Name is required' }, { status: 400 });
|
|
}
|
|
const special = await prisma.special.create({
|
|
data: {
|
|
name,
|
|
subtitle: subtitle || null,
|
|
maxAttempts: Number(maxAttempts),
|
|
unlockSteps,
|
|
launchDate: launchDate ? new Date(launchDate) : null,
|
|
endDate: endDate ? new Date(endDate) : null,
|
|
curator: curator || null,
|
|
},
|
|
});
|
|
return NextResponse.json(special);
|
|
}
|
|
|
|
export async function DELETE(request: Request) {
|
|
const { id } = await request.json();
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'ID required' }, { status: 400 });
|
|
}
|
|
await prisma.special.delete({ where: { id: Number(id) } });
|
|
return NextResponse.json({ success: true });
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
const { id, name, subtitle, maxAttempts, unlockSteps, launchDate, endDate, curator } = await request.json();
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'ID required' }, { status: 400 });
|
|
}
|
|
const updated = await prisma.special.update({
|
|
where: { id: Number(id) },
|
|
data: {
|
|
...(name && { name }),
|
|
subtitle: subtitle || null, // Allow clearing or setting
|
|
...(maxAttempts && { maxAttempts: Number(maxAttempts) }),
|
|
...(unlockSteps && { unlockSteps }),
|
|
launchDate: launchDate ? new Date(launchDate) : null,
|
|
endDate: endDate ? new Date(endDate) : null,
|
|
curator: curator || null,
|
|
},
|
|
});
|
|
return NextResponse.json(updated);
|
|
}
|