From 2e85fef7e23795c5f9dbe7d902150d375885ca22 Mon Sep 17 00:00:00 2001 From: June Date: Tue, 3 Feb 2026 20:48:27 +1300 Subject: [PATCH] updating entries works fully now !! --- src/lib/components/editor/index.svelte | 15 ++++++++++++--- src/lib/upload.ts | 17 ++++++++++++++++- src/routes/api/entry/update/+server.ts | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/routes/api/entry/update/+server.ts diff --git a/src/lib/components/editor/index.svelte b/src/lib/components/editor/index.svelte index 5c617c3..e0ba0bf 100644 --- a/src/lib/components/editor/index.svelte +++ b/src/lib/components/editor/index.svelte @@ -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; + }
@@ -22,7 +31,7 @@ diff --git a/src/lib/upload.ts b/src/lib/upload.ts index 5904574..909a120 100644 --- a/src/lib/upload.ts +++ b/src/lib/upload.ts @@ -39,4 +39,19 @@ export async function createEntry(newEntry) { }, body: JSON.stringify(newEntry), }); -} \ No newline at end of file +} + +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), + }); +} diff --git a/src/routes/api/entry/update/+server.ts b/src/routes/api/entry/update/+server.ts new file mode 100644 index 0000000..72aff9e --- /dev/null +++ b/src/routes/api/entry/update/+server.ts @@ -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); + } +} \ No newline at end of file