You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { bookmarkSchema, newBookmarkSchema } from '@/types'
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { bookmark } from '@/lib/schema'
|
|
import { db } from '@/lib/db'
|
|
import { eq } from 'drizzle-orm'
|
|
|
|
export const GET = async () => {
|
|
const bookmarks = await db.select().from(bookmark)
|
|
|
|
return NextResponse.json(bookmarks)
|
|
}
|
|
|
|
export const POST = async (request: NextRequest) => {
|
|
const body = await request.json()
|
|
|
|
const newBookmark = newBookmarkSchema.safeParse(body)
|
|
|
|
if (newBookmark.success) {
|
|
await db.insert(bookmark).values(newBookmark.data)
|
|
|
|
return NextResponse.json({ status: 200 })
|
|
}
|
|
|
|
return NextResponse.json(newBookmark.error)
|
|
}
|
|
|
|
export const PATCH = async (request: NextRequest) => {
|
|
const body = await request.json()
|
|
|
|
const updateBookmark = bookmarkSchema.safeParse(body)
|
|
|
|
if (updateBookmark.success) {
|
|
await db
|
|
.update(bookmark)
|
|
.set({ ...updateBookmark.data })
|
|
.where(eq(bookmark.id, updateBookmark.data.id))
|
|
|
|
return NextResponse.json({ message: 'Bookmark Updated' })
|
|
}
|
|
|
|
return NextResponse.json(updateBookmark.error)
|
|
}
|
|
|
|
export const DELETE = async (request: NextRequest) => {
|
|
const body = await request.json()
|
|
|
|
const bookmarkId = body.bookmarkId
|
|
|
|
await db.delete(bookmark).where(eq(bookmark.id, bookmarkId))
|
|
|
|
return NextResponse.json({ message: 'Bookmark Deleted' })
|
|
}
|