1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-04 12:45: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") {
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();

View file

@ -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: {} };
},
};

View file

@ -1,13 +1,98 @@
<script lang="ts">
import { enhance } from "$app/forms";
import AdventureCard from "$lib/components/AdventureCard.svelte";
import type { Adventure } from "$lib/utils/types";
import type { SubmitFunction } from "@sveltejs/kit";
import type { PageData } from "./$types";
let visitedValue = "all";
let typeValue = "";
export let data: PageData;
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>
<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>
{#if adventureArray.length > 0}
<div