Skip to content

Commit 0ce67f1

Browse files
refactor get events query parameter handling
1 parent f48158a commit 0ce67f1

File tree

2 files changed

+94
-75
lines changed

2 files changed

+94
-75
lines changed

api/get_events.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type eventResponse struct {
3434
TicketLink *string `json:"ticket_link,omitempty"`
3535
SpaceId *int `json:"space_id,omitempty"`
3636
SpaceName *string `json:"space_name,omitempty"`
37-
SpaceAccessibilityFlags *int64 `json:"space_accessibility_flags,omitempty"`
37+
SpaceAccessibilityFlags *string `json:"space_accessibility_flags,omitempty"`
3838
VenueId *int `json:"venue_id,omitempty"`
3939
VenueName *string `json:"venue_name,omitempty"`
4040
VenueCity *string `json:"venue_city,omitempty"`
@@ -111,7 +111,7 @@ func (h *ApiHandler) buildEventFilters(gc *gin.Context) (
111111
cityStr, _ := GetContextParam(gc, "city")
112112
eventTypesStr, _ := GetContextParam(gc, "event_types")
113113
tagsStr, _ := GetContextParam(gc, "tags")
114-
accessibilityInfosStr, _ := GetContextParam(gc, "accessibility")
114+
accessibilityFlagsStr, _ := GetContextParam(gc, "accessibility")
115115
visitorInfosStr, _ := GetContextParam(gc, "visitor_infos")
116116
ageStr, _ := GetContextParam(gc, "age")
117117
lonStr, _ := GetContextParam(gc, "lon")
@@ -160,28 +160,37 @@ func (h *ApiHandler) buildEventFilters(gc *gin.Context) (
160160
if errBuild != nil {
161161
return "", "", "", nil, 0, errBuild
162162
}
163+
163164
nextArgIndex, errBuild = sql_utils.BuildSanitizedSearchCondition(searchStr, "ep.search_text", "search", nextArgIndex, &conditions, &args)
164165
if errBuild != nil {
165166
return "", "", "", nil, 0, errBuild
166167
}
168+
167169
nextArgIndex, errBuild = sql_utils.BuildSanitizedIlikeCondition(titleStr, "ep.title", "title", nextArgIndex, &conditions, &args)
168170
if errBuild != nil {
169171
return "", "", "", nil, 0, errBuild
170172
}
171173

172174
if countryCodesStr != "" {
173-
format := "COALESCE(edp.venue_country, ep.venue_country) = ANY(%s)"
174-
nextArgIndex, errBuild = sql_utils.BuildInConditionForStringSlice(countryCodesStr, format, "countries", nextArgIndex, &conditions, &args)
175+
nextArgIndex, errBuild = sql_utils.BuildInConditionForStringSlice(
176+
countryCodesStr,
177+
"COALESCE(edp.venue_country, ep.venue_country) = ANY($%d::text[])",
178+
nextArgIndex,
179+
&conditions,
180+
&args,
181+
)
175182
if errBuild != nil {
176183
return "", "", "", nil, 0, errBuild
177184
}
178185
}
186+
179187
if postalCodeStr != "" {
180188
nextArgIndex, errBuild = sql_utils.BuildLikeConditions(postalCodeStr, "COALESCE(edp.venue_postal_code, ep.venue_postal_code)", nextArgIndex, &conditions, &args)
181189
if errBuild != nil {
182190
return "", "", "", nil, 0, errBuild
183191
}
184192
}
193+
185194
nextArgIndex, errBuild = sql_utils.BuildSanitizedIlikeCondition(cityStr, "COALESCE(edp.venue_city, ep.venue_city)", "city", nextArgIndex, &conditions, &args)
186195
if errBuild != nil {
187196
return "", "", "", nil, 0, errBuild
@@ -192,42 +201,50 @@ func (h *ApiHandler) buildEventFilters(gc *gin.Context) (
192201
return "", "", "", nil, 0, errBuild
193202
}
194203
}
204+
195205
if organizationIdsStr != "" {
196206
nextArgIndex, errBuild = sql_utils.BuildColumnInIntCondition(organizationIdsStr, "ep.organization_id", nextArgIndex, &conditions, &args)
197207
if errBuild != nil {
198208
return "", "", "", nil, 0, errBuild
199209
}
200210
}
211+
201212
if venueIdsStr != "" {
202213
nextArgIndex, errBuild = sql_utils.BuildColumnInIntCondition(venueIdsStr, "COALESCE(edp.venue_id, ep.venue_id)", nextArgIndex, &conditions, &args)
203214
if errBuild != nil {
204215
return "", "", "", nil, 0, errBuild
205216
}
206217
}
218+
207219
if spaceIdsStr != "" {
208220
nextArgIndex, errBuild = sql_utils.BuildColumnInIntCondition(spaceIdsStr, "COALESCE(edp.space_id, ep.space_id)", nextArgIndex, &conditions, &args)
209221
if errBuild != nil {
210222
return "", "", "", nil, 0, errBuild
211223
}
212224
}
225+
213226
if spaceTypesStr != "" {
214227
nextArgIndex, errBuild = sql_utils.BuildColumnInIntCondition(spaceTypesStr, "COALESCE(edp.space_type_id, ep.space_type_id)", nextArgIndex, &conditions, &args)
215228
if errBuild != nil {
216229
return "", "", "", nil, 0, errBuild
217230
}
218231
}
232+
219233
nextArgIndex, errBuild = sql_utils.BuildGeoRadiusCondition(lonStr, latStr, radiusStr, "COALESCE(edp.venue_geo_pos, ep.venue_geo_pos)", nextArgIndex, &conditions, &args)
220234
if errBuild != nil {
221235
return "", "", "", nil, 0, errBuild
222236
}
237+
223238
nextArgIndex, errBuild = sql_utils.BuildContainedInColumnIntRangeCondition(ageStr, "ep.min_age", "ep.max_age", nextArgIndex, &conditions, &args)
224239
if errBuild != nil {
225240
return "", "", "", nil, 0, errBuild
226241
}
227-
nextArgIndex, errBuild = sql_utils.BuildBitmaskCondition(accessibilityInfosStr, "edp.space_accessibility_flags", "accessibility", nextArgIndex, &conditions, &args)
242+
243+
nextArgIndex, errBuild = sql_utils.BuildBitmaskCondition(accessibilityFlagsStr, "edp.space_accessibility_flags", "accessibility", nextArgIndex, &conditions, &args)
228244
if errBuild != nil {
229245
return "", "", "", nil, 0, errBuild
230246
}
247+
231248
nextArgIndex, errBuild = sql_utils.BuildBitmaskCondition(visitorInfosStr, "edp.visitor_info_flags", "visitor_infos", nextArgIndex, &conditions, &args)
232249
if errBuild != nil {
233250
return "", "", "", nil, 0, errBuild
@@ -248,9 +265,9 @@ func (h *ApiHandler) buildEventFilters(gc *gin.Context) (
248265
}
249266

250267
if tagsStr != "" {
251-
nextArgIndex, errBuild = sql_utils.BuildArrayContainsCondition(
268+
nextArgIndex, errBuild = sql_utils.BuildInConditionForStringSlice(
252269
tagsStr,
253-
"tags",
270+
"tags && $%d::text[]",
254271
nextArgIndex,
255272
&conditions,
256273
&args,
@@ -288,6 +305,8 @@ func (h *ApiHandler) GetEvents(gc *gin.Context) {
288305
query = strings.Replace(query, "{{conditions}}", conditionsStr, 1)
289306
query = strings.Replace(query, "{{limit}}", limitClause, 1)
290307

308+
fmt.Println(query)
309+
291310
rows, err := h.DbPool.Query(ctx, query, args...)
292311
if err != nil {
293312
gc.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("query failed: %v", err)})
@@ -410,8 +429,6 @@ ORDER BY date_count DESC`,
410429
query = strings.Replace(query, "{{conditions}}", conditionsStr, 1)
411430
// query = strings.Replace(query, "{{limit}}", limitClause, 1)
412431

413-
fmt.Println("GetEventTypeSummary query:", query)
414-
415432
rows, err := h.DbPool.Query(gc.Request.Context(), query, args...)
416433
if err != nil {
417434
gc.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})

0 commit comments

Comments
 (0)