-
- Discover the World's Most Thrilling Adventures
-
+ {#if data.user}
+ {#if data.user.first_name && data.user.first_name !== null}
+
+ {data.user.first_name.charAt(0).toUpperCase() +
+ data.user.first_name.slice(1)}, Discover the World's Most
+ Thrilling Adventures
+
+ {:else}
+
+ Discover the World's Most Thrilling Adventures
+
+ {/if}
+ {:else}
+
+ Discover the World's Most Thrilling Adventures
+
+ {/if}
Discover and plan your next epic adventure with our cutting-edge
travel app. Explore breathtaking destinations, create custom
diff --git a/src/routes/api/trip/+server.ts b/src/routes/api/trip/+server.ts
new file mode 100644
index 0000000..3073abd
--- /dev/null
+++ b/src/routes/api/trip/+server.ts
@@ -0,0 +1,51 @@
+import { db } from "$lib/db/db.server";
+import { adventureTable, userPlannedTrips } from "$lib/db/schema";
+import type { Adventure, Trip } from "$lib/utils/types";
+import { json, type RequestEvent, type RequestHandler } from "@sveltejs/kit";
+import { and, eq } from "drizzle-orm";
+
+/**
+ * Handles the GET request for retrieving a trip.
+ * @param {Request} request - The request object.
+ * @param {Response} response - The response object.
+ * @returns {Promise} - A promise that resolves when the request is handled.
+ */
+export const GET: RequestHandler = async ({ url, locals }) => {
+ const id = url.searchParams.get("id");
+ const user = locals.user;
+
+ if (!user) {
+ return json({ error: "Unauthorized" }, { status: 401 });
+ }
+
+ if (!id) {
+ return json({ error: "Missing adventure ID" }, { status: 400 });
+ }
+
+ const trip = await db
+ .select()
+ .from(userPlannedTrips)
+ .where(
+ and(
+ eq(userPlannedTrips.id, Number(id)), // Convert id to number
+ eq(userPlannedTrips.userId, user.id)
+ )
+ )
+ .limit(1)
+ .execute();
+
+ if (trip.length === 0) {
+ return json({ error: "Trip not found" }, { status: 404 });
+ }
+
+ JSON.stringify(
+ trip.map((r) => {
+ const adventure: Trip = r as Trip;
+ })
+ );
+
+ // console.log("GET /api/adventure?id=", id);
+ // console.log("User:", user);
+
+ return json({ trip }, { status: 200 });
+};
diff --git a/src/routes/api/trips/+server.ts b/src/routes/api/trips/+server.ts
index 4dc6fe3..50b0e40 100644
--- a/src/routes/api/trips/+server.ts
+++ b/src/routes/api/trips/+server.ts
@@ -1,7 +1,7 @@
import { db } from "$lib/db/db.server";
import { userPlannedTrips } from "$lib/db/schema";
import { error, type RequestEvent } from "@sveltejs/kit";
-import { eq } from "drizzle-orm";
+import { and, eq } from "drizzle-orm";
export async function POST(event: RequestEvent): Promise {
if (!event.locals.user) {
@@ -92,3 +92,38 @@ export async function GET(event: RequestEvent): Promise {
},
});
}
+
+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",
+ },
+ });
+ }
+
+ const body = await event.request.json();
+ if (!body.id) {
+ return error(400, {
+ message: "No trip id provided",
+ });
+ }
+
+ let res = await db
+ .delete(userPlannedTrips)
+ .where(
+ and(
+ eq(userPlannedTrips.userId, event.locals.user.id),
+ eq(userPlannedTrips.id, body.id)
+ )
+ )
+ .execute();
+
+ return new Response(JSON.stringify({ message: "Trip deleted" }), {
+ status: 200,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+}
diff --git a/src/routes/planner/+page.svelte b/src/routes/planner/+page.svelte
index 1a6dafa..53add80 100644
--- a/src/routes/planner/+page.svelte
+++ b/src/routes/planner/+page.svelte
@@ -13,6 +13,7 @@
import SucessToast from "$lib/components/SucessToast.svelte";
import mapDrawing from "$lib/assets/adventure_map.svg";
import CreateNewTripPlan from "$lib/components/CreateNewTripPlan.svelte";
+ import TripCard from "$lib/components/TripCard.svelte";
export let data;
let adventuresPlans: Adventure[] = [];
@@ -154,6 +155,34 @@
showToast("Failed to get trips");
});
}
+
+ async function removeTrip(event: { detail: number }) {
+ let initialLength: number = tripPlans.length;
+ const response = await fetch("/api/trips", {
+ method: "DELETE",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ id: event.detail }),
+ })
+ .then((response) => response.json())
+ .then((data) => {
+ console.log("Success:", data);
+ let theTrip = tripPlans.find((trip) => trip.id === event.detail);
+ if (theTrip) {
+ let newArray = tripPlans.filter((trip) => trip.id !== event.detail);
+ if (newArray.length === initialLength - 1) {
+ tripPlans = newArray;
+ showToast("Trip removed successfully!");
+ } else {
+ showToast("Failed to remove trip");
+ }
+ }
+ })
+ .catch((error) => {
+ showToast("Failed to get trips");
+ });
+ }
{#if isShowingToast}
@@ -247,19 +276,20 @@