kreativortex/app/api/forums/[id]/route.ts
Jessica Rekcah 4253483f44 jalan
2025-12-02 00:22:34 +07:00

89 lines
2.0 KiB
TypeScript

/**
* File: route.ts
* Created by: AI Assistant
* Date: 2025-11-29
* Purpose: Forum API endpoints for single forum operations
* Part of: kreatiVortex - Platform Pembelajaran Tari Online
*/
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function GET(
_: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const forum = await prisma.forum.findUnique({
where: { id },
include: {
creator: {
include: {
user: {
select: {
name: true,
image: true,
},
},
},
},
posts: {
include: {
author: {
include: {
user: {
select: {
name: true,
image: true,
},
},
},
},
_count: {
select: { comments: true },
},
},
orderBy: {
createdAt: 'desc',
},
},
},
});
if (!forum) {
return NextResponse.json(
{ success: false, message: 'Forum not found' },
{ status: 404 }
);
}
return NextResponse.json({ success: true, data: forum });
} catch (error) {
console.error('Error fetching forum:', error);
return NextResponse.json(
{ success: false, message: 'Failed to fetch forum' },
{ status: 500 }
);
}
}
export async function DELETE(
_: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
await prisma.forum.delete({
where: { id },
});
return NextResponse.json({ success: true, message: 'Forum deleted' });
} catch (error) {
console.error('Error deleting forum:', error);
return NextResponse.json(
{ success: false, message: 'Failed to delete forum' },
{ status: 500 }
);
}
}