diff --git a/src/lib/components/AdventureCard.svelte b/src/lib/components/AdventureCard.svelte index d52eb83..1b2180f 100644 --- a/src/lib/components/AdventureCard.svelte +++ b/src/lib/components/AdventureCard.svelte @@ -53,7 +53,7 @@

{location}

{/if} - {#if date !== ""} + {#if date && date !== ""}

{date}

@@ -73,7 +73,7 @@ >

{name}

- {#if location != ""} + {#if location && location != ""}

{location}

@@ -92,7 +92,7 @@ >

{name}

- {#if location !== ""} + {#if location && location !== ""}

{location}

diff --git a/src/routes/api/visits/+server.ts b/src/routes/api/visits/+server.ts index 777106d..bf575de 100644 --- a/src/routes/api/visits/+server.ts +++ b/src/routes/api/visits/+server.ts @@ -1,6 +1,6 @@ import { lucia } from "$lib/server/auth"; import type { RequestEvent } from "@sveltejs/kit"; -import { userVisitedAdventures } from "$lib/db/schema"; +import { adventureTable, userVisitedAdventures } from "$lib/db/schema"; import { db } from "$lib/db/db.server"; import { and, eq } from "drizzle-orm"; import type { Adventure } from "$lib/utils/types"; @@ -17,18 +17,12 @@ export async function GET(event: RequestEvent): Promise { } let result = await db .select() - .from(userVisitedAdventures) - .where(eq(userVisitedAdventures.userId, event.locals.user.id)) + .from(adventureTable) + .where(eq(adventureTable.userId, event.locals.user.id)) .execute(); return new Response( - JSON.stringify({ - adventures: result.map((item) => ({ - id: item.adventureID, - name: item.adventureName, - location: item.location, - date: item.visitedDate, - })), - }), + // turn the result into an Adventure object array + JSON.stringify(result.map((r) => r as Adventure)), { status: 200, headers: { @@ -62,11 +56,11 @@ export async function DELETE(event: RequestEvent): Promise { } let res = await db - .delete(userVisitedAdventures) + .delete(adventureTable) .where( and( - eq(userVisitedAdventures.userId, event.locals.user.id), - eq(userVisitedAdventures.adventureID, Number(id)) + eq(adventureTable.userId, event.locals.user.id), + eq(adventureTable.id, Number(id)) ) ) .execute(); @@ -90,28 +84,30 @@ export async function POST(event: RequestEvent): Promise { }); } - // get properties from the body - const { name, location, date } = await event.request.json(); + const { newAdventure } = await event.request.json(); + console.log(newAdventure); + const { name, location, date } = newAdventure; // insert the adventure to the user's visited list await db - .insert(userVisitedAdventures) + .insert(adventureTable) .values({ userId: event.locals.user.id, - adventureName: name, + type: "mylog", + name: name, location: location, - visitedDate: date, + date: date, }) .execute(); let res = await db .select() - .from(userVisitedAdventures) + .from(adventureTable) .where( and( - eq(userVisitedAdventures.userId, event.locals.user.id), - eq(userVisitedAdventures.adventureName, name), - eq(userVisitedAdventures.location, location), - eq(userVisitedAdventures.visitedDate, date) + eq(adventureTable.userId, event.locals.user.id), + eq(adventureTable.name, name), + eq(adventureTable.location, location), + eq(adventureTable.date, date) ) ) .execute(); @@ -121,7 +117,7 @@ export async function POST(event: RequestEvent): Promise { JSON.stringify({ adventure: { name, location, date }, message: { message: "Adventure added" }, - id: res[0].adventureID, + id: res[0].id, }), { status: 200, @@ -144,20 +140,22 @@ export async function PUT(event: RequestEvent): Promise { } // get properties from the body - const { id, name, location, date } = await event.request.json(); + const { newAdventure } = await event.request.json(); + console.log(newAdventure); + const { name, location, date, id } = newAdventure; // update the adventure in the user's visited list await db - .update(userVisitedAdventures) + .update(adventureTable) .set({ - adventureName: name, + name: name, location: location, - visitedDate: date, + date: date, }) .where( and( - eq(userVisitedAdventures.userId, event.locals.user.id), - eq(userVisitedAdventures.adventureID, Number(id)) + eq(adventureTable.userId, event.locals.user.id), + eq(adventureTable.id, Number(id)) ) ) .execute(); diff --git a/src/routes/log/+page.svelte b/src/routes/log/+page.svelte index 30f786f..5849516 100644 --- a/src/routes/log/+page.svelte +++ b/src/routes/log/+page.svelte @@ -28,7 +28,7 @@ // Sets the adventures array to the data from the server onMount(async () => { console.log(data); - adventures = data.result.adventures; + adventures = data.result; isLoading = false; }); @@ -63,15 +63,22 @@ let currentDate = new Date(); let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format // post to /api/visits + + let newAdventure: Adventure = { + type: "mylog", + name: newName, + location: newLocation, + date: dateString, + id: -1, + }; + fetch("/api/visits", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ - name: newName, - location: newLocation, - date: dateString, + newAdventure, }), }) .then((response) => response.json()) @@ -82,6 +89,7 @@ ...adventures, { id: newId, + type: "mylog", name: newName, location: newLocation, date: dateString, @@ -99,6 +107,15 @@ function saveAdventure(event: { detail: Adventure }) { console.log("Event" + event.detail); + + let newAdventure: Adventure = { + type: "mylog", + name: event.detail.name, + location: event.detail.location, + date: event.detail.date, + id: event.detail.id, + }; + // put request to /api/visits with id and advneture data fetch("/api/visits", { method: "PUT", @@ -106,10 +123,7 @@ "Content-Type": "application/json", }, body: JSON.stringify({ - id: event.detail.id, - name: event.detail.name, - location: event.detail.location, - date: event.detail.date, + newAdventure, }), }) .then((response) => response.json()) diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index 9e984f4..53ba034 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -1,40 +1 @@ import type { Adventure } from "$lib/utils/types"; - -let adventures: Adventure[] = []; - -import { visitCount } from "$lib/utils/stores/visitCountStore"; - -// Check if localStorage is available (browser environment) -const isBrowser = typeof window !== "undefined"; - -// - -export function getNextId() { - let nextId = Math.max(0, ...adventures.map((adventure) => adventure.id)) + 1; - return nextId; -} - -export function setAdventures(importArray: Adventure[]) { - adventures = importArray; -} - -export function addAdventure(adventure: Adventure) { - adventures = [...adventures, adventure]; - if (isBrowser) { - localStorage.setItem("adventures", JSON.stringify(adventures)); - visitCount.update((n) => n + 1); - } - console.log(adventures); -} - -export function getAdventures(): Adventure[] { - return adventures; -} - -export function clearAdventures() { - adventures = []; - if (isBrowser) { - localStorage.setItem("adventures", JSON.stringify(adventures)); - visitCount.set(0); - } -}