diff --git a/src/routes/api/visits/+server.ts b/src/routes/api/visits/+server.ts index 3f9c243..62083f5 100644 --- a/src/routes/api/visits/+server.ts +++ b/src/routes/api/visits/+server.ts @@ -2,7 +2,7 @@ import { lucia } from "$lib/server/auth"; import type { RequestEvent } from "@sveltejs/kit"; import { userVisitedAdventures } from "$lib/db/schema"; import { db } from "$lib/db/db.server"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import type { Adventure } from "$lib/utils/types"; // Gets all the adventures that the user has visited @@ -37,3 +37,39 @@ export async function GET(event: RequestEvent): Promise { } ); } + +// deletes the adventure given the adventure id and the user object +export async function DELETE(event: RequestEvent): Promise { + if (!event.locals.user) { + return new Response(JSON.stringify({ error: "No user found" }), { + status: 401, + headers: { + "Content-Type": "application/json", + }, + }); + } + + // get id from the body + const { id } = await event.request.json(); + + let res = await db + .delete(userVisitedAdventures) + .where( + and( + eq(userVisitedAdventures.userId, event.locals.user.id), + eq(userVisitedAdventures.adventureID, Number(id)) + ) + ) + .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", + }, + }); +} \ No newline at end of file diff --git a/src/routes/log/+page.server.ts b/src/routes/log/+page.server.ts index 41f111f..fa7fff8 100644 --- a/src/routes/log/+page.server.ts +++ b/src/routes/log/+page.server.ts @@ -1,5 +1,6 @@ import { redirect } from "@sveltejs/kit"; import type { PageServerLoad } from "./$types"; +import type { Adventure } from "$lib/utils/types"; export const load: PageServerLoad = async (event) => { if (!event.locals.user) { @@ -7,6 +8,7 @@ export const load: PageServerLoad = async (event) => { } const response = await event.fetch("/api/visits"); const result = await response.json(); + // let array = result.adventures as Adventure[]; return { result, }; diff --git a/src/routes/log/+page.svelte b/src/routes/log/+page.svelte index 2d8f6a9..afcaecb 100644 --- a/src/routes/log/+page.svelte +++ b/src/routes/log/+page.svelte @@ -1,6 +1,7 @@
@@ -183,15 +207,15 @@
- {#each data.result.adventures as adventure (adventure.id)} + {#each adventures as adventure (adventure.id)} {/each}
diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index c994ee9..5ae6374 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -7,13 +7,13 @@ 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); - } -} +// // 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;