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

New search api refactoring

This commit is contained in:
Sean Morley 2024-06-03 23:10:08 +00:00
parent 2a5c7c12b1
commit 50a2cbcd4c
4 changed files with 117 additions and 102 deletions

View file

@ -18,16 +18,20 @@
onMount(() => {
if (window.location.pathname === "/search") {
searchVal = new URLSearchParams(window.location.search).get("all") || "";
searchVal =
new URLSearchParams(window.location.search).get("value") || "";
}
});
async function goToSearch() {
if (searchVal === "") {
return;
}
let reload: boolean = false;
if (window.location.pathname === "/search") {
reload = true;
}
await goto("/search?all=" + searchVal);
await goto("/search?value=" + searchVal);
if (reload) {
location.reload();
}

View file

@ -0,0 +1,105 @@
import { db } from "$lib/db/db.server";
import { adventureTable } from "$lib/db/schema";
import { json, type RequestHandler } from "@sveltejs/kit";
import { ilike, and, eq, arrayContains, or } from "drizzle-orm";
export const GET: RequestHandler = async ({
url,
locals,
}): Promise<Response> => {
const value = url.searchParams.get("value") as string;
const type = url.searchParams.get("type") as string;
const user = locals.user;
const visited = url.searchParams.get("visited");
let isVisited: boolean | undefined = undefined;
if (visited === "true") {
isVisited = true;
} else if (visited === "false") {
isVisited = false;
}
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);
return json({
adventures: [
...activityResults.adventures,
...locationResults.adventures,
...namesResults.adventures,
],
});
} else if (type === "activity") {
return json(await activitySearch(value, locals, isVisited));
} else if (type === "location") {
return json(await locationSearch(value, locals));
} else if (type === "name") {
return json(await nameSearch(value, locals));
}
return json({ error: "No results found." }, { status: 400 });
};
async function activitySearch(
value: string,
locals: any,
visited: boolean | undefined
) {
let arr: string[] = [];
arr.push(value.toLowerCase());
let res = await db
.select()
.from(adventureTable)
.where(
and(
arrayContains(adventureTable.activityTypes, arr),
eq(adventureTable.userId, locals.user.id),
or(
visited === true ? eq(adventureTable.type, "mylog") : undefined,
visited === false ? eq(adventureTable.type, "planner") : undefined
)
)
)
.execute();
return {
adventures: res,
};
}
async function locationSearch(value: string, locals: any) {
let res = await db
.select()
.from(adventureTable)
.where(
and(
ilike(adventureTable.location, `%${value}%`),
eq(adventureTable.userId, locals.user.id)
)
)
.execute();
return {
adventures: res,
};
}
async function nameSearch(value: string, locals: any) {
let res = await db
.select()
.from(adventureTable)
.where(
and(
ilike(adventureTable.name, `%${value}%`),
eq(adventureTable.userId, locals.user.id)
)
)
.execute();
return {
adventures: res,
};
}

View file

@ -1,9 +1,6 @@
// +page.server.js
import { redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { db } from "$lib/db/db.server";
import { adventureTable } from "$lib/db/schema";
import { and, eq, arrayContains, ilike } from "drizzle-orm";
/**
* Loads the page data based on the provided URL and locals.
@ -11,104 +8,14 @@ import { and, eq, arrayContains, ilike } from "drizzle-orm";
* @param {PageServerLoadParams} params - The parameters for loading the page.
* @returns {Promise<PageServerLoadResult>} The result of loading the page.
*/
export const load: PageServerLoad = async ({ url, locals }) => {
export const load: PageServerLoad = async ({ url, locals, fetch }) => {
if (!locals.user) {
return redirect(301, "/login");
}
let param: string = "";
let value: string = "";
if (Array.from(url.searchParams.entries()).length > 0) {
const params = Array.from(url.searchParams.entries());
param = params[0][0];
value = params[0][1];
} else {
param = "all";
value = "";
}
// Activity type search
if (param === "activity") {
return {
props: await activitySearch(value, locals),
};
}
if (param === "location") {
return {
props: await locationSearch(value, locals),
};
}
if (param === "name") {
return {
props: await nameSearch(value, locals),
};
}
if (param == "all" || param == "") {
console.log("all");
const activityResults = await activitySearch(value, locals);
const locationResults = await locationSearch(value, locals);
const namesResults = await nameSearch(value, locals);
return {
props: {
adventures: [
...activityResults.adventures,
...locationResults.adventures,
...namesResults.adventures,
],
},
};
}
async function activitySearch(value: string, locals: any) {
let arr: string[] = [];
arr.push(value.toLowerCase());
let res = await db
.select()
.from(adventureTable)
.where(
and(
arrayContains(adventureTable.activityTypes, arr),
eq(adventureTable.userId, locals.user.id)
)
)
.execute();
return {
adventures: res,
};
}
async function locationSearch(value: string, locals: any) {
let res = await db
.select()
.from(adventureTable)
.where(
and(
ilike(adventureTable.location, `%${value}%`),
eq(adventureTable.userId, locals.user.id)
)
)
.execute();
return {
adventures: res,
};
}
async function nameSearch(value: string, locals: any) {
let res = await db
.select()
.from(adventureTable)
.where(
and(
ilike(adventureTable.name, `%${value}%`),
eq(adventureTable.userId, locals.user.id)
)
)
.execute();
return {
adventures: res,
};
if (!url.searchParams.has("value")) {
return { props: { adventures: [] } };
}
let data = await fetch("/api/search?value=" + url.searchParams.get("value"));
let json = await data.json();
return { props: { adventures: json.adventures } };
};

View file

@ -5,7 +5,6 @@
export let data: PageData;
let adventureArray: Adventure[] = data.props?.adventures as Adventure[];
console.log(adventureArray);
</script>
<main>