Add delete entry function

This commit is contained in:
2026-02-04 14:29:21 +13:00
parent 59e117003f
commit d14f3dfdb6
3 changed files with 39 additions and 2 deletions

View File

@@ -17,6 +17,22 @@
} }
edit = false; edit = false;
} }
async function deleteEntry() {
const res = await fetch(`/api/entry/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: entry.id })
});
if (res.ok) {
entry = null;
} else {
alert('Failed to delete entry');
}
}
</script> </script>
<div class="flex flex-row mb-2"> <div class="flex flex-row mb-2">
@@ -24,7 +40,13 @@
{#if edit} {#if edit}
<button <button
class="bg-white/10 px-3 rounded-lg ml-auto" class="bg-red-500 px-3 rounded-lg ml-auto"
onclick={deleteEntry}
>
delete
</button>
<button
class="bg-white/10 px-3 rounded-lg ml-1"
onclick={() => edit = false} onclick={() => edit = false}
> >
cancel cancel

View File

@@ -0,0 +1,16 @@
import { db } from "$lib/server/db";
import { entryTable } from "$lib/server/db/schema";
import { httpResponse } from "$lib/server/http";
import { eq } from "drizzle-orm";
export async function POST({ request }) {
const { id, content, image } = await request.json();
try {
await db.delete(entryTable).where(eq(entryTable.id, id)).execute();
return httpResponse({ message: "Entry deleted successfully" }, 200);
} catch (error) {
return httpResponse({ message: "Failed to delete entry", details: error }, 500);
}
}

View File

@@ -2,7 +2,6 @@ import { db } from "$lib/server/db";
import { entryTable } from "$lib/server/db/schema"; import { entryTable } from "$lib/server/db/schema";
import { httpResponse } from "$lib/server/http"; import { httpResponse } from "$lib/server/http";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { uploadImage } from "$lib/upload.js";
export async function POST({ request }) { export async function POST({ request }) {
const { id, content, image } = await request.json(); const { id, content, image } = await request.json();