1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 05:05:17 +02:00

refactor: Improve search functionality by using case-insensitive search for adventure location and name

This commit is contained in:
Sean Morley 2024-06-04 21:09:10 +00:00
parent ca04bc0095
commit 18b4dfa922
3 changed files with 117 additions and 8 deletions

View file

@ -17,14 +17,15 @@ export const GET: RequestHandler = async ({
} else if (visited === "false") { } else if (visited === "false") {
isVisited = false; isVisited = false;
} }
console.log("visited", visited, isVisited);
if (!user) { if (!user) {
return json({ error: "Unauthorized" }, { status: 401 }); return json({ error: "Unauthorized" }, { status: 401 });
} }
if (!type) { if (!type) {
const activityResults = await activitySearch(value, locals, isVisited); const activityResults = await activitySearch(value, locals, isVisited);
const locationResults = await locationSearch(value, locals); const locationResults = await locationSearch(value, locals, isVisited);
const namesResults = await nameSearch(value, locals); const namesResults = await nameSearch(value, locals, isVisited);
// remove duplicates by id // remove duplicates by id
let adventures: any = {}; let adventures: any = {};
@ -44,9 +45,9 @@ export const GET: RequestHandler = async ({
} else if (type === "activity") { } else if (type === "activity") {
return json(await activitySearch(value, locals, isVisited)); return json(await activitySearch(value, locals, isVisited));
} else if (type === "location") { } else if (type === "location") {
return json(await locationSearch(value, locals)); return json(await locationSearch(value, locals, isVisited));
} else if (type === "name") { } 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 }); 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 let res = await db
.select() .select()
.from(adventureTable) .from(adventureTable)
.where( .where(
and( and(
ilike(adventureTable.location, `%${value}%`), 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(); .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 let res = await db
.select() .select()
.from(adventureTable) .from(adventureTable)
.where( .where(
and( and(
ilike(adventureTable.name, `%${value}%`), 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(); .execute();

View file

@ -19,3 +19,10 @@ export const load: PageServerLoad = async ({ url, locals, fetch }) => {
let json = await data.json(); let json = await data.json();
return { props: { adventures: json.adventures } }; return { props: { adventures: json.adventures } };
}; };
export const actions = {
default: async () => {
console.log("default");
return { props: {} };
},
};

View file

@ -1,13 +1,98 @@
<script lang="ts"> <script lang="ts">
import { enhance } from "$app/forms";
import AdventureCard from "$lib/components/AdventureCard.svelte"; import AdventureCard from "$lib/components/AdventureCard.svelte";
import type { Adventure } from "$lib/utils/types"; import type { Adventure } from "$lib/utils/types";
import type { SubmitFunction } from "@sveltejs/kit";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
let visitedValue = "all";
let typeValue = "";
export let data: PageData; export let data: PageData;
let adventureArray: Adventure[] = data.props?.adventures as Adventure[]; let adventureArray: Adventure[] = data.props?.adventures as Adventure[];
const filter: SubmitFunction = async ({ formData }) => {
const radioValue = formData.get("visited");
const typeValue = formData.get("type");
const value = new URLSearchParams(location.search).get("value");
console.log(value);
console.log(
`/api/search?value=${value}&type=${typeValue}&visited=${radioValue}`
);
let data = await fetch(
`/api/search?value=${value}&type=${typeValue}&visited=${radioValue}`
);
console.log(data);
adventureArray = [];
let res = await data.json();
adventureArray = res.adventures as Adventure[];
console.log(radioValue);
};
</script> </script>
<main> <main>
<form method="post" use:enhance={filter}>
<input
type="radio"
name="visited"
value="all"
class="radio radio-primary"
bind:group={visitedValue}
checked
/>
All
<input
type="radio"
bind:group={visitedValue}
name="visited"
value="false"
class="radio radio-primary"
/>
Not Visited
<input
type="radio"
bind:group={visitedValue}
name="visited"
value="true"
class="radio radio-primary"
/>
Visited
<br />
<input
type="radio"
name="type"
value=""
class="radio radio-primary"
bind:group={typeValue}
/>
All
<input
type="radio"
name="type"
value="activity"
class="radio radio-primary"
bind:group={typeValue}
/>
Activity
<input
type="radio"
name="type"
bind:group={typeValue}
value="location"
class="radio radio-primary"
/>
Location
<input
type="radio"
bind:group={typeValue}
name="type"
value="name"
class="radio radio-primary"
/>
Name
<!-- submit button -->
<button type="submit" class="btn btn-primary">Search</button>
</form>
<h1 class="text-center font-bold text-4xl">Search Results</h1> <h1 class="text-center font-bold text-4xl">Search Results</h1>
{#if adventureArray.length > 0} {#if adventureArray.length > 0}
<div <div