Add region (state) to address
This commit is contained in:
parent
0ff493cd53
commit
6aa7fa60b4
6 changed files with 97 additions and 9 deletions
|
|
@ -105,6 +105,15 @@ var Addresses = Table[
|
||||||
Generated: false,
|
Generated: false,
|
||||||
AutoIncr: false,
|
AutoIncr: false,
|
||||||
},
|
},
|
||||||
|
Region: column{
|
||||||
|
Name: "region",
|
||||||
|
DBType: "text",
|
||||||
|
Default: "",
|
||||||
|
Comment: "",
|
||||||
|
Nullable: false,
|
||||||
|
Generated: false,
|
||||||
|
AutoIncr: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Indexes: addressIndexes{
|
Indexes: addressIndexes{
|
||||||
AddressPkey: index{
|
AddressPkey: index{
|
||||||
|
|
@ -207,11 +216,12 @@ type addressColumns struct {
|
||||||
PostalCode column
|
PostalCode column
|
||||||
Street column
|
Street column
|
||||||
Unit column
|
Unit column
|
||||||
|
Region column
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c addressColumns) AsSlice() []column {
|
func (c addressColumns) AsSlice() []column {
|
||||||
return []column{
|
return []column{
|
||||||
c.Country, c.Created, c.Geom, c.H3cell, c.ID, c.Locality, c.Number, c.PostalCode, c.Street, c.Unit,
|
c.Country, c.Created, c.Geom, c.H3cell, c.ID, c.Locality, c.Number, c.PostalCode, c.Street, c.Unit, c.Region,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ type AddressTemplate struct {
|
||||||
PostalCode func() string
|
PostalCode func() string
|
||||||
Street func() string
|
Street func() string
|
||||||
Unit func() string
|
Unit func() string
|
||||||
|
Region func() string
|
||||||
|
|
||||||
r addressR
|
r addressR
|
||||||
f *Factory
|
f *Factory
|
||||||
|
|
@ -160,6 +161,10 @@ func (o AddressTemplate) BuildSetter() *models.AddressSetter {
|
||||||
val := o.Unit()
|
val := o.Unit()
|
||||||
m.Unit = omit.From(val)
|
m.Unit = omit.From(val)
|
||||||
}
|
}
|
||||||
|
if o.Region != nil {
|
||||||
|
val := o.Region()
|
||||||
|
m.Region = omit.From(val)
|
||||||
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
@ -212,6 +217,9 @@ func (o AddressTemplate) Build() *models.Address {
|
||||||
if o.Unit != nil {
|
if o.Unit != nil {
|
||||||
m.Unit = o.Unit()
|
m.Unit = o.Unit()
|
||||||
}
|
}
|
||||||
|
if o.Region != nil {
|
||||||
|
m.Region = o.Region()
|
||||||
|
}
|
||||||
|
|
||||||
o.setModelRels(m)
|
o.setModelRels(m)
|
||||||
|
|
||||||
|
|
@ -268,6 +276,10 @@ func ensureCreatableAddress(m *models.AddressSetter) {
|
||||||
val := random_string(nil)
|
val := random_string(nil)
|
||||||
m.Unit = omit.From(val)
|
m.Unit = omit.From(val)
|
||||||
}
|
}
|
||||||
|
if !(m.Region.IsValue()) {
|
||||||
|
val := random_string(nil)
|
||||||
|
m.Region = omit.From(val)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// insertOptRels creates and inserts any optional the relationships on *models.Address
|
// insertOptRels creates and inserts any optional the relationships on *models.Address
|
||||||
|
|
@ -437,6 +449,7 @@ func (m addressMods) RandomizeAllColumns(f *faker.Faker) AddressMod {
|
||||||
AddressMods.RandomPostalCode(f),
|
AddressMods.RandomPostalCode(f),
|
||||||
AddressMods.RandomStreet(f),
|
AddressMods.RandomStreet(f),
|
||||||
AddressMods.RandomUnit(f),
|
AddressMods.RandomUnit(f),
|
||||||
|
AddressMods.RandomRegion(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -750,6 +763,37 @@ func (m addressMods) RandomUnit(f *faker.Faker) AddressMod {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the model columns to this value
|
||||||
|
func (m addressMods) Region(val string) AddressMod {
|
||||||
|
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
|
||||||
|
o.Region = func() string { return val }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the Column from the function
|
||||||
|
func (m addressMods) RegionFunc(f func() string) AddressMod {
|
||||||
|
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
|
||||||
|
o.Region = f
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear any values for the column
|
||||||
|
func (m addressMods) UnsetRegion() AddressMod {
|
||||||
|
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
|
||||||
|
o.Region = nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generates a random value for the column using the given faker
|
||||||
|
// if faker is nil, a default faker is used
|
||||||
|
func (m addressMods) RandomRegion(f *faker.Faker) AddressMod {
|
||||||
|
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
|
||||||
|
o.Region = func() string {
|
||||||
|
return random_string(f)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (m addressMods) WithParentsCascading() AddressMod {
|
func (m addressMods) WithParentsCascading() AddressMod {
|
||||||
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
|
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
|
||||||
if isDone, _ := addressWithParentsCascadingCtx.Value(ctx); isDone {
|
if isDone, _ := addressWithParentsCascadingCtx.Value(ctx); isDone {
|
||||||
|
|
|
||||||
|
|
@ -146,6 +146,7 @@ func (f *Factory) FromExistingAddress(m *models.Address) *AddressTemplate {
|
||||||
o.PostalCode = func() string { return m.PostalCode }
|
o.PostalCode = func() string { return m.PostalCode }
|
||||||
o.Street = func() string { return m.Street }
|
o.Street = func() string { return m.Street }
|
||||||
o.Unit = func() string { return m.Unit }
|
o.Unit = func() string { return m.Unit }
|
||||||
|
o.Region = func() string { return m.Region }
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if len(m.R.Mailers) > 0 {
|
if len(m.R.Mailers) > 0 {
|
||||||
|
|
|
||||||
6
db/migrations/00078_address_region.sql
Normal file
6
db/migrations/00078_address_region.sql
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
-- +goose Up
|
||||||
|
ALTER TABLE address ADD COLUMN region TEXT;
|
||||||
|
UPDATE address SET region = 'California';
|
||||||
|
ALTER TABLE address ALTER COLUMN region SET NOT NULL;
|
||||||
|
-- +goose Down
|
||||||
|
ALTER TABLE address DROP COLUMN region;
|
||||||
|
|
@ -35,6 +35,7 @@ type Address struct {
|
||||||
PostalCode string `db:"postal_code" `
|
PostalCode string `db:"postal_code" `
|
||||||
Street string `db:"street" `
|
Street string `db:"street" `
|
||||||
Unit string `db:"unit" `
|
Unit string `db:"unit" `
|
||||||
|
Region string `db:"region" `
|
||||||
|
|
||||||
R addressR `db:"-" `
|
R addressR `db:"-" `
|
||||||
|
|
||||||
|
|
@ -61,7 +62,7 @@ type addressR struct {
|
||||||
func buildAddressColumns(alias string) addressColumns {
|
func buildAddressColumns(alias string) addressColumns {
|
||||||
return addressColumns{
|
return addressColumns{
|
||||||
ColumnsExpr: expr.NewColumnsExpr(
|
ColumnsExpr: expr.NewColumnsExpr(
|
||||||
"country", "created", "geom", "h3cell", "id", "locality", "number_", "postal_code", "street", "unit",
|
"country", "created", "geom", "h3cell", "id", "locality", "number_", "postal_code", "street", "unit", "region",
|
||||||
).WithParent("address"),
|
).WithParent("address"),
|
||||||
tableAlias: alias,
|
tableAlias: alias,
|
||||||
Country: psql.Quote(alias, "country"),
|
Country: psql.Quote(alias, "country"),
|
||||||
|
|
@ -74,6 +75,7 @@ func buildAddressColumns(alias string) addressColumns {
|
||||||
PostalCode: psql.Quote(alias, "postal_code"),
|
PostalCode: psql.Quote(alias, "postal_code"),
|
||||||
Street: psql.Quote(alias, "street"),
|
Street: psql.Quote(alias, "street"),
|
||||||
Unit: psql.Quote(alias, "unit"),
|
Unit: psql.Quote(alias, "unit"),
|
||||||
|
Region: psql.Quote(alias, "region"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,6 +92,7 @@ type addressColumns struct {
|
||||||
PostalCode psql.Expression
|
PostalCode psql.Expression
|
||||||
Street psql.Expression
|
Street psql.Expression
|
||||||
Unit psql.Expression
|
Unit psql.Expression
|
||||||
|
Region psql.Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c addressColumns) Alias() string {
|
func (c addressColumns) Alias() string {
|
||||||
|
|
@ -114,10 +117,11 @@ type AddressSetter struct {
|
||||||
PostalCode omit.Val[string] `db:"postal_code" `
|
PostalCode omit.Val[string] `db:"postal_code" `
|
||||||
Street omit.Val[string] `db:"street" `
|
Street omit.Val[string] `db:"street" `
|
||||||
Unit omit.Val[string] `db:"unit" `
|
Unit omit.Val[string] `db:"unit" `
|
||||||
|
Region omit.Val[string] `db:"region" `
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AddressSetter) SetColumns() []string {
|
func (s AddressSetter) SetColumns() []string {
|
||||||
vals := make([]string, 0, 10)
|
vals := make([]string, 0, 11)
|
||||||
if s.Country.IsValue() {
|
if s.Country.IsValue() {
|
||||||
vals = append(vals, "country")
|
vals = append(vals, "country")
|
||||||
}
|
}
|
||||||
|
|
@ -148,6 +152,9 @@ func (s AddressSetter) SetColumns() []string {
|
||||||
if s.Unit.IsValue() {
|
if s.Unit.IsValue() {
|
||||||
vals = append(vals, "unit")
|
vals = append(vals, "unit")
|
||||||
}
|
}
|
||||||
|
if s.Region.IsValue() {
|
||||||
|
vals = append(vals, "region")
|
||||||
|
}
|
||||||
return vals
|
return vals
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,6 +189,9 @@ func (s AddressSetter) Overwrite(t *Address) {
|
||||||
if s.Unit.IsValue() {
|
if s.Unit.IsValue() {
|
||||||
t.Unit = s.Unit.MustGet()
|
t.Unit = s.Unit.MustGet()
|
||||||
}
|
}
|
||||||
|
if s.Region.IsValue() {
|
||||||
|
t.Region = s.Region.MustGet()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AddressSetter) Apply(q *dialect.InsertQuery) {
|
func (s *AddressSetter) Apply(q *dialect.InsertQuery) {
|
||||||
|
|
@ -190,7 +200,7 @@ func (s *AddressSetter) Apply(q *dialect.InsertQuery) {
|
||||||
})
|
})
|
||||||
|
|
||||||
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||||||
vals := make([]bob.Expression, 10)
|
vals := make([]bob.Expression, 11)
|
||||||
if s.Country.IsValue() {
|
if s.Country.IsValue() {
|
||||||
vals[0] = psql.Arg(s.Country.MustGet())
|
vals[0] = psql.Arg(s.Country.MustGet())
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -251,6 +261,12 @@ func (s *AddressSetter) Apply(q *dialect.InsertQuery) {
|
||||||
vals[9] = psql.Raw("DEFAULT")
|
vals[9] = psql.Raw("DEFAULT")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.Region.IsValue() {
|
||||||
|
vals[10] = psql.Arg(s.Region.MustGet())
|
||||||
|
} else {
|
||||||
|
vals[10] = psql.Raw("DEFAULT")
|
||||||
|
}
|
||||||
|
|
||||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
@ -260,7 +276,7 @@ func (s AddressSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AddressSetter) Expressions(prefix ...string) []bob.Expression {
|
func (s AddressSetter) Expressions(prefix ...string) []bob.Expression {
|
||||||
exprs := make([]bob.Expression, 0, 10)
|
exprs := make([]bob.Expression, 0, 11)
|
||||||
|
|
||||||
if s.Country.IsValue() {
|
if s.Country.IsValue() {
|
||||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||||
|
|
@ -332,6 +348,13 @@ func (s AddressSetter) Expressions(prefix ...string) []bob.Expression {
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.Region.IsValue() {
|
||||||
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||||
|
psql.Quote(append(prefix, "region")...),
|
||||||
|
psql.Arg(s.Region),
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
return exprs
|
return exprs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -831,6 +854,7 @@ type addressWhere[Q psql.Filterable] struct {
|
||||||
PostalCode psql.WhereMod[Q, string]
|
PostalCode psql.WhereMod[Q, string]
|
||||||
Street psql.WhereMod[Q, string]
|
Street psql.WhereMod[Q, string]
|
||||||
Unit psql.WhereMod[Q, string]
|
Unit psql.WhereMod[Q, string]
|
||||||
|
Region psql.WhereMod[Q, string]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addressWhere[Q]) AliasedAs(alias string) addressWhere[Q] {
|
func (addressWhere[Q]) AliasedAs(alias string) addressWhere[Q] {
|
||||||
|
|
@ -849,6 +873,7 @@ func buildAddressWhere[Q psql.Filterable](cols addressColumns) addressWhere[Q] {
|
||||||
PostalCode: psql.Where[Q, string](cols.PostalCode),
|
PostalCode: psql.Where[Q, string](cols.PostalCode),
|
||||||
Street: psql.Where[Q, string](cols.Street),
|
Street: psql.Where[Q, string](cols.Street),
|
||||||
Unit: psql.Where[Q, string](cols.Unit),
|
Unit: psql.Where[Q, string](cols.Unit),
|
||||||
|
Region: psql.Where[Q, string](cols.Region),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type address struct {
|
type address struct {
|
||||||
Number int32 `db:"number_"`
|
|
||||||
Street string `db:"street"`
|
|
||||||
Locality string `db:"locality"`
|
|
||||||
PostalCode string `db:"postal_code"`
|
|
||||||
Country string `db:"country"`
|
Country string `db:"country"`
|
||||||
|
Locality string `db:"locality"`
|
||||||
|
Number int32 `db:"number_"`
|
||||||
|
PostalCode string `db:"postal_code"`
|
||||||
|
Street string `db:"street"`
|
||||||
|
Region string `db:"postal_code"`
|
||||||
}
|
}
|
||||||
type contentMailer struct {
|
type contentMailer struct {
|
||||||
Address address
|
Address address
|
||||||
|
|
@ -48,6 +49,7 @@ func getMailer(ctx context.Context, r *http.Request) (*html.Response[contentMail
|
||||||
"address.number_",
|
"address.number_",
|
||||||
"address.street",
|
"address.street",
|
||||||
"address.locality",
|
"address.locality",
|
||||||
|
"address.region",
|
||||||
"address.postal_code",
|
"address.postal_code",
|
||||||
"address.country",
|
"address.country",
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue