diff --git a/docs/docs.go b/docs/docs.go index 8e70423..12ecbb6 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -711,10 +711,6 @@ const docTemplate = `{ "postal_code": { "type": "string", "example": "1234567890" - }, - "province_id": { - "type": "integer", - "example": 1 } } }, diff --git a/docs/swagger.json b/docs/swagger.json index 31dc8f9..3de7854 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -700,10 +700,6 @@ "postal_code": { "type": "string", "example": "1234567890" - }, - "province_id": { - "type": "integer", - "example": 1 } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d90fec9..51cc7f5 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -19,9 +19,6 @@ definitions: postal_code: example: "1234567890" type: string - province_id: - example: 1 - type: integer type: object addressparam.BenefactorAddAddressResponse: properties: diff --git a/param/benefactor/address/add.go b/param/benefactor/address/add.go index 099376d..37484a9 100644 --- a/param/benefactor/address/add.go +++ b/param/benefactor/address/add.go @@ -8,7 +8,7 @@ type BenefactorAddAddressRequest struct { Lat float64 `json:"lat" example:"22.23"` Lon float64 `json:"lon" example:"22.22"` CityID uint `json:"city_id" example:"1"` - ProvinceID uint `json:"province_id" example:"1"` + ProvinceID uint `json:"-"` BenefactorID uint `json:"benefactor_id" example:"1"` } diff --git a/repository/mysql/address/create.go b/repository/mysql/address/create.go index 6a1557a..036a5bb 100644 --- a/repository/mysql/address/create.go +++ b/repository/mysql/address/create.go @@ -12,6 +12,14 @@ import ( func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) { const op = "mysqlbenefactor.createBenefactorAddress" + // Get Province ID by City ID + var provinceID uint + pErr := d.conn.Conn().QueryRowContext(ctx, `SELECT province_id FROM cities WHERE id = ?`, address.CityID).Scan(&provinceID) + if pErr != nil && pErr != sql.ErrNoRows { + return entity.Address{}, richerror.New(op).WithErr(pErr). + WithMessage("error querying for existing main address").WithKind(richerror.KindUnexpected) + } + address.ProvinceID = provinceID // Check if the user already has a main address var count int err := d.conn.Conn().QueryRowContext(ctx, `SELECT COUNT(*) FROM addresses WHERE is_main = true AND benefactor_id = ?`, address.BenefactorID).Scan(&count) @@ -25,7 +33,7 @@ func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address // Insert the new address res, err := d.conn.Conn().ExecContext(ctx, `INSERT INTO addresses (postal_code, address, lat, lon, is_main, city_id, province_id, benefactor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, - address.PostalCode, address.Address, address.Lat, address.Lon, address.IsMain, address.CityID, address.ProvinceID, address.BenefactorID) + address.PostalCode, address.Address, address.Lat, address.Lon, address.IsMain, address.CityID, provinceID, address.BenefactorID) if err != nil { return entity.Address{}, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) diff --git a/service/benefactor/address/add.go b/service/benefactor/address/add.go index 25a56a6..e77db5b 100644 --- a/service/benefactor/address/add.go +++ b/service/benefactor/address/add.go @@ -17,7 +17,6 @@ func (s Service) Add(ctx context.Context, req param.BenefactorAddAddressRequest) Lat: req.Lat, Lon: req.Lon, CityID: req.CityID, - ProvinceID: req.ProvinceID, BenefactorID: req.BenefactorID, }) if err != nil { diff --git a/validator/benefactor/address/add_address.go b/validator/benefactor/address/add_address.go index f18c2c6..56a5315 100644 --- a/validator/benefactor/address/add_address.go +++ b/validator/benefactor/address/add_address.go @@ -31,10 +31,6 @@ func (v Validator) ValidateAddAddress(req param.BenefactorAddAddressRequest) (ma validation.Field(&req.CityID, validation.Required, validation.By(v.doesCityExist)), - - validation.Field(&req.ProvinceID, - validation.Required, - validation.By(v.doesProvinceExist)), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/address/validator.go b/validator/benefactor/address/validator.go index 2764d67..520a73f 100644 --- a/validator/benefactor/address/validator.go +++ b/validator/benefactor/address/validator.go @@ -13,7 +13,6 @@ type BenefactorSvc interface { } type Repository interface { IsExistCityByID(ctx context.Context, id uint) (bool, error) - IsExistProvinceByID(ctx context.Context, id uint) (bool, error) } type Validator struct { benefactorSvc BenefactorSvc @@ -37,22 +36,6 @@ func (v Validator) doesBenefactorExist(value interface{}) error { return nil } -func (v Validator) doesProvinceExist(value interface{}) error { - provinceID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - isExisted, err := v.repository.IsExistProvinceByID(context.Background(), provinceID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - if !isExisted { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - func (v Validator) doesCityExist(value interface{}) error { cityID, ok := value.(uint) if !ok {