forked from ebhomengo/niki
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
|
package mysqladdress
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||
|
querybuilder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder"
|
||
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||
|
)
|
||
|
|
||
|
func (d *DB) UpdateAddress(ctx context.Context, address entity.Address) error {
|
||
|
const op = "mysqladdress.UpdateAddress"
|
||
|
|
||
|
fields := make(map[string]interface{})
|
||
|
|
||
|
if address.PostalCode != "" {
|
||
|
fields["postal_code"] = address.PostalCode
|
||
|
}
|
||
|
if address.Address != "" {
|
||
|
fields["address"] = address.Address
|
||
|
}
|
||
|
if address.Name != "" {
|
||
|
fields["name"] = address.Name
|
||
|
}
|
||
|
if address.Lat != 0 {
|
||
|
fields["lat"] = address.Lat
|
||
|
}
|
||
|
if address.Lon != 0 {
|
||
|
fields["lon"] = address.Lon
|
||
|
}
|
||
|
if address.CityID != 0 {
|
||
|
provinceID, err := d.getProvinceIDByCityID(ctx, address.CityID)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
fields["province_id"] = provinceID
|
||
|
fields["city_id"] = address.CityID
|
||
|
}
|
||
|
|
||
|
query, values := querybuilder.BuildUpdateQuery("addresses", fields, "id", address.ID)
|
||
|
query += " and benefactor_id = ?"
|
||
|
values = append(values, address.BenefactorID)
|
||
|
_, err := d.conn.Conn().ExecContext(ctx, query, values...)
|
||
|
if err != nil {
|
||
|
return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
|
||
|
WithKind(richerror.KindUnexpected)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|