From ed9a579fc27adc51fada0862c349a69c4e86611e Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Wed, 10 Apr 2024 14:51:51 +0000 Subject: [PATCH] Add POST endpoint to create new adventure and update adventureService --- src/routes/api/visits/+server.ts | 57 +++++++++++++++++++++++++++++--- src/routes/log/+page.svelte | 45 +++++++++++++++++++------ src/services/adventureService.ts | 16 +-------- 3 files changed, 88 insertions(+), 30 deletions(-) diff --git a/src/routes/api/visits/+server.ts b/src/routes/api/visits/+server.ts index 62083f5..e123ec2 100644 --- a/src/routes/api/visits/+server.ts +++ b/src/routes/api/visits/+server.ts @@ -62,14 +62,63 @@ export async function DELETE(event: RequestEvent): Promise { ) .execute(); - console.log(res); - console.log(id); - console.log(event.locals.user.id); - return new Response(JSON.stringify({ id: id, res: res }), { status: 200, headers: { "Content-Type": "application/json", }, }); +} + +// add the adventure to the user's visited list +export async function POST(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 { name, location, created } = await event.request.json(); + + // insert the adventure to the user's visited list + await db + .insert(userVisitedAdventures) + .values({ + userId: event.locals.user.id, + adventureName: name, + location: location, + visitedDate: created, + }) + .execute(); +let res = await db + .select() + .from(userVisitedAdventures) + .where( + and( + eq(userVisitedAdventures.userId, event.locals.user.id), + eq(userVisitedAdventures.adventureName, name), + eq(userVisitedAdventures.location, location), + eq(userVisitedAdventures.visitedDate, created) + ) + ) + .execute(); + +// return a response with the adventure object values + return new Response( + JSON.stringify({ + adventure: { name, location, created }, + message: { message: "Adventure added" }, + id: res[0].adventureID + }), + { + 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 afcaecb..2583b12 100644 --- a/src/routes/log/+page.svelte +++ b/src/routes/log/+page.svelte @@ -53,17 +53,40 @@ const createNewAdventure = () => { let currentDate = new Date(); let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format - const newAdventure: Adventure = { - id: getNextId(), - name: newName, - location: newLocation, - created: dateString, - }; - addAdventure(newAdventure); - newName = ""; // Reset newName and newLocation after adding adventure - newLocation = ""; - adventures = getAdventures(); // add to local array - showToast("added"); + // post to /api/visits + fetch("/api/visits", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + name: newName, + location: newLocation, + created: dateString, + }), + }) + .then((response) => response.json()) + .then((data) => { + console.log("Success:", data); + let newId = data.id; + console.log("New ID: " + newId); + console.log("New Name: " + newName); + adventures = [ + ...adventures, + { + id: newId, + name: newName, + location: newLocation, + created: dateString, + }, + ]; + newName = ""; // Reset newName and newLocation after adding adventure + newLocation = ""; + showToast("added"); + }) + .catch((error) => { + console.error("Error:", error); + }); }; // function triggerRemoveAdventure(event: { detail: number }) { diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index 5ae6374..7088fc4 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -7,13 +7,7 @@ import { visitCount } from "$lib/utils/stores/visitCountStore"; // Check if localStorage is available (browser environment) const isBrowser = typeof window !== "undefined"; -// // Load adventures from localStorage on startup (only in the browser) -// if (isBrowser) { -// const storedAdventures = localStorage.getItem("adventures"); -// if (storedAdventures) { -// adventures = JSON.parse(storedAdventures); -// } -// } +// export function getNextId() { let nextId = Math.max(0, ...adventures.map((adventure) => adventure.id)) + 1; @@ -37,14 +31,6 @@ export function getAdventures(): Adventure[] { return adventures; } -export function removeAdventure(event: { detail: number }) { - adventures = adventures.filter((adventure) => adventure.id !== event.detail); - if (isBrowser) { - localStorage.setItem("adventures", JSON.stringify(adventures)); - visitCount.update((n) => n - 1); - } -} - export function saveEdit(adventure: Adventure) { let editId = adventure.id; console.log("saving edit");