forked from ebhomengo/niki
1
0
Fork 0

"fix(niki):Get Province ID by City ID"

This commit is contained in:
AMiR 2024-05-26 19:56:18 +03:30
parent 5262356b5e
commit bdf197e84b
8 changed files with 10 additions and 35 deletions

View File

@ -711,10 +711,6 @@ const docTemplate = `{
"postal_code": { "postal_code": {
"type": "string", "type": "string",
"example": "1234567890" "example": "1234567890"
},
"province_id": {
"type": "integer",
"example": 1
} }
} }
}, },

View File

@ -700,10 +700,6 @@
"postal_code": { "postal_code": {
"type": "string", "type": "string",
"example": "1234567890" "example": "1234567890"
},
"province_id": {
"type": "integer",
"example": 1
} }
} }
}, },

View File

@ -19,9 +19,6 @@ definitions:
postal_code: postal_code:
example: "1234567890" example: "1234567890"
type: string type: string
province_id:
example: 1
type: integer
type: object type: object
addressparam.BenefactorAddAddressResponse: addressparam.BenefactorAddAddressResponse:
properties: properties:

View File

@ -8,7 +8,7 @@ type BenefactorAddAddressRequest struct {
Lat float64 `json:"lat" example:"22.23"` Lat float64 `json:"lat" example:"22.23"`
Lon float64 `json:"lon" example:"22.22"` Lon float64 `json:"lon" example:"22.22"`
CityID uint `json:"city_id" example:"1"` CityID uint `json:"city_id" example:"1"`
ProvinceID uint `json:"province_id" example:"1"` ProvinceID uint `json:"-"`
BenefactorID uint `json:"benefactor_id" example:"1"` BenefactorID uint `json:"benefactor_id" example:"1"`
} }

View File

@ -12,6 +12,14 @@ import (
func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) { func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) {
const op = "mysqlbenefactor.createBenefactorAddress" 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 // Check if the user already has a main address
var count int var count int
err := d.conn.Conn().QueryRowContext(ctx, `SELECT COUNT(*) FROM addresses WHERE is_main = true AND benefactor_id = ?`, address.BenefactorID).Scan(&count) 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 // 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 (?, ?, ?, ?, ?, ?, ?, ?)`, 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 { if err != nil {
return entity.Address{}, richerror.New(op).WithErr(err). return entity.Address{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)

View File

@ -17,7 +17,6 @@ func (s Service) Add(ctx context.Context, req param.BenefactorAddAddressRequest)
Lat: req.Lat, Lat: req.Lat,
Lon: req.Lon, Lon: req.Lon,
CityID: req.CityID, CityID: req.CityID,
ProvinceID: req.ProvinceID,
BenefactorID: req.BenefactorID, BenefactorID: req.BenefactorID,
}) })
if err != nil { if err != nil {

View File

@ -31,10 +31,6 @@ func (v Validator) ValidateAddAddress(req param.BenefactorAddAddressRequest) (ma
validation.Field(&req.CityID, validation.Field(&req.CityID,
validation.Required, validation.Required,
validation.By(v.doesCityExist)), validation.By(v.doesCityExist)),
validation.Field(&req.ProvinceID,
validation.Required,
validation.By(v.doesProvinceExist)),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,7 +13,6 @@ type BenefactorSvc interface {
} }
type Repository interface { type Repository interface {
IsExistCityByID(ctx context.Context, id uint) (bool, error) IsExistCityByID(ctx context.Context, id uint) (bool, error)
IsExistProvinceByID(ctx context.Context, id uint) (bool, error)
} }
type Validator struct { type Validator struct {
benefactorSvc BenefactorSvc benefactorSvc BenefactorSvc
@ -37,22 +36,6 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
return nil 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 { func (v Validator) doesCityExist(value interface{}) error {
cityID, ok := value.(uint) cityID, ok := value.(uint)
if !ok { if !ok {