From f8f12e4ba3709c154ec3de1cf2bc01d17137fe9e Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Wed, 15 May 2024 21:17:04 +0000 Subject: [PATCH] chore: Refactor trip API endpoint to include adventures --- src/routes/api/trip/+server.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/routes/api/trip/+server.ts b/src/routes/api/trip/+server.ts index 3073abd..fedb60d 100644 --- a/src/routes/api/trip/+server.ts +++ b/src/routes/api/trip/+server.ts @@ -22,7 +22,7 @@ export const GET: RequestHandler = async ({ url, locals }) => { return json({ error: "Missing adventure ID" }, { status: 400 }); } - const trip = await db + let trip = await db .select() .from(userPlannedTrips) .where( @@ -38,14 +38,37 @@ export const GET: RequestHandler = async ({ url, locals }) => { return json({ error: "Trip not found" }, { status: 404 }); } + let adventures = await db + .select() + .from(adventureTable) + .where(eq(adventureTable.tripId, trip[0].id)) + .execute(); + JSON.stringify( trip.map((r) => { const adventure: Trip = r as Trip; }) ); + if (adventures.length !== 0) { + JSON.stringify( + adventures.map((r) => { + const adventure: Adventure = r as Adventure; + if (typeof adventure.activityTypes === "string") { + try { + adventure.activityTypes = JSON.parse(adventure.activityTypes); + } catch (error) { + console.error("Error parsing activityTypes:", error); + adventure.activityTypes = undefined; + } + } + return adventure; + }) + ); + } + // console.log("GET /api/adventure?id=", id); // console.log("User:", user); - return json({ trip }, { status: 200 }); + return json({ trip, adventures }, { status: 200 }); };