nidus-sync/platform/geocode/address.go

47 lines
1.6 KiB
Go
Raw Normal View History

2026-04-10 22:32:40 +00:00
package geocode
import (
"context"
"fmt"
"github.com/Gleipnir-Technology/nidus-sync/db"
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
platformaddress "github.com/Gleipnir-Technology/nidus-sync/platform/address"
2026-04-10 22:32:40 +00:00
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
//"github.com/rs/zerolog/log"
2026-04-10 22:32:40 +00:00
)
// Ensure the provided address exists. If it doesn't add it to the database.
func EnsureAddress(ctx context.Context, txn db.Ex, a types.Address) (types.Address, error) {
existing, err := querypublic.AddressFromGID(ctx, txn, a.GID)
2026-04-10 22:32:40 +00:00
if err != nil {
return types.Address{}, fmt.Errorf("query address from gid: %w", err)
}
if existing != nil {
return types.AddressFromModel(*existing), nil
}
addr, err := platformaddress.InsertAddress(ctx, txn, a)
2026-04-10 22:32:40 +00:00
if err != nil {
return types.Address{}, fmt.Errorf("insert address: %w", err)
2026-04-10 22:32:40 +00:00
}
return addr, nil
2026-04-10 22:32:40 +00:00
}
func ensureAddressFromFeature(ctx context.Context, txn db.Ex, feature stadia.GeocodeFeature) (types.Address, error) {
var result types.Address
if feature.Geometry.Type != "Point" {
return result, fmt.Errorf("Can't hanlde stadia geometry %s", feature.Geometry.Type)
2026-04-12 19:39:50 +00:00
}
existing, err := querypublic.AddressFromGID(ctx, txn, feature.Properties.GID)
if err != nil {
return types.Address{}, fmt.Errorf("query address from gid: %w", err)
}
if existing != nil {
return types.AddressFromModel(*existing), nil
2026-04-12 19:39:50 +00:00
}
return platformaddress.InsertAddressFeature(ctx, txn, feature)
2026-04-10 22:32:40 +00:00
}