Skip to content

Commit

Permalink
added airports page with standard and custom airports
Browse files Browse the repository at this point in the history
  • Loading branch information
vsimakhin committed Feb 15, 2025
1 parent 3155b79 commit ac01234
Show file tree
Hide file tree
Showing 39 changed files with 786 additions and 2,616 deletions.
311 changes: 152 additions & 159 deletions app/handlers_airport.go

Large diffs are not rendered by default.

63 changes: 0 additions & 63 deletions app/handlers_aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,11 @@ package main

import (
"embed"
"fmt"
"io/fs"
"net/http"
"strings"
)

//go:embed static
var staticFS embed.FS

// other auxiliary handlers
func (app *application) HandlerStatic() http.Handler {
return http.FileServer(http.FS(staticFS))
}

func (app *application) HandlerFavicon(w http.ResponseWriter, r *http.Request) {
data, err := fs.ReadFile(staticFS, "static/favicon.ico")
if err != nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
w.Header().Set("Cache-Control", "private, max-age=3600, must-revalidate")
w.Write(data)
}

func (app *application) HandlerNotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)

if err := app.renderTemplate(w, r, "notfound", nil); err != nil {
app.errorLog.Println(err)
}
}

func (app *application) HandlerNotAllowed(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)

if err := app.renderTemplate(w, r, "notallowed", nil); err != nil {
app.errorLog.Println(err)
}
}

func (app *application) HandlerPreferences(w http.ResponseWriter, r *http.Request) {
settings, err := app.db.GetSettings()
if err != nil {
app.errorLog.Println(fmt.Errorf("cannot get settings - %s", err))
http.Error(w, "cannot get settings", http.StatusInternalServerError)
return
}

data := map[string]interface{}{
"hide_stats_se": settings.HideStatsFields.SE,
"hide_stats_me": settings.HideStatsFields.ME,
"hide_stats_mcc": settings.HideStatsFields.MCC,
"hide_stats_night": settings.HideStatsFields.Night,
"hide_stats_ifr": settings.HideStatsFields.IFR,
"hide_stats_pic": settings.HideStatsFields.PIC,
"hide_stats_copilot": settings.HideStatsFields.CoPilot,
"hide_stats_dual": settings.HideStatsFields.Dual,
"hide_stats_instructor": settings.HideStatsFields.Instructor,
"hide_stats_sim": settings.HideStatsFields.Sim,
"hide_stats_crosscountry": settings.HideStatsFields.CrossCountry,
"hide_stats_landings": settings.HideStatsFields.Landings,
"hide_stats_distance": settings.HideStatsFields.Distance,
"signature_image": settings.SignatureImage,
}

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

//go:embed ui/dist/*
var ui embed.FS

Expand Down
62 changes: 9 additions & 53 deletions app/handlers_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,71 +75,27 @@ func (app *application) HandlerApiSettingsSignature(w http.ResponseWriter, r *ht
app.writeOkResponse(w, "Signature updated")
}

////////////////////////////////////
// for review

func (app *application) HandlerSettingsAirportDB(w http.ResponseWriter, r *http.Request) {
records, err := app.db.GetAirportCount()
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

data := make(map[string]interface{})
data["records"] = records
data["activePage"] = "settings"
data["activeSubPage"] = "airports"
if err := app.renderTemplate(w, r, "settings-airportdb", &templateData{Data: data}); err != nil {
app.errorLog.Println(err)
}
}

// HandlerSettingsAirportDBSave serves the POST request for airportdb settings update
func (app *application) HandlerSettingsAirportDBSave(w http.ResponseWriter, r *http.Request) {

cursettings, err := app.db.GetSettings()
func (app *application) HandlerApiSettingsAirports(w http.ResponseWriter, r *http.Request) {
oldsettings, err := app.db.GetSettings()
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
app.handleError(w, err)
return
}

var settings models.Settings
var response models.JSONResponse

err = json.NewDecoder(r.Body).Decode(&settings)
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
app.handleError(w, err)
return
}

cursettings.AirportDBSource = settings.AirportDBSource
cursettings.NoICAOFilter = settings.NoICAOFilter

err = app.db.UpdateSettings(cursettings)
if err != nil {
app.errorLog.Println(err)
response.OK = false
response.Message = err.Error()
} else {
response.OK = true
response.Message = "Settings have been updated"
}

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

// HandlerSettingsAircraftClasses is a handler for aircraft groups/classes
func (app *application) HandlerSettingsAircraftClasses(w http.ResponseWriter, r *http.Request) {

settings, err := app.db.GetSettings()
oldsettings.AirportDBSource = settings.AirportDBSource
oldsettings.NoICAOFilter = settings.NoICAOFilter
err = app.db.UpdateSettings(oldsettings)
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
app.handleError(w, err)
return
}

app.writeJSON(w, http.StatusOK, settings.AircraftClasses)
app.writeOkResponse(w, "Airports DB Settings updated")
}
31 changes: 9 additions & 22 deletions app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ func (app *application) routes() *chi.Mux {
r.Get("/list", app.HandlerApiSettingsList)
r.Put("/general", app.HandlerApiSettingsUpdate)
r.Put("/signature", app.HandlerApiSettingsSignature)
r.Put("/airports", app.HandlerApiSettingsAirports)
})

// airports
r.Route("/airport", func(r chi.Router) {
r.With(middleware.Compress(5)).Get("/standard-list", app.HandlerApiAirportList)
r.Get("/{id}", app.HandlerAirportByID)
r.With(middleware.Compress(5)).Get("/standard-list", app.HandlerApiStandardAirportList)
r.With(middleware.Compress(5)).Get("/custom-list", app.HandlerApiCustomAirportList)
r.With(middleware.Compress(5)).Get("/list", app.HandlerApiAirportList)
r.Post("/custom", app.HandlerApiAirportCustomNew)
r.Put("/custom", app.HandlerApiAirportCustomUpdate)
r.Delete("/custom", app.HandlerApiAirportCustomDelete)
r.Get("/{id}", app.HandlerApiAirportByID)
r.Post("/update-db", app.HandlerApiAirportDBUpdate)
})

// export
Expand Down Expand Up @@ -198,23 +205,6 @@ func (app *application) routes() *chi.Mux {
r.Post(APIImportCreateBackup, app.HandlerImportCreateBackup)
r.Post(APIImportRun, app.HandlerImportRun)

// airports
r.Get(APIAirportID, app.HandlerAirportByID)
r.Get(APIAirportUpdate, app.HandlerAirportUpdate)
// r.Get(APIAirportStandardData, app.HandlerAirportDBData)
r.Get(APIAirportCustomData, app.HandlerAirportCustomData)
r.Post(APIAirportAddCustom, app.HandlerAirportAddCustom)
r.Post(APIAirportDeleteCustom, app.HandlerAirportDeleteCustom)

// aircrafts
r.Get(APISettingsAircraftClasses, app.HandlerSettingsAircraftClasses)
r.Get(APIAircrafts, app.HandlerAircrafts)
r.Get(APIAircraftsFilter, app.HandlerAircrafts)

// settings
r.Get(APISettingsAirportDB, app.HandlerSettingsAirportDB)
r.Post(APISettingsAirportDB, app.HandlerSettingsAirportDBSave)

// stats
r.Get(APIStatsTotals, app.HandlerStatsTotals)
r.Get(APIStatsTotalsByType, app.HandlerStatsTotalsByType)
Expand All @@ -228,9 +218,6 @@ func (app *application) routes() *chi.Mux {
r.Get(APIStatsTotalsByTypePage, app.HandlerStatsTotalsByTypePage)
r.Get(APIStatsTotalsByClassPage, app.HandlerStatsTotalsByClassPage)
r.Get(APIStatsLimitsPage, app.HandlerStatsLimitsPage)

// api & parameters
r.Get(APIPreferences, app.HandlerPreferences)
})

r.Handle("/*", middleware.Compress(5)(app.HandlerUI()))
Expand Down
19 changes: 0 additions & 19 deletions app/static/css/dark.css

This file was deleted.

Loading

0 comments on commit ac01234

Please sign in to comment.