feat: whatever the fuck this is, but sqlc is getting in my way a bit; decode the jsonb, i hate this

This commit is contained in:
etwas 2025-04-15 21:48:06 +02:00
parent 086bec1f66
commit 344e22235c
Signed by: etwas
SSH key fingerprint: SHA256:bHhIeAdn/2k9jmOs6+u6ox98VYmoHUN3HfnpV2w8Ws0

51
main.go
View file

@ -195,6 +195,49 @@ func (h *stopIdInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// Return departures at specific stop // Return departures at specific stop
type GetDeparturesByIdRowWithString struct {
bahndb_rest.GetDeparturesByIdRow
}
func (r GetDeparturesByIdRowWithString) MarshalJSON() ([]byte, error) {
var direction map[string]interface{}
err := json.Unmarshal(r.Direction, &direction)
if err != nil {
return nil, err
}
// Create an anonymous struct that mirrors the original but with string Direction
tmp := struct {
ID string `json:"id"`
LineName pgtype.Text `json:"lineName"`
LineProductName pgtype.Text `json:"lineProductName"`
DepartureDelay pgtype.Int4 `json:"departureDelay"`
Direction interface{} `json:"direction"`
Stopovers interface{} `json:"stopovers"`
Departure pgtype.Timestamptz `json:"departure"`
PlannedDeparture pgtype.Timestamptz `json:"plannedDeparture"`
DeparturePlatform pgtype.Text `json:"departurePlattform"`
PlannedDeparturePlatform pgtype.Text `json:"plannedDeparturePlattform"`
Cancelled pgtype.Text `json:"cancelled"`
RealtimeDataUpdatedAt pgtype.Timestamptz `json:"realtimeDataUpdatedAt"`
}{
ID: r.ID,
LineName: r.LineName,
LineProductName: r.LineProductName,
DepartureDelay: r.DepartureDelay,
Direction: direction,
Stopovers: r.Stopovers,
Departure: r.Departure,
PlannedDeparture: r.PlannedDeparture,
DeparturePlatform: r.DeparturePlatform,
PlannedDeparturePlatform: r.PlannedDeparturePlatform,
Cancelled: r.Cancelled,
RealtimeDataUpdatedAt: r.RealtimeDataUpdatedAt,
}
return json.Marshal(tmp)
}
type stopDeparturesHandler struct{} type stopDeparturesHandler struct{}
func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -273,10 +316,16 @@ func (h *stopDeparturesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
return return
} }
var stationsReturn []GetDeparturesByIdRowWithString
for _, station := range stations {
stationsReturn = append(stationsReturn, GetDeparturesByIdRowWithString{station})
}
// Return Data // Return Data
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
err = enc.Encode(stations) err = enc.Encode(stationsReturn)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)