Skip to content

Commit

Permalink
feat: deleteItem() and deleteComment() (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Jun 28, 2023
1 parent f468fe7 commit d601e32
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
24 changes: 24 additions & 0 deletions utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ export async function createItem(item: Item) {
if (!res.ok) throw new Error(`Failed to create item: ${item}`);
}

export async function deleteItem(item: Item) {
const itemsKey = ["items", item.id];
const itemsByTimeKey = ["items_by_time", item.createdAt.getTime(), item.id];
const itemsByUserKey = ["items_by_user", item.userId, item.id];

const res = await kv.atomic()
.delete(itemsKey)
.delete(itemsByTimeKey)
.delete(itemsByUserKey)
.commit();

if (!res.ok) throw new Error(`Failed to delete item: ${item}`);
}

export async function getItem(id: string) {
return await getValue<Item>(["items", id]);
}
Expand Down Expand Up @@ -149,6 +163,16 @@ export async function createComment(comment: Comment) {
if (!res.ok) throw new Error(`Failed to create comment: ${comment}`);
}

export async function deleteComment(comment: Comment) {
const commentsByItemKey = ["comments_by_item", comment.itemId, comment.id];

const res = await kv.atomic()
.delete(commentsByItemKey)
.commit();

if (!res.ok) throw new Error(`Failed to delete comment: ${comment}`);
}

export async function getCommentsByItem(itemId: string) {
return await getValues<Comment>({ prefix: ["comments_by_item", itemId] });
}
Expand Down
13 changes: 11 additions & 2 deletions utils/db_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
createItem,
createUser,
createVote,
deleteComment,
deleteItem,
deleteUserBySession,
deleteVote,
getAllItems,
Expand Down Expand Up @@ -85,14 +87,17 @@ Deno.test("[db] getAllItems()", async () => {
assertArrayIncludes(await getAllItems(), [item1, item2]);
});

Deno.test("[db] (get/create)Item()", async () => {
Deno.test("[db] (get/create/delete)Item()", async () => {
const item = genNewItem();

assertEquals(await getItem(item.id), null);

await createItem(item);
await assertRejects(async () => await createItem(item));
assertEquals(await getItem(item.id), item);

await deleteItem(item);
assertEquals(await getItem(item.id), null);
});

Deno.test("[db] getItemsByUser()", async () => {
Expand Down Expand Up @@ -172,7 +177,7 @@ Deno.test("[db] newCommentProps()", () => {
assertEquals(typeof commentProps.id, "string");
});

Deno.test("[db] createComment() + getCommentsByItem()", async () => {
Deno.test("[db] (create/delete)Comment() + getCommentsByItem()", async () => {
const itemId = crypto.randomUUID();
const comment1 = genNewComment({
itemId,
Expand All @@ -187,6 +192,10 @@ Deno.test("[db] createComment() + getCommentsByItem()", async () => {
await createComment(comment2);
await assertRejects(async () => await createComment(comment2));
assertArrayIncludes(await getCommentsByItem(itemId), [comment1, comment2]);

await deleteComment(comment1);
await deleteComment(comment2);
assertEquals(await getCommentsByItem(itemId), []);
});

Deno.test("[db] votes", async () => {
Expand Down

0 comments on commit d601e32

Please sign in to comment.