diff --git a/src/routes/api/visits/+server.ts b/src/routes/api/visits/+server.ts index e123ec2..6d3bd84 100644 --- a/src/routes/api/visits/+server.ts +++ b/src/routes/api/visits/+server.ts @@ -121,4 +121,48 @@ let res = await db }, } ); +} + +// put route to update existing adventure +export async function PUT(event: RequestEvent): Promise { + if (!event.locals.user) { + return new Response(JSON.stringify({ error: "No user found" }), { + status: 401, + headers: { + "Content-Type": "application/json", + }, + }); + } + + // get properties from the body + const { id, name, location, created } = await event.request.json(); + + // update the adventure in the user's visited list + await db + .update(userVisitedAdventures) + .set({ + adventureName: name, + location: location, + visitedDate: created, + }) + .where( + and( + eq(userVisitedAdventures.userId, event.locals.user.id), + eq(userVisitedAdventures.adventureID, Number(id)) + ) + ) + .execute(); + + return new Response( + JSON.stringify({ + adventure: { id, name, location, created }, + message: { message: "Adventure updated" }, + }), + { + status: 200, + headers: { + "Content-Type": "application/json", + }, + } + ); } \ No newline at end of file diff --git a/src/routes/log/+page.svelte b/src/routes/log/+page.svelte index 2583b12..be07c59 100644 --- a/src/routes/log/+page.svelte +++ b/src/routes/log/+page.svelte @@ -9,7 +9,6 @@ clearAdventures, getAdventures, getNextId, - saveEdit, } from "../../services/adventureService"; import { onMount } from "svelte"; import { exportData } from "../../services/export"; @@ -67,10 +66,8 @@ }) .then((response) => response.json()) .then((data) => { - console.log("Success:", data); let newId = data.id; - console.log("New ID: " + newId); - console.log("New Name: " + newName); + // add to local array for instant view update adventures = [ ...adventures, { @@ -89,25 +86,37 @@ }); }; - // function triggerRemoveAdventure(event: { detail: number }) { - // removeAdventure(event); - // showToast("removed"); - // adventures = getAdventures(); - // // remove from data.result.adventures - // data.result.adventures = data.result.adventures.filter( - // (adventure: Adventure) => adventure.id !== event.detail, - // ); - // } - function saveAdventure(event: { detail: Adventure }) { console.log("Event" + event.detail); - saveEdit(event.detail); - editId = NaN; - editName = ""; - editLocation = ""; - editCreated = ""; - adventures = getAdventures(); - showToast("edited"); + // put request to /api/visits with id and advneture data + fetch("/api/visits", { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + id: event.detail.id, + name: event.detail.name, + location: event.detail.location, + created: event.detail.created, + }), + }) + .then((response) => response.json()) + .then((data) => { + console.log("Success:", data); + // update local array with new data + adventures = adventures.map((adventure) => + adventure.id === event.detail.id ? event.detail : adventure, + ); + editId = NaN; + editName = ""; + editLocation = ""; + editCreated = ""; + showToast("edited"); + }) + .catch((error) => { + console.error("Error:", error); + }); } function editAdventure(event: { detail: number }) { diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index 7088fc4..9e984f4 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -31,28 +31,6 @@ export function getAdventures(): Adventure[] { return adventures; } -export function saveEdit(adventure: Adventure) { - let editId = adventure.id; - console.log("saving edit"); - let editName = adventure.name; - let editLocation = adventure.location; - let editCreated = adventure.created; - let oldAdventure: Adventure | undefined = adventures.find( - (adventure) => adventure.id === editId - ); - console.log("old" + oldAdventure); - if (oldAdventure) { - oldAdventure.name = editName; - oldAdventure.location = editLocation; - oldAdventure.created = editCreated; - } - editId = NaN; - console.log("done"); - if (isBrowser) { - localStorage.setItem("adventures", JSON.stringify(adventures)); - } -} - export function clearAdventures() { adventures = []; if (isBrowser) {