|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/gin-gonic/gin" |
| 8 | + "github.com/sndcds/grains/grains_api" |
| 9 | + "github.com/sndcds/uranus/app" |
| 10 | +) |
| 11 | + |
| 12 | +// GetVenue returns a venue by Id with spaces and organization |
| 13 | +func (h *ApiHandler) GetVenue(gc *gin.Context) { |
| 14 | + ctx := gc.Request.Context() |
| 15 | + apiRequest := grains_api.NewRequest(gc, "ger-venue") |
| 16 | + |
| 17 | + // Structs for nested data |
| 18 | + type SpaceResult struct { |
| 19 | + Id int `json:"id"` |
| 20 | + Name *string `json:"name,omitempty"` |
| 21 | + TotalCapacity *int `json:"total_capacity,omitempty"` |
| 22 | + SeatingCapacity *int `json:"seating_capacity,omitempty"` |
| 23 | + BuildingLevel *int `json:"building_level,omitempty"` |
| 24 | + WebsiteLink *string `json:"website_link,omitempty"` |
| 25 | + Description *string `json:"description,omitempty"` |
| 26 | + AreaSqm *float64 `json:"area_sqm,omitempty"` |
| 27 | + SpaceType *string `json:"space_type,omitempty"` |
| 28 | + SpaceTypeName *string `json:"space_type_name,omitempty"` |
| 29 | + SpaceTypeDesc *string `json:"space_type_description,omitempty"` |
| 30 | + } |
| 31 | + |
| 32 | + type OrganizationResult struct { |
| 33 | + Id int `json:"id"` |
| 34 | + Name *string `json:"name,omitempty"` |
| 35 | + WebsiteLink *string `json:"website_link,omitempty"` |
| 36 | + City *string `json:"city,omitempty"` |
| 37 | + Country *string `json:"country,omitempty"` |
| 38 | + } |
| 39 | + |
| 40 | + type VenueResult struct { |
| 41 | + Id int `json:"id"` |
| 42 | + Name *string `json:"name,omitempty"` |
| 43 | + Type *string `json:"type,omitempty"` |
| 44 | + TypeName *string `json:"type_name,omitempty"` |
| 45 | + TypeDescription *string `json:"type_description,omitempty"` |
| 46 | + OpenedAt *string `json:"opened_at,omitempty"` |
| 47 | + ClosedAt *string `json:"closed_at,omitempty"` |
| 48 | + Description *string `json:"description,omitempty"` |
| 49 | + Street *string `json:"street,omitempty"` |
| 50 | + HouseNumber *string `json:"house_number,omitempty"` |
| 51 | + PostalCode *string `json:"postal_code,omitempty"` |
| 52 | + City *string `json:"city,omitempty"` |
| 53 | + Country *string `json:"country,omitempty"` |
| 54 | + State *string `json:"state,omitempty"` |
| 55 | + ContactEmail *string `json:"contact_email,omitempty"` |
| 56 | + ContactPhone *string `json:"contact_phone,omitempty"` |
| 57 | + WebsiteLink *string `json:"website_link,omitempty"` |
| 58 | + Lon *float64 `json:"lon,omitempty"` |
| 59 | + Lat *float64 `json:"lat,omitempty"` |
| 60 | + Organization *OrganizationResult `json:"organization,omitempty"` |
| 61 | + Spaces []SpaceResult `json:"spaces,omitempty"` |
| 62 | + } |
| 63 | + |
| 64 | + venueId, ok := ParamInt(gc, "venueId") |
| 65 | + if !ok { |
| 66 | + apiRequest.Error(http.StatusBadRequest, "venue Id is required") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + lang := gc.DefaultQuery("lang", "en") |
| 71 | + apiRequest.SetMeta("language", lang) |
| 72 | + |
| 73 | + query := app.UranusInstance.SqlGetVenue |
| 74 | + |
| 75 | + row := h.DbPool.QueryRow(ctx, query, venueId, lang) |
| 76 | + |
| 77 | + // Temporary variables for SQL scan |
| 78 | + var venue VenueResult |
| 79 | + var org OrganizationResult |
| 80 | + var spacesJSON []byte |
| 81 | + |
| 82 | + err := row.Scan( |
| 83 | + &venue.Id, |
| 84 | + &venue.Name, |
| 85 | + &venue.Type, |
| 86 | + &venue.TypeName, |
| 87 | + &venue.TypeDescription, |
| 88 | + &venue.OpenedAt, |
| 89 | + &venue.ClosedAt, |
| 90 | + &venue.Description, |
| 91 | + &venue.Street, |
| 92 | + &venue.HouseNumber, |
| 93 | + &venue.PostalCode, |
| 94 | + &venue.City, |
| 95 | + &venue.Country, |
| 96 | + &venue.State, |
| 97 | + &venue.ContactEmail, |
| 98 | + &venue.ContactPhone, |
| 99 | + &venue.WebsiteLink, |
| 100 | + &venue.Lon, |
| 101 | + &venue.Lat, |
| 102 | + &org.Id, |
| 103 | + &org.Name, |
| 104 | + &org.WebsiteLink, |
| 105 | + &org.City, |
| 106 | + &org.Country, |
| 107 | + &spacesJSON, |
| 108 | + ) |
| 109 | + if err != nil { |
| 110 | + debugf("GetVenue error: %v", err) |
| 111 | + apiRequest.SetMeta("err_code", "1001") |
| 112 | + apiRequest.InternalServerError() |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + // Assign organization if any fields are non-nil |
| 117 | + if org.Name != nil || org.WebsiteLink != nil || org.City != nil || org.Country != nil { |
| 118 | + venue.Organization = &org |
| 119 | + } |
| 120 | + |
| 121 | + // Decode spaces JSON into structs |
| 122 | + if len(spacesJSON) > 0 { |
| 123 | + if err := json.Unmarshal(spacesJSON, &venue.Spaces); err != nil { |
| 124 | + apiRequest.SetMeta("err_code", "1002") |
| 125 | + apiRequest.InternalServerError() |
| 126 | + return |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + apiRequest.Success(http.StatusOK, venue, "") |
| 131 | +} |
0 commit comments