diff options
| author | MohamedBassem <me@mbassem.com> | 2024-02-09 01:50:35 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-02-09 01:57:31 +0000 |
| commit | 08a5694e451218f1bcb2ad9eb42fd93250afbb96 (patch) | |
| tree | 2cc2351f26b0ab98268db4bc463c0c3aa3f78a3b /packages/web/app/api/v1/bookmarks/route.ts | |
| parent | c5bfa5000f178475d0b019b5a960916134b2ecfb (diff) | |
| download | karakeep-08a5694e451218f1bcb2ad9eb42fd93250afbb96.tar.zst | |
[refactor] Extract the bookmark model to be a high level model to support other type of bookmarks
Diffstat (limited to 'packages/web/app/api/v1/bookmarks/route.ts')
| -rw-r--r-- | packages/web/app/api/v1/bookmarks/route.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/web/app/api/v1/bookmarks/route.ts b/packages/web/app/api/v1/bookmarks/route.ts new file mode 100644 index 00000000..b9305ca8 --- /dev/null +++ b/packages/web/app/api/v1/bookmarks/route.ts @@ -0,0 +1,47 @@ +import { authOptions } from "@/lib/auth"; +import { bookmarkLink, getBookmarks } from "@/lib/services/bookmarks"; + +import { + zNewBookmarkRequestSchema, + ZGetBookmarksResponse, + ZBookmark, +} from "@/lib/types/api/bookmarks"; +import { getServerSession } from "next-auth"; +import { NextRequest, NextResponse } from "next/server"; + +export async function POST(request: NextRequest) { + // TODO: We probably should be using an API key here instead of the session; + const session = await getServerSession(authOptions); + if (!session) { + return new Response(null, { status: 401 }); + } + + const linkRequest = zNewBookmarkRequestSchema.safeParse(await request.json()); + + if (!linkRequest.success) { + return NextResponse.json( + { + error: linkRequest.error.toString(), + }, + { status: 400 }, + ); + } + + const bookmark = await bookmarkLink(linkRequest.data.url, session.user.id); + + let response: ZBookmark = { ...bookmark }; + return NextResponse.json(response, { status: 201 }); +} + +export async function GET() { + // TODO: We probably should be using an API key here instead of the session; + const session = await getServerSession(authOptions); + if (!session) { + return new Response(null, { status: 401 }); + } + + const bookmarks = await getBookmarks(session.user.id); + + let response: ZGetBookmarksResponse = { bookmarks }; + return NextResponse.json(response); +} |
