From 18b4dfa922c9daf0cac41c83f64b2d0e996dafce Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Tue, 4 Jun 2024 21:09:10 +0000 Subject: [PATCH] refactor: Improve search functionality by using case-insensitive search for adventure location and name --- src/routes/api/search/+server.ts | 33 +++++++++--- src/routes/search/+page.server.ts | 7 +++ src/routes/search/+page.svelte | 85 +++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 8 deletions(-) diff --git a/src/routes/api/search/+server.ts b/src/routes/api/search/+server.ts index 4c6f84b..6a00ce5 100644 --- a/src/routes/api/search/+server.ts +++ b/src/routes/api/search/+server.ts @@ -17,14 +17,15 @@ export const GET: RequestHandler = async ({ } else if (visited === "false") { isVisited = false; } + console.log("visited", visited, isVisited); if (!user) { return json({ error: "Unauthorized" }, { status: 401 }); } if (!type) { const activityResults = await activitySearch(value, locals, isVisited); - const locationResults = await locationSearch(value, locals); - const namesResults = await nameSearch(value, locals); + const locationResults = await locationSearch(value, locals, isVisited); + const namesResults = await nameSearch(value, locals, isVisited); // remove duplicates by id let adventures: any = {}; @@ -44,9 +45,9 @@ export const GET: RequestHandler = async ({ } else if (type === "activity") { return json(await activitySearch(value, locals, isVisited)); } else if (type === "location") { - return json(await locationSearch(value, locals)); + return json(await locationSearch(value, locals, isVisited)); } else if (type === "name") { - return json(await nameSearch(value, locals)); + return json(await nameSearch(value, locals, isVisited)); } return json({ error: "No results found." }, { status: 400 }); }; @@ -78,14 +79,22 @@ async function activitySearch( }; } -async function locationSearch(value: string, locals: any) { +async function locationSearch( + value: string, + locals: any, + visited: boolean | undefined +) { let res = await db .select() .from(adventureTable) .where( and( ilike(adventureTable.location, `%${value}%`), - eq(adventureTable.userId, locals.user.id) + eq(adventureTable.userId, locals.user.id), + or( + visited === true ? eq(adventureTable.type, "mylog") : undefined, + visited === false ? eq(adventureTable.type, "planner") : undefined + ) ) ) .execute(); @@ -95,14 +104,22 @@ async function locationSearch(value: string, locals: any) { }; } -async function nameSearch(value: string, locals: any) { +async function nameSearch( + value: string, + locals: any, + visited: boolean | undefined +) { let res = await db .select() .from(adventureTable) .where( and( ilike(adventureTable.name, `%${value}%`), - eq(adventureTable.userId, locals.user.id) + eq(adventureTable.userId, locals.user.id), + or( + visited === true ? eq(adventureTable.type, "mylog") : undefined, + visited === false ? eq(adventureTable.type, "planner") : undefined + ) ) ) .execute(); diff --git a/src/routes/search/+page.server.ts b/src/routes/search/+page.server.ts index 7dc3dc4..4033fbe 100644 --- a/src/routes/search/+page.server.ts +++ b/src/routes/search/+page.server.ts @@ -19,3 +19,10 @@ export const load: PageServerLoad = async ({ url, locals, fetch }) => { let json = await data.json(); return { props: { adventures: json.adventures } }; }; + +export const actions = { + default: async () => { + console.log("default"); + return { props: {} }; + }, +}; diff --git a/src/routes/search/+page.svelte b/src/routes/search/+page.svelte index a290b48..f2c59a3 100644 --- a/src/routes/search/+page.svelte +++ b/src/routes/search/+page.svelte @@ -1,13 +1,98 @@
+
+ + All + + Not Visited + + Visited +
+ + All + + Activity + + Location + + Name + + +

Search Results

{#if adventureArray.length > 0}