From d60e4d605eb99d3f0d6f29cdd8ebfe1260808179 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Wed, 12 Jun 2024 14:43:23 +0330 Subject: [PATCH] fix(niki): benefactor update address --- .../http_server/benefactor/address/update.go | 5 ++- docs/docs.go | 4 +- docs/swagger.json | 4 +- docs/swagger.yaml | 1 - pkg/query_builder/update.go | 22 ---------- repository/mysql/address/update.go | 44 +++++-------------- service/benefactor/address/update.go | 1 + validator/benefactor/address/update.go | 20 ++++----- 8 files changed, 26 insertions(+), 75 deletions(-) delete mode 100644 pkg/query_builder/update.go diff --git a/delivery/http_server/benefactor/address/update.go b/delivery/http_server/benefactor/address/update.go index a83e917..3d7b338 100644 --- a/delivery/http_server/benefactor/address/update.go +++ b/delivery/http_server/benefactor/address/update.go @@ -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) } diff --git a/docs/docs.go b/docs/docs.go index 97c94cf..458ae7e 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -219,9 +219,7 @@ const docTemplate = `{ } } } - } - }, - "/address/{id}": { + }, "patch": { "security": [ { diff --git a/docs/swagger.json b/docs/swagger.json index 0455951..3895348 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -208,9 +208,7 @@ } } } - } - }, - "/address/{id}": { + }, "patch": { "security": [ { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 12c085c..dd24963 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -641,7 +641,6 @@ paths: summary: Get a benefactor address tags: - Address - /address/{id}: patch: consumes: - application/json diff --git a/pkg/query_builder/update.go b/pkg/query_builder/update.go deleted file mode 100644 index bfaef1d..0000000 --- a/pkg/query_builder/update.go +++ /dev/null @@ -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 -} diff --git a/repository/mysql/address/update.go b/repository/mysql/address/update.go index 8d406b8..ee249f4 100644 --- a/repository/mysql/address/update.go +++ b/repository/mysql/address/update.go @@ -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) } diff --git a/service/benefactor/address/update.go b/service/benefactor/address/update.go index 1790df5..51dc71c 100644 --- a/service/benefactor/address/update.go +++ b/service/benefactor/address/update.go @@ -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) } diff --git a/validator/benefactor/address/update.go b/validator/benefactor/address/update.go index 1f45152..aacdeb9 100644 --- a/validator/benefactor/address/update.go +++ b/validator/benefactor/address/update.go @@ -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)