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 | |
| 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')
| -rw-r--r-- | packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts | 32 | ||||
| -rw-r--r-- | packages/web/app/api/v1/bookmarks/route.ts | 47 |
2 files changed, 79 insertions, 0 deletions
diff --git a/packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts b/packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts new file mode 100644 index 00000000..6adcf771 --- /dev/null +++ b/packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts @@ -0,0 +1,32 @@ +import { authOptions } from "@/lib/auth"; +import { deleteBookmark } from "@/lib/services/bookmarks"; +import { Prisma } from "@remember/db"; + +import { getServerSession } from "next-auth"; +import { NextRequest } from "next/server"; + +export async function DELETE( + _request: NextRequest, + { params }: { params: { bookmarkId: string } }, +) { + // 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 }); + } + + try { + await deleteBookmark(params.bookmarkId, session.user.id); + } catch (e: unknown) { + if ( + e instanceof Prisma.PrismaClientKnownRequestError && + e.code === "P2025" // RecordNotFound + ) { + return new Response(null, { status: 404 }); + } else { + throw e; + } + } + + return new Response(null, { status: 201 }); +} 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); +} |
