Skip to content

Commit db2d289

Browse files
add events geojson
1 parent 250ed6a commit db2d289

File tree

5 files changed

+123
-2
lines changed

5 files changed

+123
-2
lines changed

api/get_events.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/gin-gonic/gin"
11+
"github.com/sndcds/grains/grains_api"
1112
"github.com/sndcds/uranus/app"
1213
"github.com/sndcds/uranus/sql_utils"
1314
)
@@ -540,6 +541,97 @@ FROM (
540541
gc.JSON(http.StatusOK, gin.H{"venue-summary": venues})
541542
}
542543

544+
func (h *ApiHandler) GetEventsGeoJSON(gc *gin.Context) {
545+
ctx := gc.Request.Context()
546+
apiRequest := grains_api.NewRequest(gc, "get-events-geojson")
547+
548+
dateConditions, conditionsStr, limitClause, args, _, err := h.buildEventFilters(gc)
549+
if err != nil {
550+
apiRequest.Error(http.StatusBadRequest, "")
551+
return
552+
}
553+
554+
query := app.UranusInstance.SqlGetEventsGeoJSON
555+
query = strings.Replace(query, "{{date_conditions}}", dateConditions, 1)
556+
query = strings.Replace(query, "{{conditions}}", conditionsStr, 1)
557+
query = strings.Replace(query, "{{limit}}", limitClause, 1)
558+
559+
debugf(query)
560+
debugf("ARGS (%d):\n", len(args))
561+
for i, arg := range args {
562+
fmt.Printf(" $%d = %#v (type %T)\n", i+1, arg, arg)
563+
}
564+
565+
rows, err := h.DbPool.Query(ctx, query, args...)
566+
if err != nil {
567+
debugf("internal server error: %v", err.Error())
568+
apiRequest.InternalServerError()
569+
return
570+
}
571+
defer rows.Close()
572+
573+
type EventResponse struct {
574+
EventDateId int `json:"event_date_id"`
575+
EventId int `json:"event_id"`
576+
VenueId *int `json:"venue_id"`
577+
VenueName *string `json:"venue_name"`
578+
VenueCity *string `json:"venue_city"`
579+
VenueCountry *string `json:"venue_country"`
580+
VenueLat *float64 `json:"venue_lat"`
581+
VenueLon *float64 `json:"venue_lon"`
582+
Title string `json:"title"`
583+
StartDate string `json:"start_date"`
584+
StartTime *string `json:"start_time"`
585+
}
586+
587+
var events []EventResponse
588+
589+
for rows.Next() {
590+
var e EventResponse
591+
if err := rows.Scan(
592+
&e.EventDateId,
593+
&e.EventId,
594+
&e.VenueId,
595+
&e.VenueName,
596+
&e.VenueCity,
597+
&e.VenueCountry,
598+
&e.VenueLon,
599+
&e.VenueLat,
600+
&e.Title,
601+
&e.StartDate,
602+
&e.StartTime,
603+
); err != nil {
604+
debugf("internal server error: %v", err.Error())
605+
apiRequest.InternalServerError()
606+
return
607+
}
608+
events = append(events, e)
609+
}
610+
611+
if len(events) == 0 {
612+
apiRequest.NotFound("no events found")
613+
return
614+
}
615+
616+
apiRequest.SetMeta("event_count", len(events))
617+
618+
lastEvent := events[len(events)-1]
619+
lastEventStartAt := lastEvent.StartDate
620+
if lastEvent.StartTime != nil {
621+
lastEventStartAt += "T" + *lastEvent.StartTime
622+
}
623+
lastEventDateId := lastEvent.EventDateId
624+
625+
apiRequest.Success(
626+
http.StatusOK,
627+
gin.H{
628+
"events": events,
629+
"last_event_start_at": lastEventStartAt,
630+
"last_event_date_id": lastEventDateId,
631+
},
632+
"")
633+
}
634+
543635
func validateAllowedQueryParams(c *gin.Context, allowed map[string]struct{}) error {
544636
for key := range c.Request.URL.Query() {
545637
if _, ok := allowed[key]; !ok {

api/get_venues_for_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// TODO: Review code
1111
// TODO: Add url parameter
1212

13-
func (h *ApiHandler) GetVenuesGeoJson(gc *gin.Context) {
13+
func (h *ApiHandler) GetVenuesGeoJSON(gc *gin.Context) {
1414
ctx := gc.Request.Context()
1515

1616
query := app.UranusInstance.SqlGetGeojsonVenues

app/uranus.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Uranus struct {
3333
SqlGetEventDates string
3434
SqlGetEventsProjected string
3535
SqlGetEventsTypeSummary string
36+
SqlGetEventsGeoJSON string
3637
SqlGetUserOrganizationPermissions string
3738
SqlGetUserEffectiveVenuePermissions string
3839
SqlGetAdminOrganization string
@@ -250,6 +251,7 @@ func (app *Uranus) PrepareSql() error {
250251
{"sql/get-event.sql", &app.SqlGetEvent, nil},
251252
{"sql/get-event-dates.sql", &app.SqlGetEventDates, nil},
252253
{"sql/get-events-projected.sql", &app.SqlGetEventsProjected, nil},
254+
{"sql/get-events-geojson.sql", &app.SqlGetEventsGeoJSON, nil},
253255

254256
{"sql/get-events-type-summary.sql", &app.SqlGetEventsTypeSummary, nil},
255257

sql/get-events-geojson.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SELECT
2+
edp.event_date_id,
3+
edp.event_id,
4+
v.venue_id,
5+
v.venue_name,
6+
v.venue_city,
7+
v.venue_country,
8+
ST_X(v.venue_geo_pos) AS venue_lon,
9+
ST_Y(v.venue_geo_pos) AS venue_lat,
10+
ep.title,
11+
TO_CHAR(edp.start_date, 'YYYY-MM-DD') AS start_date,
12+
TO_CHAR(edp.start_time, 'HH24:MI') AS start_time
13+
FROM {{schema}}.event_date_projection edp
14+
JOIN {{schema}}.event_projection ep
15+
ON ep.event_id = edp.event_id
16+
LEFT JOIN LATERAL (
17+
SELECT
18+
COALESCE(edp.venue_id, ep.venue_id) AS venue_id,
19+
COALESCE(edp.venue_name, ep.venue_name) AS venue_name,
20+
COALESCE(edp.venue_city, ep.venue_city) AS venue_city,
21+
COALESCE(edp.venue_country, ep.venue_country) AS venue_country,
22+
COALESCE(edp.venue_geo_pos, ep.venue_geo_pos) AS venue_geo_pos
23+
) v ON true
24+
WHERE {{date_conditions}}
25+
{{conditions}}
26+
{{limit}}

uranus-api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ func main() {
101101
publicRoute.GET("/events", apiHandler.GetEvents) // TODO: check!
102102
publicRoute.GET("/events/type-summary", apiHandler.GetEventTypeSummary) // TODO: check!
103103
publicRoute.GET("/events/venue-summary", apiHandler.GetEventVenueSummary) // TODO: check!
104+
publicRoute.GET("/events/geojson", apiHandler.GetEventsGeoJSON)
104105

105106
publicRoute.GET("/event/:eventId/date/:dateId", apiHandler.GetEventByDateId)
106107
publicRoute.GET("/event/:eventId/date/:dateId/ics", apiHandler.GetEventDateICS) // TODO: check!
107108

108-
publicRoute.GET("/venues/geojson", apiHandler.GetVenuesGeoJson) // TODO: check!
109+
publicRoute.GET("/venues/geojson", apiHandler.GetVenuesGeoJSON) // TODO: check!
109110

110111
publicRoute.GET("/organization/:organizationId", apiHandler.GetOrganization) // TODO: check!
111112
publicRoute.GET("/organizations", apiHandler.GetOrganizations) // TODO: check!

0 commit comments

Comments
 (0)