aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorkamtschatka <simon.schatka@gmx.at>2024-07-27 22:32:37 +0200
committerGitHub <noreply@github.com>2024-07-27 13:32:37 -0700
commit5e4decbe967ec28f9263bcb1d9907ee86262b91e (patch)
tree0a8f9764af4ef3bc2db7c5e993fef354782a2285 /packages
parenteb0a28ee6b1dd15fb459e0900f5e885ac5c92fec (diff)
downloadkarakeep-5e4decbe967ec28f9263bcb1d9907ee86262b91e.tar.zst
feature(cli): Allow updating tags/lists from CLI (#211)
* Improve the CLI #209 added the possibility to assign tags to bookmarks while creating added the possibility to assign a newly created to a list right away added the possibility to add and remove tags from bookmarks * minor tweaks --------- Co-authored-by: MohamedBassem <me@mbassem.com>
Diffstat (limited to '')
-rw-r--r--packages/trpc/routers/bookmarks.test.ts24
-rw-r--r--packages/trpc/routers/bookmarks.ts55
2 files changed, 62 insertions, 17 deletions
diff --git a/packages/trpc/routers/bookmarks.test.ts b/packages/trpc/routers/bookmarks.test.ts
index 67e73ad1..802bd992 100644
--- a/packages/trpc/routers/bookmarks.test.ts
+++ b/packages/trpc/routers/bookmarks.test.ts
@@ -126,23 +126,37 @@ describe("Bookmark Routes", () => {
await api.updateTags({
bookmarkId: createdBookmark.id,
- attach: [{ tagName: "tag1" }, { tagName: "tag2" }],
+ attach: [
+ { tagName: "tag1" },
+ { tagName: "tag2" },
+ { tagName: "tag3" },
+ { tagName: "tag4" },
+ ],
detach: [],
});
let bookmark = await api.getBookmark({ bookmarkId: createdBookmark.id });
- expect(bookmark.tags.map((t) => t.name).sort()).toEqual(["tag1", "tag2"]);
+ expect(bookmark.tags.map((t) => t.name).sort()).toEqual([
+ "tag1",
+ "tag2",
+ "tag3",
+ "tag4",
+ ]);
const tag1Id = bookmark.tags.filter((t) => t.name == "tag1")[0].id;
await api.updateTags({
bookmarkId: bookmark.id,
- attach: [{ tagName: "tag3" }],
- detach: [{ tagId: tag1Id }],
+ attach: [{ tagName: "tag5" }],
+ detach: [{ tagId: tag1Id }, { tagName: "tag4" }],
});
bookmark = await api.getBookmark({ bookmarkId: bookmark.id });
- expect(bookmark.tags.map((t) => t.name).sort()).toEqual(["tag2", "tag3"]);
+ expect(bookmark.tags.map((t) => t.name).sort()).toEqual([
+ "tag2",
+ "tag3",
+ "tag5",
+ ]);
await api.updateTags({
bookmarkId: bookmark.id,
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts
index d2aa36bb..e685d5c2 100644
--- a/packages/trpc/routers/bookmarks.ts
+++ b/packages/trpc/routers/bookmarks.ts
@@ -665,8 +665,13 @@ export const bookmarksAppRouter = router({
tagName: z.string().optional(),
}),
),
- // Detach by tag ids
- detach: z.array(z.object({ tagId: z.string() })),
+ detach: z.array(
+ z.object({
+ // At least one of the two must be set
+ tagId: z.string().optional(),
+ tagName: z.string().optional(), // Also allow removing by tagName, to make CLI usage easier
+ }),
+ ),
}),
)
.output(
@@ -679,23 +684,49 @@ export const bookmarksAppRouter = router({
.mutation(async ({ input, ctx }) => {
return ctx.db.transaction(async (tx) => {
// Detaches
+ const idsToRemove: string[] = [];
if (input.detach.length > 0) {
- await tx.delete(tagsOnBookmarks).where(
- and(
- eq(tagsOnBookmarks.bookmarkId, input.bookmarkId),
- inArray(
- tagsOnBookmarks.tagId,
- input.detach.map((t) => t.tagId),
+ const namesToRemove: string[] = [];
+ input.detach.forEach((detachInfo) => {
+ if (detachInfo.tagId) {
+ idsToRemove.push(detachInfo.tagId);
+ }
+ if (detachInfo.tagName) {
+ namesToRemove.push(detachInfo.tagName);
+ }
+ });
+
+ if (namesToRemove.length > 0) {
+ (
+ await tx.query.bookmarkTags.findMany({
+ where: and(
+ eq(bookmarkTags.userId, ctx.user.id),
+ inArray(bookmarkTags.name, namesToRemove),
+ ),
+ columns: {
+ id: true,
+ },
+ })
+ ).forEach((tag) => {
+ idsToRemove.push(tag.id);
+ });
+ }
+
+ await tx
+ .delete(tagsOnBookmarks)
+ .where(
+ and(
+ eq(tagsOnBookmarks.bookmarkId, input.bookmarkId),
+ inArray(tagsOnBookmarks.tagId, idsToRemove),
),
- ),
- );
+ );
}
if (input.attach.length == 0) {
return {
bookmarkId: input.bookmarkId,
attached: [],
- detached: input.detach.map((t) => t.tagId),
+ detached: idsToRemove,
};
}
@@ -751,7 +782,7 @@ export const bookmarksAppRouter = router({
return {
bookmarkId: input.bookmarkId,
attached: allIds,
- detached: input.detach.map((t) => t.tagId),
+ detached: idsToRemove,
};
});
}),