mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
search api
This commit is contained in:
parent
aa03c49979
commit
7431d45124
5 changed files with 114 additions and 4 deletions
|
@ -139,6 +139,17 @@ class AdventureViewSet(viewsets.ModelViewSet):
|
||||||
serializer = self.get_serializer(queryset, many=True)
|
serializer = self.get_serializer(queryset, many=True)
|
||||||
|
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
@action(detail=False, methods=['get'])
|
||||||
|
def search(self, request):
|
||||||
|
query = self.request.query_params.get('query', '')
|
||||||
|
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)
|
||||||
|
adventures = self.paginate_and_respond(queryset, request)
|
||||||
|
return adventures
|
||||||
|
|
||||||
def paginate_and_respond(self, queryset, request):
|
def paginate_and_respond(self, queryset, request):
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
|
|
|
@ -2,8 +2,8 @@ version: "3.9"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
web:
|
web:
|
||||||
#build: ./frontend/
|
build: ./frontend/
|
||||||
image: ghcr.io/seanmorley15/adventurelog-frontend:latest
|
#image: ghcr.io/seanmorley15/adventurelog-frontend:latest
|
||||||
environment:
|
environment:
|
||||||
- PUBLIC_SERVER_URL=http://server:8000
|
- PUBLIC_SERVER_URL=http://server:8000
|
||||||
- ORIGIN=http://localhost:8080
|
- ORIGIN=http://localhost:8080
|
||||||
|
@ -23,8 +23,8 @@ services:
|
||||||
- postgres_data:/var/lib/postgresql/data/
|
- postgres_data:/var/lib/postgresql/data/
|
||||||
|
|
||||||
server:
|
server:
|
||||||
#build: ./backend/
|
build: ./backend/
|
||||||
image: ghcr.io/seanmorley15/adventurelog-backend:latest
|
#image: ghcr.io/seanmorley15/adventurelog-backend:latest
|
||||||
environment:
|
environment:
|
||||||
- PGHOST=db
|
- PGHOST=db
|
||||||
- PGDATABASE=database
|
- PGDATABASE=database
|
||||||
|
|
|
@ -193,6 +193,44 @@
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
{#if currentView == 'table'}
|
||||||
|
<table class="table table-compact w-full">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Date</th>
|
||||||
|
<th>Rating</th>
|
||||||
|
<th>Type</th>
|
||||||
|
<th>Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{#each adventures as adventure}
|
||||||
|
<tr>
|
||||||
|
<td>{adventure.name}</td>
|
||||||
|
<td>{adventure.date}</td>
|
||||||
|
<td>{adventure.rating}</td>
|
||||||
|
<td>{adventure.type}</td>
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
class="btn btn-sm btn-primary"
|
||||||
|
on:click={() => editAdventure(new CustomEvent('edit', { detail: adventure }))}
|
||||||
|
>
|
||||||
|
Edit
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-sm btn-error"
|
||||||
|
on:click={() =>
|
||||||
|
deleteAdventure(new CustomEvent('delete', { detail: adventure.id }))}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{/each}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{/if}
|
||||||
<div class="join flex items-center justify-center mt-4">
|
<div class="join flex items-center justify-center mt-4">
|
||||||
{#if next || previous}
|
{#if next || previous}
|
||||||
<div class="join">
|
<div class="join">
|
||||||
|
|
39
frontend/src/routes/search/+page.server.ts
Normal file
39
frontend/src/routes/search/+page.server.ts
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import type { Adventure } from '$lib/types';
|
||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
||||||
|
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
|
||||||
|
|
||||||
|
export const load = (async (event) => {
|
||||||
|
// get url param query
|
||||||
|
const query = event.url.searchParams.get('query');
|
||||||
|
|
||||||
|
if (!query) {
|
||||||
|
return { data: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = await fetch(`${serverEndpoint}/api/adventures/search/?query=${query}`, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Cookie: `${event.cookies.get('auth')}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
let data = await res.json();
|
||||||
|
console.log('Search data:', data);
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
adventures: data.results as Adventure[],
|
||||||
|
nextPage: data.next,
|
||||||
|
prevPage: data.previous,
|
||||||
|
total: data.count,
|
||||||
|
query
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
console.error('Failed to fetch search data');
|
||||||
|
return { data: [] };
|
||||||
|
}
|
||||||
|
}) satisfies PageServerLoad;
|
22
frontend/src/routes/search/+page.svelte
Normal file
22
frontend/src/routes/search/+page.svelte
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<script lang="ts">
|
||||||
|
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
||||||
|
import NotFound from '$lib/components/NotFound.svelte';
|
||||||
|
import type { Adventure } from '$lib/types';
|
||||||
|
import type { PageData } from './$types';
|
||||||
|
|
||||||
|
export let data: PageData;
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
let adventures: Adventure[] = [];
|
||||||
|
if (data.props) {
|
||||||
|
adventures = data.props.adventures;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if adventures.length === 0}
|
||||||
|
<NotFound />
|
||||||
|
{:else}
|
||||||
|
{#each adventures as adventure}
|
||||||
|
<AdventureCard type={adventure.type} {adventure} />
|
||||||
|
{/each}
|
||||||
|
{/if}
|
Loading…
Add table
Add a link
Reference in a new issue