Skip to content

Commit

Permalink
update data flightnumber endpoint to use flights data
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Aug 25, 2024
1 parent e56bf12 commit c226f1d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
26 changes: 18 additions & 8 deletions go/api/data/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/csv"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/explore-flights/monorepo/go/common"
Expand Down Expand Up @@ -257,16 +256,27 @@ func (h *Handler) Aircraft(ctx context.Context) ([]Aircraft, error) {
}

func (h *Handler) FlightNumber(ctx context.Context, fn, airport string, d common.LocalDate) (*common.Flight, error) {
return loadJson[*common.Flight](
flights, err := loadJson[[]*common.Flight](
ctx,
h,
fmt.Sprintf(
"processed/flight_numbers/%s/%s/%s.json",
fn,
airport,
d.Time(nil).Format("2006/01/02"),
),
"processed/flights/"+d.Time(nil).Format("2006/01/02")+".json",
)

if err != nil {
if adapt.IsS3NotFound(err) {
return nil, nil
} else {
return nil, err
}
}

for _, f := range flights {
if f.Number().String() == fn && f.DepartureAirport == airport {
return f, nil
}
}

return nil, nil
}

func (h *Handler) loadCsv(ctx context.Context, name string) (*csvReader, error) {
Expand Down
9 changes: 4 additions & 5 deletions go/api/web/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"github.com/explore-flights/monorepo/go/api/data"
"github.com/explore-flights/monorepo/go/common"
"github.com/explore-flights/monorepo/go/common/adapt"
"github.com/labstack/echo/v4"
"net/http"
)
Expand Down Expand Up @@ -59,10 +58,6 @@ func NewFlightNumberHandler(dh *data.Handler) echo.HandlerFunc {

flight, err := dh.FlightNumber(c.Request().Context(), fn, airport, d)
if err != nil {
if adapt.IsS3NotFound(err) {
return echo.NewHTTPError(http.StatusNotFound)
}

noCache(c)

if errors.Is(err, context.DeadlineExceeded) {
Expand All @@ -72,6 +67,10 @@ func NewFlightNumberHandler(dh *data.Handler) echo.HandlerFunc {
return echo.NewHTTPError(http.StatusInternalServerError)
}

if flight == nil {
return echo.NewHTTPError(http.StatusNotFound)
}

return c.JSON(http.StatusOK, flight)
}
}

0 comments on commit c226f1d

Please sign in to comment.