import { db } from "@/lib/db" import { getCurrentUser } from "@/lib/session" import { revalidatePath } from "next/cache" import { NextRequest, NextResponse } from "next/server" export async function PUT(req: NextRequest) { const session = await getCurrentUser() if (!session) { return NextResponse.json({ status: 401, message: 'Unauthorized' }) } const userId = session.id const data = await req.json() data.gameId = parseInt(data.gameId) try { if (data.add) { await db.user.update({ where: { id: userId }, data: { favGameList: { push: data.gameId } } }) } else { const user = await db.user.findFirst({ where: { id: userId }, select: { favGameList: true }, }) await db.user.update({ where: { id: userId }, data: { favGameList: { set: user?.favGameList.filter((id: number) => id !== data.gameId), } } }) } const path = req.nextUrl.searchParams.get('path') || '/' revalidatePath(path) return NextResponse.json({ message: 'Game added' }, { status: 201 }) } catch (error: any) { return NextResponse.json({ message: error.message }, { status: 500 }) } }