From 08a5694e451218f1bcb2ad9eb42fd93250afbb96 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 9 Feb 2024 01:50:35 +0000 Subject: [refactor] Extract the bookmark model to be a high level model to support other type of bookmarks --- .../web/app/api/v1/bookmarks/[bookmarkId]/route.ts | 32 ++++++++++++++ packages/web/app/api/v1/bookmarks/route.ts | 47 +++++++++++++++++++++ packages/web/app/api/v1/links/[linkId]/route.ts | 32 -------------- packages/web/app/api/v1/links/route.ts | 49 ---------------------- 4 files changed, 79 insertions(+), 81 deletions(-) create mode 100644 packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts create mode 100644 packages/web/app/api/v1/bookmarks/route.ts delete mode 100644 packages/web/app/api/v1/links/[linkId]/route.ts delete mode 100644 packages/web/app/api/v1/links/route.ts (limited to 'packages/web/app/api') 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); +} diff --git a/packages/web/app/api/v1/links/[linkId]/route.ts b/packages/web/app/api/v1/links/[linkId]/route.ts deleted file mode 100644 index 39449d6d..00000000 --- a/packages/web/app/api/v1/links/[linkId]/route.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { authOptions } from "@/lib/auth"; -import { unbookmarkLink } from "@/lib/services/links"; -import { Prisma } from "@remember/db"; - -import { getServerSession } from "next-auth"; -import { NextRequest } from "next/server"; - -export async function DELETE( - _request: NextRequest, - { params }: { params: { linkId: 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 unbookmarkLink(params.linkId, 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/links/route.ts b/packages/web/app/api/v1/links/route.ts deleted file mode 100644 index 87541634..00000000 --- a/packages/web/app/api/v1/links/route.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { authOptions } from "@/lib/auth"; -import { bookmarkLink, getLinks } from "@/lib/services/links"; - -import { - zNewBookmarkedLinkRequestSchema, - ZGetLinksResponse, - ZBookmarkedLink, -} from "@/lib/types/api/links"; -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 = zNewBookmarkedLinkRequestSchema.safeParse( - await request.json(), - ); - - if (!linkRequest.success) { - return NextResponse.json( - { - error: linkRequest.error.toString(), - }, - { status: 400 }, - ); - } - - const link = await bookmarkLink(linkRequest.data.url, session.user.id); - - let response: ZBookmarkedLink = { ...link }; - 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 links = await getLinks(session.user.id); - - let response: ZGetLinksResponse = { links }; - return NextResponse.json(response); -} -- cgit v1.2.3-70-g09d2