119 lines
3.0 KiB
TypeScript
119 lines
3.0 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from "next/server";
|
||
|
|
import { prisma } from "@/lib/prisma";
|
||
|
|
import { requireAuth } from "@/lib/auth";
|
||
|
|
import { getUserTier, FORUM_LIMITS } from "@/lib/tiers";
|
||
|
|
|
||
|
|
export async function GET(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const { searchParams } = new URL(request.url);
|
||
|
|
const categoryId = searchParams.get("categoryId");
|
||
|
|
|
||
|
|
const where: Record<string, unknown> = {};
|
||
|
|
|
||
|
|
if (categoryId) {
|
||
|
|
where.categoryId = categoryId;
|
||
|
|
}
|
||
|
|
|
||
|
|
const threads = await prisma.forumThread.findMany({
|
||
|
|
where,
|
||
|
|
include: {
|
||
|
|
author: {
|
||
|
|
select: { id: true, name: true, isPremium: true, isPro: true },
|
||
|
|
},
|
||
|
|
category: {
|
||
|
|
select: { id: true, name: true, icon: true },
|
||
|
|
},
|
||
|
|
_count: {
|
||
|
|
select: { posts: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
orderBy: [
|
||
|
|
{ pinned: "desc" },
|
||
|
|
{ createdAt: "desc" },
|
||
|
|
],
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ threads });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("GET /api/forum/threads error:", error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Internal server error" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function POST(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const auth = await requireAuth(request);
|
||
|
|
if (!auth) {
|
||
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = await prisma.user.findUnique({ where: { id: auth.userId } });
|
||
|
|
if (!user) {
|
||
|
|
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const tier = getUserTier(user.isPremium, user.isPro);
|
||
|
|
const limits = FORUM_LIMITS[tier];
|
||
|
|
|
||
|
|
if (!limits.canPost) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Your tier does not support posting. Upgrade to Premium or Pro to create threads." },
|
||
|
|
{ status: 403 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
const { title, content, categoryId } = await request.json();
|
||
|
|
|
||
|
|
if (!title || !content || !categoryId) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Missing required fields: title, content, categoryId" },
|
||
|
|
{ status: 400 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Verify category exists
|
||
|
|
const category = await prisma.forumCategory.findUnique({
|
||
|
|
where: { id: categoryId },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!category) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Invalid category" },
|
||
|
|
{ status: 400 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
const thread = await prisma.forumThread.create({
|
||
|
|
data: {
|
||
|
|
title,
|
||
|
|
content,
|
||
|
|
categoryId,
|
||
|
|
authorId: user.id,
|
||
|
|
shariahStatus: "pending",
|
||
|
|
},
|
||
|
|
include: {
|
||
|
|
author: {
|
||
|
|
select: { id: true, name: true, isPremium: true, isPro: true },
|
||
|
|
},
|
||
|
|
category: {
|
||
|
|
select: { id: true, name: true, icon: true },
|
||
|
|
},
|
||
|
|
_count: {
|
||
|
|
select: { posts: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ thread }, { status: 201 });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("POST /api/forum/threads error:", error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Internal server error" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|