Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deleteItem() and deleteComment() #306

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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