101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { PrismaClient } from '@prisma/client';
|
|
import { requireAdminAuth } from '@/lib/auth';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export async function GET() {
|
|
try {
|
|
const genres = await prisma.genre.findMany({
|
|
orderBy: { name: 'asc' },
|
|
include: {
|
|
_count: {
|
|
select: { songs: true }
|
|
}
|
|
}
|
|
});
|
|
return NextResponse.json(genres);
|
|
} catch (error) {
|
|
console.error('Error fetching genres:', error);
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
// Check authentication
|
|
const authError = await requireAdminAuth(request as any);
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const { name, subtitle, active } = await request.json();
|
|
|
|
if (!name || typeof name !== 'string') {
|
|
return NextResponse.json({ error: 'Invalid name' }, { status: 400 });
|
|
}
|
|
|
|
const genre = await prisma.genre.create({
|
|
data: {
|
|
name: name.trim(),
|
|
subtitle: subtitle ? subtitle.trim() : null,
|
|
active: active !== undefined ? active : true
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(genre);
|
|
} catch (error) {
|
|
console.error('Error creating genre:', error);
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: Request) {
|
|
// Check authentication
|
|
const authError = await requireAdminAuth(request as any);
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const { id } = await request.json();
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'Missing id' }, { status: 400 });
|
|
}
|
|
|
|
await prisma.genre.delete({
|
|
where: { id: Number(id) },
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Error deleting genre:', error);
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
// Check authentication
|
|
const authError = await requireAdminAuth(request as any);
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const { id, name, subtitle, active } = await request.json();
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'Missing id' }, { status: 400 });
|
|
}
|
|
|
|
const genre = await prisma.genre.update({
|
|
where: { id: Number(id) },
|
|
data: {
|
|
...(name && { name: name.trim() }),
|
|
subtitle: subtitle ? subtitle.trim() : null,
|
|
...(active !== undefined && { active })
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(genre);
|
|
} catch (error) {
|
|
console.error('Error updating genre:', error);
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|