lib/app/place-services.go

67 lines
1.7 KiB
Go
Raw Permalink Normal View History

2021-01-27 22:39:23 +00:00
package app
import (
"fmt"
"code.tnxs.net/taxnexus/lib/api/geo/geo_client/place"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
)
2021-01-27 22:45:50 +00:00
// GetPlaceNameByGeocode is first class retrieval function
func GetPlaceNameByGeocode(key string, principal *User) string {
2021-01-27 22:39:23 +00:00
if key == "" || len(key) == 7 {
return ""
}
p, ok := placeCache.get(key)
if ok {
return p.Name
}
2021-01-27 22:45:50 +00:00
thePlace, err := GetPlaceByGeocode(key, principal)
2021-01-27 22:39:23 +00:00
if err != nil {
return ""
}
return thePlace.Name
}
2021-01-27 22:45:50 +00:00
// GetPlaceHasDistrictTaxesByGeocode is first class retrieval function
func GetPlaceHasDistrictTaxesByGeocode(key string, principal *User) bool {
2021-01-27 22:39:23 +00:00
if key == "" || len(key) == 7 {
return false
}
p, ok := placeCache.get(key)
if ok {
return p.HasDistrictTaxes
}
2021-01-27 22:45:50 +00:00
thePlace, err := GetPlaceByGeocode(key, principal)
2021-01-27 22:39:23 +00:00
if err != nil {
return false
}
return thePlace.HasDistrictTaxes
}
2021-01-27 22:45:50 +00:00
// GetPlaceByGeocode is first class retrieval function
func GetPlaceByGeocode(recordID string, principal *User) (geo_models.Place, error) {
2021-01-28 19:17:39 +00:00
sugar.Debug("app.getPlaceByGeocode: 📥")
2021-01-27 22:39:23 +00:00
if recordID == "" {
2021-01-28 19:17:39 +00:00
return geo_models.Place{}, fmt.Errorf("app.getPlaceByID: 💣 ⛔ key is blank")
2021-01-27 22:39:23 +00:00
}
obj, ok := placeCache.get(recordID)
if ok {
2021-01-28 19:17:39 +00:00
sugar.Debug("app.getPlaceByGeocode: 👍 🎯 📤")
2021-01-27 22:39:23 +00:00
return *obj, nil
}
geoParams := place.NewGetPlacesParamsWithTimeout(getTimeout)
geoParams.Geocode = &recordID
response, err := geoClient.Place.GetPlaces(geoParams, principal.Auth)
if err != nil {
return geo_models.Place{}, err
}
var newObj *geo_models.Place
for _, itm := range response.Payload.Data { // single iteration execution
newObj = itm
}
placeCache.put(recordID, newObj)
2021-01-28 19:17:39 +00:00
sugar.Debug("app.getPlaceByGeocode: 👍 🆕 📤")
2021-01-27 22:39:23 +00:00
return *newObj, nil
}