-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from rohitkumbhar/richer-airport-info
Showing full name and timezone info for airports
- Loading branch information
Showing
17 changed files
with
279 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package hooks | ||
|
||
import ( | ||
"backend/trips" | ||
"encoding/json" | ||
"github.com/pocketbase/pocketbase/core" | ||
"github.com/ringsaturn/tzf" | ||
"strconv" | ||
) | ||
|
||
func AddTimezoneToDestinations(e *core.RecordEvent, finder tzf.F) error { | ||
|
||
record := e.Record | ||
destinations := record.GetString("destinations") | ||
|
||
var payload []trips.Destination | ||
err := json.Unmarshal([]byte((destinations)), &payload) | ||
|
||
var updatedDestinations = make([]trips.Destination, len(payload)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
for i, destination := range payload { | ||
if destination.Latitude != "" && destination.Longitude != "" { | ||
|
||
timezone := destination.TimeZone | ||
if timezone == "" { | ||
lat, _ := strconv.ParseFloat(destination.Latitude, 64) | ||
long, _ := strconv.ParseFloat(destination.Longitude, 64) | ||
timezone = finder.GetTimezoneName(long, lat) | ||
} | ||
|
||
updatedDestinations[i] = trips.Destination{ | ||
Id: destination.Id, | ||
Name: destination.Name, | ||
StateName: destination.StateName, | ||
CountryName: destination.CountryName, | ||
Latitude: destination.Latitude, | ||
Longitude: destination.Longitude, | ||
TimeZone: timezone, | ||
} | ||
} | ||
} | ||
|
||
record.Set("destinations", updatedDestinations) | ||
return e.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
backend/migrations/1738518698_add_timezone_field_to_airports.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/pocketbase/pocketbase/core" | ||
m "github.com/pocketbase/pocketbase/migrations" | ||
) | ||
|
||
func init() { | ||
m.Register(func(app core.App) error { | ||
|
||
airports, err := app.FindCollectionByNameOrId("airports") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
timezone := airports.Fields.GetByName("timezone") | ||
if timezone == nil { | ||
airports.Fields.Add( | ||
&core.TextField{ | ||
Name: "timezone", | ||
}) | ||
} | ||
|
||
return app.Save(airports) | ||
}, func(app core.App) error { | ||
|
||
airports, err := app.FindCollectionByNameOrId("airports") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
timezone := airports.Fields.GetByName("timezone") | ||
if timezone != nil { | ||
airports.Fields.RemoveByName("timezone") | ||
} | ||
|
||
return nil | ||
}) | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/migrations/1738519299_populate_timezone_data_for_airports.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/pocketbase/pocketbase/core" | ||
m "github.com/pocketbase/pocketbase/migrations" | ||
"github.com/ringsaturn/tzf" | ||
"log" | ||
"strconv" | ||
) | ||
|
||
func init() { | ||
m.Register(func(app core.App) error { | ||
finder, err := tzf.NewDefaultFinder() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
records, err := app.FindAllRecords("airports") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, airport := range records { | ||
latitude := airport.GetString("latitude") | ||
longitude := airport.GetString("longitude") | ||
|
||
if latitude != "" && longitude != "" { | ||
lat, _ := strconv.ParseFloat(latitude, 64) | ||
long, _ := strconv.ParseFloat(longitude, 64) | ||
timezone := finder.GetTimezoneName(long, lat) | ||
airport.Set("timezone", timezone) | ||
err = app.Save(airport) | ||
if err != nil { | ||
log.Printf("Error saving airport to %s: %s", timezone, err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
}, func(app core.App) error { | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.