2024-06-06 17:54:56 +00:00
|
|
|
package mysqladdress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
2024-07-30 11:05:41 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
2024-06-06 17:54:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (d *DB) UpdateAddress(ctx context.Context, address entity.Address) error {
|
|
|
|
const op = "mysqladdress.UpdateAddress"
|
|
|
|
|
2024-06-12 11:13:23 +00:00
|
|
|
provinceID, err := d.getProvinceIDByCityID(ctx, address.CityID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-06-06 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 11:13:23 +00:00
|
|
|
query := `UPDATE addresses
|
|
|
|
SET postal_code = ?, address = ?, lat = ?, lon = ?, name = ?, city_id = ?, province_id = ?
|
|
|
|
WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`
|
2024-07-30 11:05:41 +00:00
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressUpdate, query)
|
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, uErr := stmt.ExecContext(ctx, address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.ID, address.BenefactorID)
|
2024-06-06 17:54:56 +00:00
|
|
|
if err != nil {
|
2024-06-12 11:13:23 +00:00
|
|
|
return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
|
2024-06-06 17:54:56 +00:00
|
|
|
WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|