From b380b5594241349c31b99fb8cb7ca2e1e6080954 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Thu, 1 Aug 2024 10:02:00 -0400 Subject: [PATCH] refactor: Add property filter to adventure search functionality --- backend/server/adventures/views.py | 42 ++++++++++- frontend/src/routes/search/+page.server.ts | 14 ++-- frontend/src/routes/search/+page.svelte | 86 +++++++++++++++++++--- 3 files changed, 124 insertions(+), 18 deletions(-) diff --git a/backend/server/adventures/views.py b/backend/server/adventures/views.py index e981fcc..dbbc280 100644 --- a/backend/server/adventures/views.py +++ b/backend/server/adventures/views.py @@ -151,12 +151,46 @@ class AdventureViewSet(viewsets.ModelViewSet): @action(detail=False, methods=['get']) def search(self, request): query = self.request.query_params.get('query', '') + property = self.request.query_params.get('property', 'all') if len(query) < 2: return Response({"error": "Query must be at least 2 characters long"}, status=400) - queryset = Adventure.objects.filter( - (Q(name__icontains=query) | Q(description__icontains=query) | Q(location__icontains=query) | Q(activity_types__icontains=query)) & - (Q(user_id=request.user.id) | Q(is_public=True)) -) + + if property not in ['name', 'type', 'location', 'description', 'activity_types']: + property = 'all' + + queryset = Adventure.objects.none() + + if property == 'name': + queryset = Adventure.objects.filter( + (Q(name__icontains=query)) & + (Q(user_id=request.user.id) | Q(is_public=True)) + ) + elif property == 'type': + queryset = Adventure.objects.filter( + (Q(type__icontains=query)) & + (Q(user_id=request.user.id) | Q(is_public=True)) + ) + elif property == 'location': + queryset = Adventure.objects.filter( + (Q(location__icontains=query)) & + (Q(user_id=request.user.id) | Q(is_public=True)) + ) + elif property == 'description': + queryset = Adventure.objects.filter( + (Q(description__icontains=query)) & + (Q(user_id=request.user.id) | Q(is_public=True)) + ) + elif property == 'activity_types': + queryset = Adventure.objects.filter( + (Q(activity_types__icontains=query)) & + (Q(user_id=request.user.id) | Q(is_public=True)) + ) + else: + queryset = Adventure.objects.filter( + (Q(name__icontains=query) | Q(description__icontains=query) | Q(location__icontains=query) | Q(activity_types__icontains=query)) & + (Q(user_id=request.user.id) | Q(is_public=True)) + ) + queryset = self.apply_sorting(queryset) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) diff --git a/frontend/src/routes/search/+page.server.ts b/frontend/src/routes/search/+page.server.ts index 0d270da..de98f84 100644 --- a/frontend/src/routes/search/+page.server.ts +++ b/frontend/src/routes/search/+page.server.ts @@ -7,17 +7,21 @@ const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000'; export const load = (async (event) => { // get url param query const query = event.url.searchParams.get('query'); + const property = event.url.searchParams.get('property') || 'all'; if (!query) { return { data: [] }; } - let res = await fetch(`${serverEndpoint}/api/adventures/search/?query=${query}`, { - headers: { - 'Content-Type': 'application/json', - Cookie: `${event.cookies.get('auth')}` + let res = await fetch( + `${serverEndpoint}/api/adventures/search/?query=${query}&property=${property}`, + { + headers: { + 'Content-Type': 'application/json', + Cookie: `${event.cookies.get('auth')}` + } } - }); + ); if (res.ok) { let data = await res.json(); diff --git a/frontend/src/routes/search/+page.svelte b/frontend/src/routes/search/+page.svelte index c26f2af..b196c60 100644 --- a/frontend/src/routes/search/+page.svelte +++ b/frontend/src/routes/search/+page.svelte @@ -6,6 +6,7 @@ import type { PageData } from './$types'; import EditAdventure from '$lib/components/EditAdventure.svelte'; import { appVersion } from '$lib/config'; + import { goto, invalidate } from '$app/navigation'; export let data: PageData; @@ -18,6 +19,15 @@ let publicAdventures: Adventure[] = []; let query: string | null = ''; + let property: string = 'all'; + + // on chage of property, console log the property + + function filterByProperty() { + let url = new URL(window.location.href); + url.searchParams.set('property', property); + goto(url.toString(), { invalidateAll: true }); + } onMount(() => { const urlParams = new URLSearchParams(window.location.search); @@ -47,18 +57,23 @@ }); console.log(data); + $: { + if (data.props) { + myAdventures = data.props.adventures; + publicAdventures = data.props.adventures; - if (data.props) { - myAdventures = data.props.adventures; - publicAdventures = data.props.adventures; + if (data.user?.pk != null) { + myAdventures = myAdventures.filter( + (adventure) => adventure.user_id === data.user?.pk ?? -1 + ); + } else { + myAdventures = []; + } - if (data.user?.pk != null) { - myAdventures = myAdventures.filter((adventure) => adventure.user_id === data.user?.pk ?? -1); - } else { - myAdventures = []; + publicAdventures = publicAdventures.filter( + (adventure) => adventure.user_id !== data.user?.pk + ); } - - publicAdventures = publicAdventures.filter((adventure) => adventure.user_id !== data.user?.pk); } let adventureToEdit: Adventure; @@ -97,6 +112,59 @@

AdventureLog Results

{/if} +
+ (property = 'all')} + /> + (property = 'name')} + /> + (property = 'type')} + /> + (property = 'location')} + /> + (property = 'description')} + /> + (property = 'activity_types')} + /> +
+ + {#if myAdventures.length > 0}

My Adventures