Add delete entry function
This commit is contained in:
@@ -17,6 +17,22 @@
|
||||
}
|
||||
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>
|
||||
|
||||
<div class="flex flex-row mb-2">
|
||||
@@ -24,7 +40,13 @@
|
||||
|
||||
{#if edit}
|
||||
<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}
|
||||
>
|
||||
cancel
|
||||
|
||||
16
src/routes/api/entry/delete/+server.ts
Normal file
16
src/routes/api/entry/delete/+server.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ import { db } from "$lib/server/db";
|
||||
import { entryTable } from "$lib/server/db/schema";
|
||||
import { httpResponse } from "$lib/server/http";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { uploadImage } from "$lib/upload.js";
|
||||
|
||||
export async function POST({ request }) {
|
||||
const { id, content, image } = await request.json();
|
||||
|
||||
Reference in New Issue
Block a user