Skip to content

Commit

Permalink
add first part of airports page and some optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
vsimakhin committed Feb 14, 2025
1 parent f8b40ba commit f1a0778
Show file tree
Hide file tree
Showing 19 changed files with 427 additions and 360 deletions.
36 changes: 11 additions & 25 deletions app/handlers_airport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func (app *application) HandlerAirportByID(w http.ResponseWriter, r *http.Reques
app.writeJSON(w, http.StatusOK, airport)
}

// HandlerApiAirportList returns a list of standard airports
func (app *application) HandlerApiAirportList(w http.ResponseWriter, r *http.Request) {
airports, err := app.db.GetStandardAirports()
if err != nil {
app.handleError(w, err)
return
}

app.writeJSON(w, http.StatusOK, airports)
}

/////////////////////////////////////////////////
// for futher review
/////////////////////////////////////////////////
Expand Down Expand Up @@ -262,28 +273,3 @@ func (app *application) HandlerAirportCustomData(w http.ResponseWriter, r *http.
}

// HandlerAirportDBData generates data for the standard airports table
func (app *application) HandlerAirportDBData(w http.ResponseWriter, r *http.Request) {

type TableData struct {
Data [][]string `json:"data"`
}

var tableData TableData

airports, err := app.db.GetStandardAirports()
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

for _, item := range airports {
tableRow := []string{item.ICAO, item.IATA, item.Name, item.City, item.Country,
fmt.Sprintf("%d", item.Elevation), fmt.Sprintf("%f", item.Lat), fmt.Sprintf("%f", item.Lon), "",
}

tableData.Data = append(tableData.Data, tableRow)
}

app.writeJSON(w, http.StatusOK, tableData)
}
3 changes: 2 additions & 1 deletion app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (app *application) routes() *chi.Mux {

// airports
r.Route("/airport", func(r chi.Router) {
r.With(middleware.Compress(5)).Get("/standard-list", app.HandlerApiAirportList)
r.Get("/{id}", app.HandlerAirportByID)
})

Expand Down Expand Up @@ -200,7 +201,7 @@ func (app *application) routes() *chi.Mux {
// airports
r.Get(APIAirportID, app.HandlerAirportByID)
r.Get(APIAirportUpdate, app.HandlerAirportUpdate)
r.Get(APIAirportStandardData, app.HandlerAirportDBData)
// r.Get(APIAirportStandardData, app.HandlerAirportDBData)
r.Get(APIAirportCustomData, app.HandlerAirportCustomData)
r.Post(APIAirportAddCustom, app.HandlerAirportAddCustom)
r.Post(APIAirportDeleteCustom, app.HandlerAirportDeleteCustom)
Expand Down
Loading

0 comments on commit f1a0778

Please sign in to comment.