Newer
Older

Yusuf Akgül
committed
import { db } from "@/lib/db"
import { getCurrentUser } from "@/lib/session"
import { revalidatePath } from "next/cache"
import { NextRequest, NextResponse } from "next/server"

Yusuf Akgül
committed

Yusuf Akgül
committed
return NextResponse.json({ status: 401, message: 'Unauthorized' })
}

Yusuf Akgül
committed
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),

Yusuf Akgül
committed
}
}
})
}
const path = req.nextUrl.searchParams.get('path') || '/'
revalidatePath(path)
return NextResponse.json({ message: 'Game added' }, { status: 201 })

Yusuf Akgül
committed
} catch (error: any) {
return NextResponse.json({ message: error.message }, { status: 500 })

Yusuf Akgül
committed
}