@@ -61,6 +61,20 @@ export class RecreationResourceSearchService {
61
61
lon,
62
62
} ) ;
63
63
64
+ const hasLocation = typeof lat === "number" && typeof lon === "number" ;
65
+
66
+ // Distance is used for sorting and is only calculated if lat/lon are provided
67
+ const distanceSql = hasLocation
68
+ ? Prisma . sql `, public.ST_Distance(
69
+ public.ST_Transform(public.ST_SetSRID(recreation_site_point, 3005), 3005),
70
+ public.ST_Transform(public.ST_SetSRID(public.ST_MakePoint(${ lon } , ${ lat } ), 4326), 3005)
71
+ ) as distance`
72
+ : Prisma . empty ;
73
+
74
+ const orderBySql = hasLocation
75
+ ? Prisma . sql `order by distance asc, name asc`
76
+ : Prisma . sql `order by name asc` ;
77
+
64
78
const [ recreationResources , combinedRecordCounts , combinedStaticCounts ] =
65
79
await this . prisma . $transaction ( [
66
80
this . prisma . $queryRaw < any [ ] > `
@@ -81,11 +95,12 @@ export class RecreationResourceSearchService {
81
95
recreation_structure,
82
96
has_toilets,
83
97
has_tables
98
+ ${ distanceSql }
84
99
from recreation_resource_search_view
85
100
${ whereClause }
86
- order by name asc
101
+ ${ orderBySql }
87
102
limit ${ take }
88
- ${ skip ? Prisma . sql `OFFSET ${ skip } ` : Prisma . empty } ;` ,
103
+ ${ skip ? Prisma . sql `offset ${ skip } ` : Prisma . empty } ;` ,
89
104
90
105
// Query filter menu content and counts that change based on search results
91
106
this . prisma . $queryRaw < CombinedRecordCount [ ] > `
0 commit comments