Skip to content

Commit ed8dfc1

Browse files
adjust get venues geojson to use standard uranus json header
1 parent db2d289 commit ed8dfc1

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

api/get_events.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ func (h *ApiHandler) GetEventsGeoJSON(gc *gin.Context) {
545545
ctx := gc.Request.Context()
546546
apiRequest := grains_api.NewRequest(gc, "get-events-geojson")
547547

548+
// Build SQL
548549
dateConditions, conditionsStr, limitClause, args, _, err := h.buildEventFilters(gc)
549550
if err != nil {
550551
apiRequest.Error(http.StatusBadRequest, "")
@@ -570,6 +571,7 @@ func (h *ApiHandler) GetEventsGeoJSON(gc *gin.Context) {
570571
}
571572
defer rows.Close()
572573

574+
// Event scan type
573575
type EventResponse struct {
574576
EventDateId int `json:"event_date_id"`
575577
EventId int `json:"event_id"`
@@ -584,6 +586,16 @@ func (h *ApiHandler) GetEventsGeoJSON(gc *gin.Context) {
584586
StartTime *string `json:"start_time"`
585587
}
586588

589+
type VenueEvents struct {
590+
Name string `json:"name"`
591+
Lon *float64 `json:"lon"`
592+
Lat *float64 `json:"lat"`
593+
Events []EventResponse `json:"events"`
594+
EventCount int `json:"event_count"`
595+
}
596+
597+
venues := make(map[int]*VenueEvents)
598+
587599
var events []EventResponse
588600

589601
for rows.Next() {
@@ -605,11 +617,33 @@ func (h *ApiHandler) GetEventsGeoJSON(gc *gin.Context) {
605617
apiRequest.InternalServerError()
606618
return
607619
}
620+
608621
events = append(events, e)
622+
623+
// Skip events without a venue
624+
if e.VenueId == nil {
625+
continue
626+
}
627+
628+
if e.VenueId != nil {
629+
vId := *e.VenueId
630+
if _, exists := venues[vId]; !exists {
631+
venues[vId] = &VenueEvents{
632+
Name: derefString(e.VenueName, ""),
633+
Lon: e.VenueLon,
634+
Lat: e.VenueLat,
635+
Events: []EventResponse{},
636+
// don't set EventCount yet
637+
}
638+
}
639+
640+
venues[vId].Events = append(venues[vId].Events, e)
641+
venues[vId].EventCount = len(venues[vId].Events)
642+
}
609643
}
610644

611645
if len(events) == 0 {
612-
apiRequest.NotFound("no events found")
646+
apiRequest.NoContent("no events found")
613647
return
614648
}
615649

@@ -625,11 +659,12 @@ func (h *ApiHandler) GetEventsGeoJSON(gc *gin.Context) {
625659
apiRequest.Success(
626660
http.StatusOK,
627661
gin.H{
628-
"events": events,
662+
"venues": venues,
629663
"last_event_start_at": lastEventStartAt,
630664
"last_event_date_id": lastEventDateId,
631665
},
632-
"")
666+
"",
667+
)
633668
}
634669

635670
func validateAllowedQueryParams(c *gin.Context, allowed map[string]struct{}) error {
@@ -640,3 +675,11 @@ func validateAllowedQueryParams(c *gin.Context, allowed map[string]struct{}) err
640675
}
641676
return nil
642677
}
678+
679+
// Helper for nil strings
680+
func derefString(s *string, fallback string) string {
681+
if s != nil && *s != "" {
682+
return *s
683+
}
684+
return fallback
685+
}

api/get_venues_for_map.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55

66
"github.com/gin-gonic/gin"
7+
"github.com/sndcds/grains/grains_api"
78
"github.com/sndcds/uranus/app"
89
)
910

@@ -12,12 +13,17 @@ import (
1213

1314
func (h *ApiHandler) GetVenuesGeoJSON(gc *gin.Context) {
1415
ctx := gc.Request.Context()
16+
apiRequest := grains_api.NewRequest(gc, "get-venues-geojson")
17+
18+
lang := gc.DefaultQuery("lang", "en")
19+
apiRequest.SetMeta("language", lang)
1520

1621
query := app.UranusInstance.SqlGetGeojsonVenues
22+
1723
// TODO: languageStr, default "en"
1824
rows, err := h.DbPool.Query(ctx, query, "en")
1925
if err != nil {
20-
gc.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
26+
apiRequest.InternalServerError()
2127
return
2228
}
2329
defer rows.Close()
@@ -30,30 +36,31 @@ func (h *ApiHandler) GetVenuesGeoJSON(gc *gin.Context) {
3036
}
3137

3238
// Iterate over rows and build JSON
33-
var results []map[string]interface{}
39+
var venues []map[string]interface{}
3440
for rows.Next() {
3541
values, err := rows.Values()
3642
if err != nil {
37-
gc.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
43+
apiRequest.InternalServerError()
3844
return
3945
}
4046

4147
rowMap := make(map[string]interface{}, len(values))
4248
for i, col := range columnNames {
4349
rowMap[col] = values[i]
4450
}
45-
results = append(results, rowMap)
51+
venues = append(venues, rowMap)
4652
}
4753

4854
if rows.Err() != nil {
49-
gc.JSON(http.StatusInternalServerError, gin.H{"error": rows.Err().Error()})
55+
apiRequest.InternalServerError()
5056
return
5157
}
5258

53-
if len(results) == 0 {
54-
gc.JSON(http.StatusNoContent, gin.H{"message": "query returned 0 results"})
59+
if len(venues) == 0 {
60+
apiRequest.NoContent("no venues found")
5561
return
5662
}
63+
apiRequest.SetMeta("venues_count", len(venues))
5764

58-
gc.IndentedJSON(http.StatusOK, results)
65+
apiRequest.Success(http.StatusOK, gin.H{"venues": venues}, "")
5966
}

0 commit comments

Comments
 (0)