updating entries works fully now !!
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -40,3 +40,18 @@ 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),
|
||||
});
|
||||
}
|
||||
|
||||
17
src/routes/api/entry/update/+server.ts
Normal file
17
src/routes/api/entry/update/+server.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user