Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get events by geo query using PostGIS #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.validate": [
"javascript",
"typescript",
"vue"
],
"eslint.validate": ["javascript", "typescript", "vue"],
// Tailwind Support, see https://tailwindcss.nuxt.dev/tailwind/editor-support
"tailwindCSS.experimental.configFile": ".nuxt/tailwind.config.cjs",
"files.associations": {
"*.css": "tailwindcss"
},
// Auto-complete `.value` attribute when volar is installed
"volar.autoCompleteRefs": true,
"volar.autoCompleteRefs": true
}
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ services:
image: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_DB: db
iiio2 marked this conversation as resolved.
Show resolved Hide resolved
POSTGRES_USER: user
POSTGRES_PASSWORD: password

postgis:
image: postgis/postgis
volumes:
- postgis-data:/var/lib/postgis/data
ports:
- 5432:5432
environment:
POSTGRES_DB: db
POSTGRES_USER: user
POSTGRES_PASSWORD: password

volumes:
postgres-data:
postgis-data:
21 changes: 14 additions & 7 deletions pages/[username].vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<script setup lang="ts">
const { username } = useRoute().params
const { data: profile } = useFetch(`/api/profiles/${username}`)
const { username, lng, lat, distance } = useRoute().params;
const { data: profile } = useFetch(`/api/profiles/${username}`);

const { data: events } = useFetch('/api/events', {
const { data: events } = useFetch("/api/events", {
query: {
username
}
})
username,
distance,
lng: lng || null,
lat: lat || null,
},
});
</script>

<template>
<CityProfile v-if="profile.type === 'City'" :profile="profile" :events="events" />
<CityProfile
v-if="profile.type === 'City'"
:profile="profile"
:events="events"
/>
<pre v-else>{{ profile }}</pre>
</template>
56 changes: 29 additions & 27 deletions server/api/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { getQuery } from 'h3'
import { getQuery } from "h3";
import { PrismaClient } from "@prisma/client";

export default defineEventHandler((event) => {
const { username } = getQuery(event)
const prisma = new PrismaClient();

return event.context.prisma.event.findMany({
where: {
startDate: {
gte: new Date()
},
venue: {
city: {
username
}
}
},
include: {
venue: true,
organizer: true,
styles: true
},
orderBy: [
{
startDate: 'asc'
}
],
take: 10
})
})
export default defineEventHandler(async (event) => {
const {
lng: longitude,
lat: latitude,
distance: distanceInKm,
} = getQuery(event);

const lng = Number(longitude);
const lat = Number(latitude);
const distance = Number(distanceInKm);

const events = await prisma.$queryRaw`
SELECT *
FROM "Event"
WHERE "venueId" IN (
SELECT "Profile"."id"
FROM "Profile"
WHERE ST_Distance(
ST_MakePoint(${lng}, ${lat})::geography,
ST_MakePoint("Profile"."lng", "Profile"."lat")::geography
) <= (${distance} * 80000)
);
`;

return events;
});