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 2 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
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,18 @@ services:
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:
- 5431:5431
environment:
POSTGRES_DB: db
POSTGRES_USER: user
POSTGRES_PASSWORD: password

volumes:
postgres-data:
postgis-data:
47 changes: 22 additions & 25 deletions server/api/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
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, lat, distance } = getQuery(event)
iiio2 marked this conversation as resolved.
Show resolved Hide resolved

const events = await prisma.$queryRaw`
SELECT *
FROM "Event"
WHERE venueId IN (
SELECT id
FROM "Profile"
WHERE ST_Distance_Sphere(
ST_MakePoint(${lng}, ${lat}),
ST_MakePoint(lng, lat)
) <= ${(distance as number ?? 50) * 1000}
);
`;

return events;

})