Skip to content

Commit

Permalink
Merge pull request #134 from seanmorley15/development
Browse files Browse the repository at this point in the history
chore: Add created_at field to Adventure and Collection models
  • Loading branch information
seanmorley15 authored Jul 18, 2024
2 parents b85d60a + 704eb6f commit 91f6a5c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.6 on 2024-07-18 19:27

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('adventures', '0009_alter_adventure_image'),
]

operations = [
migrations.AddField(
model_name='adventure',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='collection',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
]
2 changes: 2 additions & 0 deletions backend/server/adventures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Adventure(models.Model):
longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
collection = models.ForeignKey('Collection', on_delete=models.CASCADE, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)

def clean(self):
if self.collection:
Expand All @@ -53,6 +54,7 @@ class Collection(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(blank=True, null=True)
is_public = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)

# if connected adventures are private and collection is public, raise an error
def clean(self):
Expand Down
11 changes: 9 additions & 2 deletions backend/server/adventures/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class AdventureViewSet(viewsets.ModelViewSet):
pagination_class = StandardResultsSetPagination

def apply_sorting(self, queryset):
order_by = self.request.query_params.get('order_by', 'name')
order_by = self.request.query_params.get('order_by', 'created_at')
order_direction = self.request.query_params.get('order_direction', 'asc')
include_collections = self.request.query_params.get('include_collections', 'true')

valid_order_by = ['name', 'type', 'date', 'rating']
valid_order_by = ['name', 'type', 'date', 'rating', 'created_at']
if order_by not in valid_order_by:
order_by = 'name'

Expand All @@ -52,6 +52,13 @@ def apply_sorting(self, queryset):
if order_direction == 'desc':
ordering = f'-{ordering}'

# reverse ordering for created_at field
if order_by == 'created_at':
if order_direction == 'asc':
ordering = '-created_at'
else:
ordering = 'created_at'

print(f"Ordering by: {ordering}") # For debugging

if include_collections == 'false':
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Adventure = {
latitude: number | null;
longitude: number | null;
is_public: boolean;
created_at?: string;
};

export type Country = {
Expand Down Expand Up @@ -61,6 +62,7 @@ export type Collection = {
description: string;
is_public: boolean;
adventures: Adventure[];
created_at?: string;
};

export type OpenStreetMapPlace = {
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/routes/adventures/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@
/>
<br />
<p class="text-md font-semibold mt-2 mb-2">Order By</p>
<label for="name">Created At</label>
<input
type="radio"
name="order_by"
id="created_at"
class="radio radio-primary"
checked
value="created_at"
/>
<label for="name">Name</label>
<input
type="radio"
Expand Down
41 changes: 39 additions & 2 deletions frontend/src/routes/search/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
<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 { Adventure, OpenStreetMapPlace } from '$lib/types';
import { onMount } from 'svelte';
import type { PageData } from './$types';
import { page } from '$app/stores';
export let data: PageData;
function deleteAdventure(event: CustomEvent<number>) {
adventures = adventures.filter((adventure) => adventure.id !== event.detail);
}
let osmResults: OpenStreetMapPlace[] = [];
let query: string | null = '';
onMount(() => {
const urlParams = new URLSearchParams(window.location.search);
query = urlParams.get('query');
fetchData();
});
async function fetchData() {
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`);
const data = await res.json();
osmResults = data;
}
onMount(async () => {
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`);
const data = await res.json();
osmResults = data;
});
console.log(data);
let adventures: Adventure[] = [];
if (data.props) {
adventures = data.props.adventures;
}
</script>

{#if adventures.length === 0}
{#if adventures.length === 0 && osmResults.length === 0}
<NotFound error={data.error} />
{:else}
<h2 class="text-center font-bold text-2xl mb-4">AdventureLog Results</h2>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure}
<AdventureCard type={adventure.type} {adventure} on:delete={deleteAdventure} />
{/each}
</div>
<div class="divider"></div>
<h2 class="text-center font-bold text-2xl mb-4">Online Results</h2>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each osmResults as result}
<div class="bg-base-300 rounded-lg shadow-md p-4 w-96">
<h2 class="text-xl font-bold">{result.display_name}</h2>
<p>{result.type}</p>
<p>{result.lat}, {result.lon}</p>
</div>
{/each}
</div>
{/if}

0 comments on commit 91f6a5c

Please sign in to comment.