Skip to content
Snippets Groups Projects
route.ts 1.06 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
import { db } from "@/lib/db";
Yusuf Akgül's avatar
Yusuf Akgül committed
import { getCurrentUser } from "@/lib/session";
Yusuf Akgül's avatar
Yusuf Akgül committed
import { revalidatePath } from "next/cache";
import { NextRequest, NextResponse } from "next/server";
Yusuf Akgül's avatar
Yusuf Akgül committed
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

	try {
Yusuf Akgül's avatar
Yusuf Akgül committed
		await db.post.create({
Yusuf Akgül's avatar
Yusuf Akgül committed
			data: {
Yusuf Akgül's avatar
Yusuf Akgül committed
				content: content.gweet,
Yusuf Akgül's avatar
Yusuf Akgül committed
				userId: userId,
Yusuf Akgül's avatar
Yusuf Akgül committed
		})
		const path = req.nextUrl.searchParams.get('path') || '/';
		revalidatePath(path);
Yusuf Akgül's avatar
Yusuf Akgül committed

		return NextResponse.json({ status: 201, message: 'Message Created' })
Yusuf Akgül's avatar
Yusuf Akgül committed
	} catch (error: any) {
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() {
	try {
		const messages = await db.post.findMany({
			orderBy: {
				createdAt: "desc"
			},
			include: {
				user: true,
				Comment: true,
				Like: true
			},
		})

		return NextResponse.json(messages);
	} catch (error) {
		return NextResponse.json(error, { status: 500 });
	}
Yusuf Akgül's avatar
Yusuf Akgül committed
}