From e89a38680532c3ab72ef26dfe88bb64476b709ab Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 20 Oct 2024 15:54:34 +0000 Subject: feature(api): Add REST APIs to update bookmarks, tags and lists --- apps/web/app/api/v1/bookmarks/[bookmarkId]/route.ts | 18 ++++++++++++++++++ apps/web/app/api/v1/lists/[listId]/route.ts | 18 ++++++++++++++++++ apps/web/app/api/v1/tags/[tagId]/route.ts | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) (limited to 'apps/web/app/api') diff --git a/apps/web/app/api/v1/bookmarks/[bookmarkId]/route.ts b/apps/web/app/api/v1/bookmarks/[bookmarkId]/route.ts index 0315ff8c..8fe4d9fe 100644 --- a/apps/web/app/api/v1/bookmarks/[bookmarkId]/route.ts +++ b/apps/web/app/api/v1/bookmarks/[bookmarkId]/route.ts @@ -1,6 +1,8 @@ import { NextRequest } from "next/server"; import { buildHandler } from "@/app/api/v1/utils/handler"; +import { zUpdateBookmarksRequestSchema } from "@hoarder/shared/types/bookmarks"; + export const dynamic = "force-dynamic"; export const GET = ( @@ -17,6 +19,22 @@ export const GET = ( }, }); +export const PATCH = ( + req: NextRequest, + { params }: { params: { bookmarkId: string } }, +) => + buildHandler({ + req, + bodySchema: zUpdateBookmarksRequestSchema.omit({ bookmarkId: true }), + handler: async ({ api, body }) => { + const bookmark = await api.bookmarks.updateBookmark({ + bookmarkId: params.bookmarkId, + ...body!, + }); + return { status: 200, resp: bookmark }; + }, + }); + export const DELETE = ( req: NextRequest, { params }: { params: { bookmarkId: string } }, diff --git a/apps/web/app/api/v1/lists/[listId]/route.ts b/apps/web/app/api/v1/lists/[listId]/route.ts index f5af286d..69c99fda 100644 --- a/apps/web/app/api/v1/lists/[listId]/route.ts +++ b/apps/web/app/api/v1/lists/[listId]/route.ts @@ -1,6 +1,8 @@ import { NextRequest } from "next/server"; import { buildHandler } from "@/app/api/v1/utils/handler"; +import { zNewBookmarkListSchema } from "@hoarder/shared/types/lists"; + export const dynamic = "force-dynamic"; export const GET = ( @@ -20,6 +22,22 @@ export const GET = ( }, }); +export const PATCH = ( + req: NextRequest, + { params }: { params: { listId: string } }, +) => + buildHandler({ + req, + bodySchema: zNewBookmarkListSchema.partial(), + handler: async ({ api, body }) => { + const list = await api.lists.edit({ + listId: params.listId, + ...body!, + }); + return { status: 200, resp: list }; + }, + }); + export const DELETE = ( req: NextRequest, { params }: { params: { listId: string } }, diff --git a/apps/web/app/api/v1/tags/[tagId]/route.ts b/apps/web/app/api/v1/tags/[tagId]/route.ts index 439b6149..29b27218 100644 --- a/apps/web/app/api/v1/tags/[tagId]/route.ts +++ b/apps/web/app/api/v1/tags/[tagId]/route.ts @@ -1,6 +1,8 @@ import { NextRequest } from "next/server"; import { buildHandler } from "@/app/api/v1/utils/handler"; +import { zUpdateTagRequestSchema } from "@hoarder/shared/types/tags"; + export const dynamic = "force-dynamic"; export const GET = ( @@ -20,6 +22,22 @@ export const GET = ( }, }); +export const PATCH = ( + req: NextRequest, + { params }: { params: { tagId: string } }, +) => + buildHandler({ + req, + bodySchema: zUpdateTagRequestSchema.omit({ tagId: true }), + handler: async ({ api, body }) => { + const tag = await api.tags.update({ + tagId: params.tagId, + ...body!, + }); + return { status: 200, resp: tag }; + }, + }); + export const DELETE = ( req: NextRequest, { params }: { params: { tagId: string } }, -- cgit v1.2.3-70-g09d2