-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.ts
98 lines (80 loc) · 2.19 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { NextResponse } from 'next/server'
import prisma from '@/utils/prisma'
import isAuth from '@/utils/talksAuth'
type TalksItem = {
content: string
images?: string[]
tags?: string[]
}
// Talks API KEY
const API_KEY = process.env.TALKS_API_KEY
// GET: get talks data
export async function GET() {
try {
const data = await prisma.talks.findMany()
return NextResponse.json({ code: 200, message: 'success', data })
} catch (error) {
return NextResponse.json({ code: 404, message: 'Not Found' })
}
}
// POST: Add talks data
export async function POST(request: Request) {
if (!isAuth(request, API_KEY)) {
return NextResponse.json({ code: 401, message: 'Unauthorized' })
}
try {
const { content, images, tags }: TalksItem = await request.json()
await prisma.talks.create({
data: {
content,
images,
tags,
},
})
return NextResponse.json({ code: 200, message: 'success' })
} catch (error) {
return NextResponse.json({ code: 500, message: 'Internal Server Error' })
}
}
// Delete: Delete talks data
export async function DELETE(request: Request) {
if (!isAuth(request, API_KEY)) {
return NextResponse.json({ code: 401, message: 'Unauthorized' })
}
try {
const url = new URL(request.url)
const id = url.searchParams.get('id')
await prisma.talks.delete({
where: {
id,
},
})
return NextResponse.json({ code: 200, message: 'success' })
} catch (error) {
return NextResponse.json({ code: 500, message: 'Internal Server Error' })
}
}
// Update: Modify talks data
export async function PUT(request: Request) {
if (!isAuth(request, API_KEY)) {
return NextResponse.json({ code: 401, message: 'Unauthorized' })
}
try {
const url = new URL(request.url)
const id = url.searchParams.get('id')
const { content, images, tags }: TalksItem = await request.json()
await prisma.talks.update({
where: {
id,
},
data: {
content,
images,
tags,
},
})
return NextResponse.json({ code: 200, message: 'success' })
} catch (error) {
return NextResponse.json({ code: 500, message: 'Internal Server Error' })
}
}