-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.ts
62 lines (54 loc) · 1.44 KB
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { NextResponse } from "next/server"
import getCurrentUser from "@/app/actions/getCurrentUser"
import prisma from "@/app/libs/prismadb"
interface IParams {
listingId: string
}
export async function POST(
request: Request,
{ params }: { params: IParams }
) {
const currentUser = await getCurrentUser()
if (!currentUser) {
return NextResponse.error()
}
const { listingId } = params
if (!listingId || typeof listingId !== "string") {
throw new Error("Invalid ID")
}
let favoriteIds = [...(currentUser.favoriteIds || [])]
favoriteIds.push(listingId)
const user = await prisma.user.update({
where: {
id: currentUser.id
},
data: {
favoriteIds
}
})
return NextResponse.json(user)
}
export async function DELETE(
request: Request,
{ params }: { params: IParams }
) {
const currentUser = await getCurrentUser()
if (!currentUser) {
return NextResponse.error()
}
const { listingId } = params
if (!listingId || typeof listingId !== "string") {
throw new Error("Invalid ID")
}
let favoriteIds = [...(currentUser.favoriteIds || [])]
favoriteIds = favoriteIds.filter((id) => id !== listingId)
const user = await prisma.user.update({
where: {
id: currentUser.id
},
data: {
favoriteIds
}
})
return NextResponse.json(user)
}