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

View File

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

View File

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

View File

@ -641,7 +641,6 @@ paths:
summary: Get a benefactor address
tags:
- Address
/address/{id}:
patch:
consumes:
- 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"
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...)
provinceID, err := d.getProvinceIDByCityID(ctx, address.CityID)
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)
}

View File

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

View File

@ -12,16 +12,6 @@ import (
func (v Validator) ValidateUpdateAddress(req param.UpdateAddressRequest) (map[string]string, error) {
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,
validation.Field(&req.BenefactorID, validation.Required,
validation.By(v.doesBenefactorExist)),
@ -29,8 +19,14 @@ func (v Validator) ValidateUpdateAddress(req param.UpdateAddressRequest) (map[st
validation.Field(&req.ID, validation.Required,
validation.By(v.doesAddressExist(req.BenefactorID))),
validation.Field(&req.CityID,
validation.When(req.CityID != 0, validation.By(v.doesCityExist))),
validation.Field(&req.Address, validation.Required),
validation.Field(&req.Name, validation.Required),
validation.Field(&req.PostalCode, validation.Required),
validation.Field(&req.CityID, validation.Required,
validation.By(v.doesCityExist)),
); err != nil {
fieldErrors := make(map[string]string)