import { db } from "@/lib/db"; import { getCurrentUser } from "@/lib/session"; import { revalidatePath } from "next/cache"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { const user = await getCurrentUser(); if (!user) { return NextResponse.json({ status: 401, message: 'Unauthorized' }); } const userId = user.id; const content = await req.json() try { await db.comment.create({ data: { message: content.gweet, postId: content.postId, userId: userId, } }) const path = req.nextUrl.searchParams.get('path') || '/'; revalidatePath(path); return NextResponse.json({ status: 201, message: 'Comment Created' }) } catch (error: any) { return NextResponse.json({ status: 500, message: error.message }) } } export async function GET(req: NextRequest): Promise<NextResponse> { const pa = req.nextUrl.searchParams; try { const p = pa.get('postid') if (!p) { return NextResponse.json({ status: 400, message: 'Bad Request' }) } const message = await db.post.findUnique({ where: { id: p }, include: { user: true, }, }) const comments = await db.comment.findMany({ where: { postId: p }, orderBy: { createdAt: "desc" } }) return NextResponse.json(comments); } catch (error) { return NextResponse.json(error, { status: 500 }); } }