Skip to content

Commit

Permalink
feature(api): Add REST APIs to update bookmarks, tags and lists
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Oct 20, 2024
1 parent 20e5225 commit e89a386
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
18 changes: 18 additions & 0 deletions apps/web/app/api/v1/bookmarks/[bookmarkId]/route.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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 } },
Expand Down
18 changes: 18 additions & 0 deletions apps/web/app/api/v1/lists/[listId]/route.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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 } },
Expand Down
18 changes: 18 additions & 0 deletions apps/web/app/api/v1/tags/[tagId]/route.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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 } },
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/types/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export const zGetTagResponseSchema = z.object({
countAttachedBy: z.record(zAttachedByEnumSchema, z.number()),
});
export type ZGetTagResponse = z.infer<typeof zGetTagResponseSchema>;

export const zUpdateTagRequestSchema = z.object({
tagId: z.string(),
name: z.string().optional(),
});
12 changes: 5 additions & 7 deletions packages/trpc/routers/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type { ZAttachedByEnum } from "@hoarder/shared/types/tags";
import { SqliteError } from "@hoarder/db";
import { bookmarkTags, tagsOnBookmarks } from "@hoarder/db/schema";
import { triggerSearchReindex } from "@hoarder/shared/queues";
import { zGetTagResponseSchema } from "@hoarder/shared/types/tags";
import {
zGetTagResponseSchema,
zUpdateTagRequestSchema,
} from "@hoarder/shared/types/tags";

import type { Context } from "../index";
import { authedProcedure, router } from "../index";
Expand Down Expand Up @@ -150,12 +153,7 @@ export const tagsAppRouter = router({
return { deletedTags: res.changes };
}),
update: authedProcedure
.input(
z.object({
tagId: z.string(),
name: z.string().optional(),
}),
)
.input(zUpdateTagRequestSchema)
.output(
z.object({
id: z.string(),
Expand Down

0 comments on commit e89a386

Please sign in to comment.