fix(niki): benefactor update address

This commit is contained in:
Erfan Mohammadi 2024-06-12 14:43:23 +03:30
parent ba905cf334
commit d60e4d605e
8 changed files with 26 additions and 75 deletions

View File

@ -1,7 +1,6 @@
package benefactoraddresshandler package benefactoraddresshandler
import ( import (
"fmt"
"net/http" "net/http"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
@ -24,10 +23,12 @@ import (
func (h Handler) UpdateAddress(c echo.Context) error { func (h Handler) UpdateAddress(c echo.Context) error {
var req param.UpdateAddressRequest var req param.UpdateAddressRequest
if bErr := c.Bind(&req); bErr != nil { if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest) return echo.NewHTTPError(http.StatusBadRequest)
} }
fmt.Println(req)
if bErr := echo.PathParamsBinder(c).Uint("id", &req.ID).BindError(); bErr != nil { if bErr := echo.PathParamsBinder(c).Uint("id", &req.ID).BindError(); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest) return echo.NewHTTPError(http.StatusBadRequest)
} }

View File

@ -219,9 +219,7 @@ const docTemplate = `{
} }
} }
} }
} },
},
"/address/{id}": {
"patch": { "patch": {
"security": [ "security": [
{ {

View File

@ -208,9 +208,7 @@
} }
} }
} }
} },
},
"/address/{id}": {
"patch": { "patch": {
"security": [ "security": [
{ {

View File

@ -641,7 +641,6 @@ paths:
summary: Get a benefactor address summary: Get a benefactor address
tags: tags:
- Address - Address
/address/{id}:
patch: patch:
consumes: consumes:
- application/json - application/json

View File

@ -1,22 +0,0 @@
package querybuilder
import (
"fmt"
"strings"
)
// BuildUpdateQuery constructs a dynamic SQL update query based on the provided fields
func BuildUpdateQuery(table string, fields map[string]interface{}, idField string, idValue interface{}) (string, []interface{}) {
var setClauses []string
var args []interface{}
for field, value := range fields {
setClauses = append(setClauses, fmt.Sprintf("%s = ?", field))
args = append(args, value)
}
query := fmt.Sprintf("UPDATE %s SET %s WHERE %s = ?", table, strings.Join(setClauses, ", "), idField)
args = append(args, idValue)
return query, args
}

View File

@ -5,45 +5,25 @@ import (
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" 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" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (d *DB) UpdateAddress(ctx context.Context, address entity.Address) error { func (d *DB) UpdateAddress(ctx context.Context, address entity.Address) error {
const op = "mysqladdress.UpdateAddress" const op = "mysqladdress.UpdateAddress"
fields := make(map[string]interface{}) provinceID, err := d.getProvinceIDByCityID(ctx, address.CityID)
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 { if err != nil {
return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
return err
}
query := `UPDATE addresses
SET postal_code = ?, address = ?, lat = ?, lon = ?, name = ?, city_id = ?, province_id = ?
WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`
_, uErr := d.conn.Conn().ExecContext(ctx, query, address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.ID, address.BenefactorID)
if err != nil {
return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
WithKind(richerror.KindUnexpected) WithKind(richerror.KindUnexpected)
} }

View File

@ -22,6 +22,7 @@ func (s Service) Update(ctx context.Context, req param.UpdateAddressRequest) err
BenefactorID: req.BenefactorID, BenefactorID: req.BenefactorID,
}) })
if err != nil { if err != nil {
return richerror.New(op).WithErr(err) return richerror.New(op).WithErr(err)
} }

View File

@ -12,16 +12,6 @@ import (
func (v Validator) ValidateUpdateAddress(req param.UpdateAddressRequest) (map[string]string, error) { func (v Validator) ValidateUpdateAddress(req param.UpdateAddressRequest) (map[string]string, error) {
const op = "benefactoraddressvalidator.ValidateUpdateAddress" const op = "benefactoraddressvalidator.ValidateUpdateAddress"
if req.PostalCode == "" && req.Address == "" && req.Name == "" &&
req.Lat == 0 && req.Lon == 0 && req.CityID == 0 {
return map[string]string{"update": "at least one field should be provided"}, richerror.New(op).
WithMessage(errmsg.ErrorMsgInvalidInput).
WithKind(richerror.KindInvalid).
WithMeta(map[string]interface{}{"req": req}).
WithErr(errors.New("at least one field should be provided"))
}
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Required, validation.Field(&req.BenefactorID, validation.Required,
validation.By(v.doesBenefactorExist)), validation.By(v.doesBenefactorExist)),
@ -29,8 +19,14 @@ func (v Validator) ValidateUpdateAddress(req param.UpdateAddressRequest) (map[st
validation.Field(&req.ID, validation.Required, validation.Field(&req.ID, validation.Required,
validation.By(v.doesAddressExist(req.BenefactorID))), validation.By(v.doesAddressExist(req.BenefactorID))),
validation.Field(&req.CityID, validation.Field(&req.Address, validation.Required),
validation.When(req.CityID != 0, validation.By(v.doesCityExist))),
validation.Field(&req.Name, validation.Required),
validation.Field(&req.PostalCode, validation.Required),
validation.Field(&req.CityID, validation.Required,
validation.By(v.doesCityExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)