Add simple client API, add airport type to API response
This commit is contained in:
57
types/airport_type.go
Normal file
57
types/airport_type.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
alphafoxtrot "github.com/grumpypixel/go-airport-finder"
|
||||
)
|
||||
|
||||
type Airport struct {
|
||||
Name string
|
||||
Code string
|
||||
Distance float64
|
||||
Type string
|
||||
|
||||
data *alphafoxtrot.Airport
|
||||
}
|
||||
|
||||
func NewAirport(airport *alphafoxtrot.Airport) *Airport {
|
||||
code := strings.ToLower(airport.Country.ISOCode + airport.IATACode)
|
||||
|
||||
a := &Airport{
|
||||
Name: airport.Name,
|
||||
Code: code,
|
||||
Type: airport.Type,
|
||||
data: airport,
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Airport) String() string {
|
||||
return a.Name
|
||||
}
|
||||
|
||||
func UniqAirports(r []*Airport) []*Airport {
|
||||
seen := make(map[string]struct{})
|
||||
j := 0
|
||||
for _, v := range r {
|
||||
if _, ok := seen[v.Code]; ok {
|
||||
continue
|
||||
}
|
||||
seen[v.Code] = struct{}{}
|
||||
r[j] = v
|
||||
j++
|
||||
}
|
||||
return r[:j]
|
||||
}
|
||||
|
||||
func SortAirports(r []*Airport) {
|
||||
sort.Slice(r, func(i, j int) bool {
|
||||
if r[i].data.Type == r[j].data.Type {
|
||||
return r[i].Distance < r[j].Distance
|
||||
}
|
||||
return r[i].data.Type < r[j].data.Type
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user