Skip to content
Snippets Groups Projects
route.ts 1.66 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
import { getCurrentUser } from "@/lib/session";
import { revalidatePath } from "next/cache";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
Yusuf Akgül's avatar
Yusuf Akgül committed
    const user = await getCurrentUser();
Yusuf Akgül's avatar
Yusuf Akgül committed
    if (!user) {
        return NextResponse.json({ status: 401, message: 'Unauthorized' });
Yusuf Akgül's avatar
Yusuf Akgül committed
    const userId = user.id;
    const content = await req.json()
Yusuf Akgül's avatar
Yusuf Akgül committed
                message: content.gweet,
                postId: content.postId,
                userId: userId,
            }
        })
        const path = req.nextUrl.searchParams.get('path') || '/';
        revalidatePath(path);

Yusuf Akgül's avatar
Yusuf Akgül committed
        return NextResponse.json({ status: 201, message: 'Comment Created' })
Yusuf Akgül's avatar
Yusuf Akgül committed
        return NextResponse.json({ status: 500, message: error.message })
Yusuf Akgül's avatar
Yusuf Akgül committed
export async function GET(req: NextRequest): Promise<NextResponse> {
    const pa = req.nextUrl.searchParams;

Yusuf Akgül's avatar
Yusuf Akgül committed
        const p = pa.get('postid')
Yusuf Akgül's avatar
Yusuf Akgül committed
        if (!p) {
            return NextResponse.json({ status: 400, message: 'Bad Request' })
        }
Yusuf Akgül's avatar
Yusuf Akgül committed
        const message = await db.post.findUnique({
            where: {
                id: p
            },
            include: {
                user: true,
            },
        })

        const comments = await db.comment.findMany({
            where: {
                postId: p
            },
Yusuf Akgül's avatar
Yusuf Akgül committed
        return NextResponse.json(comments);
Yusuf Akgül's avatar
Yusuf Akgül committed
        return NextResponse.json(error, { status: 500 });