|
8 | 8 | "strings" |
9 | 9 |
|
10 | 10 | "github.com/gin-gonic/gin" |
| 11 | + "github.com/sndcds/grains/grains_api" |
11 | 12 | "github.com/sndcds/uranus/app" |
12 | 13 | "github.com/sndcds/uranus/sql_utils" |
13 | 14 | ) |
@@ -540,6 +541,97 @@ FROM ( |
540 | 541 | gc.JSON(http.StatusOK, gin.H{"venue-summary": venues}) |
541 | 542 | } |
542 | 543 |
|
| 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 | + |
543 | 635 | func validateAllowedQueryParams(c *gin.Context, allowed map[string]struct{}) error { |
544 | 636 | for key := range c.Request.URL.Query() { |
545 | 637 | if _, ok := allowed[key]; !ok { |
|
0 commit comments