updating entries works fully now !!

This commit is contained in:
2026-02-03 20:48:27 +13:00
parent faed363820
commit 2e85fef7e2
3 changed files with 45 additions and 4 deletions

View File

@@ -2,12 +2,21 @@
import ImageTarget from "./imageTarget.svelte";
import TextEditor from "./textEditor.svelte";
import { formatDate } from "$lib/date.ts";
import { createEntry } from "$lib/upload.ts";
import { createEntry, updateEntry } from "$lib/upload.ts";
let { newEntryDate, entry = $bindable(), edit = $bindable(false) } = $props();
let newEntry = $state({ date: newEntryDate, image: "", content: "" });
async function saveEntry() {
if (entry) {
await updateEntry(entry);
} else {
await createEntry(newEntry);
}
edit = false;
}
</script>
<div class="flex flex-row mb-2">
@@ -22,7 +31,7 @@
</button>
<button
class="bg-white/20 px-3 rounded-lg ml-1"
onclick={() => { createEntry(newEntry); edit = false }}
onclick={saveEntry}
>
done
</button>

View File

@@ -39,4 +39,19 @@ export async function createEntry(newEntry) {
},
body: JSON.stringify(newEntry),
});
}
}
export async function updateEntry(entry) {
if (entry.image) {
const url = await uploadImage(entry.image)
entry.image = url
}
await fetch("/api/entry/update", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(entry),
});
}

View File

@@ -0,0 +1,17 @@
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();
try {
await db.update(entryTable).set({ content, image }).where(eq(entryTable.id, id)).execute();
return httpResponse({ message: "Entry updated successfully" }, 200);
} catch (error) {
return httpResponse({ message: "Failed to update entry", details: error }, 500);
}
}