From 7a3e50ef97b87d5c87ca6b3ecffaf95c10f20e0e Mon Sep 17 00:00:00 2001 From: quang1472001 Date: Wed, 5 Oct 2022 17:59:44 +0700 Subject: [PATCH] update location --- client/location.go | 201 ++++++++++++++++++++++++++++++++++++-- client/location_dao.go | 180 ---------------------------------- model/location_request.go | 57 +++++++---- subject/location.go | 52 +++++----- 4 files changed, 261 insertions(+), 229 deletions(-) delete mode 100644 client/location_dao.go diff --git a/client/location.go b/client/location.go index 2e8d30e..96d235c 100644 --- a/client/location.go +++ b/client/location.go @@ -28,7 +28,7 @@ func (l Location) GetLocationByCode(payload model.LocationRequestPayload) (*mode Data *model.ResponseLocationAddress `json:"data"` Error string `json:"error"` } - if err := json.Unmarshal(msg.Data, &r); err != nil { + if err = json.Unmarshal(msg.Data, &r); err != nil { return nil, err } @@ -38,7 +38,7 @@ func (l Location) GetLocationByCode(payload model.LocationRequestPayload) (*mode return r.Data, nil } -// GetProvincesByCodes ... ... +// GetProvincesByCodes ... func (l Location) GetProvincesByCodes(p model.ProvinceRequestPayload) (*model.LocationProvinceResponse, error) { msg, err := natsio.GetServer().Request(subject.Location.GetProvincesByCodes, toBytes(p)) if err != nil { @@ -46,11 +46,11 @@ func (l Location) GetProvincesByCodes(p model.ProvinceRequestPayload) (*model.Lo } var r struct { - Data *model.LocationProvinceResponse `json:"data"'` + Data *model.LocationProvinceResponse `json:"data"` Error string `json:"error"` } - if err := json.Unmarshal(msg.Data, &r); err != nil { + if err = json.Unmarshal(msg.Data, &r); err != nil { return nil, err } @@ -71,7 +71,7 @@ func (l Location) GetDistrictsByCodes(p model.DistrictRequestPayload) (*model.Lo Error string `json:"error"` } - if err := json.Unmarshal(msg.Data, &r); err != nil { + if err = json.Unmarshal(msg.Data, &r); err != nil { return nil, err } @@ -94,7 +94,7 @@ func (l Location) GetWardsByCodes(p model.WardRequestPayload) (*model.LocationWa Error string `json:"error"` } - if err := json.Unmarshal(msg.Data, &r); err != nil { + if err = json.Unmarshal(msg.Data, &r); err != nil { return nil, err } @@ -104,3 +104,192 @@ func (l Location) GetWardsByCodes(p model.WardRequestPayload) (*model.LocationWa return r.Data, nil } + +// GetProvinceByCondition ... +func (l Location) GetProvinceByCondition(p model.ProvinceRequestCondition) (*model.LocationProvinceDetailResponse, error) { + msg, err := natsio.GetServer().Request(subject.Location.GetProvinceByCondition, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.LocationProvinceDetailResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// GetProvincesByCondition ... +func (l Location) GetProvincesByCondition(p model.DistrictRequestCondition) ([]*model.LocationProvinceDetailResponse, error) { + msg, err := natsio.GetServer().Request(subject.Location.GetProvincesByCondition, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data []*model.LocationProvinceDetailResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// GetDistrictByCondition ... +func (l Location) GetDistrictByCondition(p model.ProvinceRequestCondition) (*model.LocationProvinceDetailResponse, error) { + msg, err := natsio.GetServer().Request(subject.Location.GetDistrictByCondition, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.LocationProvinceDetailResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// GetDistrictsByCondition ... +func (l Location) GetDistrictsByCondition(p model.ProvinceRequestCondition) ([]*model.LocationDistrictDetailResponse, error) { + msg, err := natsio.GetServer().Request(subject.Location.GetDistrictsByCondition, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data []*model.LocationDistrictDetailResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// GetWardByCondition ... +func (l Location) GetWardByCondition(p model.DistrictRequestCondition) (*model.LocationWardDetailResponse, error) { + msg, err := natsio.GetServer().Request(subject.Location.GetWardByCondition, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.LocationWardDetailResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// GetWardsByCondition ... +func (l Location) GetWardsByCondition(p model.ProvinceRequestCondition) ([]*model.LocationWardDetailResponse, error) { + msg, err := natsio.GetServer().Request(subject.Location.GetWardsByCondition, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data []*model.LocationWardDetailResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// CountProvinceByCondition ... +func (l Location) CountProvinceByCondition(p model.WardRequestCondition) (int64, error) { + msg, err := natsio.GetServer().Request(subject.Location.CountProvinceByCondition, toBytes(p)) + if err != nil { + return 0, err + } + var r struct { + Data int64 `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return 0, err + } + if r.Error != "" { + return 0, errors.New(r.Error) + } + return r.Data, nil +} + +// CountDistrictByCondition ... +func (l Location) CountDistrictByCondition(p model.FindWithCondition) (int64, error) { + msg, err := natsio.GetServer().Request(subject.Location.CountDistrictByCondition, toBytes(p)) + if err != nil { + return 0, err + } + var r struct { + Data int64 `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return 0, err + } + if r.Error != "" { + return 0, errors.New(r.Error) + } + return r.Data, nil +} + +// CountWardByCondition ... +func (l Location) CountWardByCondition(p model.FindWithCondition) (int64, error) { + msg, err := natsio.GetServer().Request(subject.Location.CountWardByCondition, toBytes(p)) + if err != nil { + return 0, err + } + var r struct { + Data int64 `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return 0, err + } + if r.Error != "" { + return 0, errors.New(r.Error) + } + return r.Data, nil +} diff --git a/client/location_dao.go b/client/location_dao.go deleted file mode 100644 index 3c09c55..0000000 --- a/client/location_dao.go +++ /dev/null @@ -1,180 +0,0 @@ -package client - -import ( - "encoding/json" - "errors" - "github.com/Selly-Modules/natsio" - "github.com/Selly-Modules/natsio/model" - "github.com/Selly-Modules/natsio/subject" -) - -// FindOneProvinceByCondition ... -func (l Location) FindOneProvinceByCondition(p model.FindOneCondition) (*model.LocationProvinceDetailResponse, error) { - msg, err := natsio.GetServer().Request(subject.Location.FindOneProvinceByCondition, bsonToBytes(p)) - if err != nil { - return nil, err - } - var r struct { - Data *model.LocationProvinceDetailResponse `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Data, nil -} - -// FindOneDistrictByCondition ... -func (l Location) FindOneDistrictByCondition(p model.FindOneCondition) (*model.LocationDistrictDetailResponse, error) { - msg, err := natsio.GetServer().Request(subject.Location.FindOneDistrictByCondition, bsonToBytes(p)) - if err != nil { - return nil, err - } - var r struct { - Data *model.LocationDistrictDetailResponse `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Data, nil -} - -// FindOneWardByCondition ... -func (l Location) FindOneWardByCondition(p model.FindOneCondition) (*model.LocationWardDetailResponse, error) { - msg, err := natsio.GetServer().Request(subject.Location.FindOneWardByCondition, bsonToBytes(p)) - if err != nil { - return nil, err - } - var r struct { - Data *model.LocationWardDetailResponse `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Data, nil -} - -// FindProvinceByCondition ... -func (l Location) FindProvinceByCondition(p model.FindWithCondition) ([]*model.LocationProvinceDetailResponse, error) { - msg, err := natsio.GetServer().Request(subject.Location.FindProvinceByCondition, bsonToBytes(p)) - if err != nil { - return nil, err - } - var r struct { - Data []*model.LocationProvinceDetailResponse `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Data, nil -} - -// FindDistrictByCondition ... -func (l Location) FindDistrictByCondition(p model.FindWithCondition) ([]*model.LocationDistrictDetailResponse, error) { - msg, err := natsio.GetServer().Request(subject.Location.FindDistrictByCondition, bsonToBytes(p)) - if err != nil { - return nil, err - } - var r struct { - Data []*model.LocationDistrictDetailResponse `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Data, nil -} - -// FindWardByCondition ... -func (l Location) FindWardByCondition(p model.FindWithCondition) ([]*model.LocationWardDetailResponse, error) { - msg, err := natsio.GetServer().Request(subject.Location.FindWardByCondition, bsonToBytes(p)) - if err != nil { - return nil, err - } - var r struct { - Data []*model.LocationWardDetailResponse `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return nil, err - } - if r.Error != "" { - return nil, errors.New(r.Error) - } - return r.Data, nil -} - -// CountProvinceByCondition ... -func (l Location) CountProvinceByCondition(p model.FindWithCondition) (int64, error) { - msg, err := natsio.GetServer().Request(subject.Location.CountProvinceByCondition, bsonToBytes(p)) - if err != nil { - return 0, err - } - var r struct { - Data int64 `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return 0, err - } - if r.Error != "" { - return 0, errors.New(r.Error) - } - return r.Data, nil -} - -// CountDistrictByCondition ... -func (l Location) CountDistrictByCondition(p model.FindWithCondition) (int64, error) { - msg, err := natsio.GetServer().Request(subject.Location.CountDistrictByCondition, bsonToBytes(p)) - if err != nil { - return 0, err - } - var r struct { - Data int64 `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return 0, err - } - if r.Error != "" { - return 0, errors.New(r.Error) - } - return r.Data, nil -} - -// CountWardByCondition ... -func (l Location) CountWardByCondition(p model.FindWithCondition) (int64, error) { - msg, err := natsio.GetServer().Request(subject.Location.CountWardByCondition, bsonToBytes(p)) - if err != nil { - return 0, err - } - var r struct { - Data int64 `json:"data"` - Error string `json:"error"` - } - if err = json.Unmarshal(msg.Data, &r); err != nil { - return 0, err - } - if r.Error != "" { - return 0, errors.New(r.Error) - } - return r.Data, nil -} diff --git a/model/location_request.go b/model/location_request.go index 4533db7..22bdea7 100644 --- a/model/location_request.go +++ b/model/location_request.go @@ -1,23 +1,46 @@ package model // LocationRequestPayload ... -type LocationRequestPayload struct { - Province int `json:"province"` - District int `json:"district"` - Ward int `json:"ward"` -} +type ( + LocationRequestPayload struct { + Province int `json:"province"` + District int `json:"district"` + Ward int `json:"ward"` + } -// ProvinceRequestPayload ... -type ProvinceRequestPayload struct { - Codes []int `json:"codes"` -} + // ProvinceRequestPayload ... + ProvinceRequestPayload struct { + Codes []int `json:"codes"` + } -// DistrictRequestPayload ... -type DistrictRequestPayload struct { - Codes []int `json:"codes"` -} + // ProvinceRequestCondition ... + ProvinceRequestCondition struct { + Code int `json:"code"` + Codes []int `json:"codes"` + } -// WardRequestPayload ... -type WardRequestPayload struct { - Codes []int `json:"codes"` -} + // DistrictRequestPayload ... + DistrictRequestPayload struct { + Codes []int `json:"codes"` + } + + // DistrictRequestCondition ... + DistrictRequestCondition struct { + Code int `json:"code"` + Codes int `json:"codes"` + ProvinceCode int `json:"provinceCode"` + } + + // WardRequestPayload ... + WardRequestPayload struct { + Codes []int `json:"codes"` + } + + // WardRequestCondition ... + WardRequestCondition struct { + Code int `json:"code"` + Codes []int `json:"codes"` + DistrictCode int `json:"districtCode"` + ProvinceCode int `json:"provinceCode"` + } +) diff --git a/subject/location.go b/subject/location.go index 7525c25..40a21e4 100644 --- a/subject/location.go +++ b/subject/location.go @@ -7,31 +7,31 @@ func getLocationValue(val string) string { } var Location = struct { - GetLocationByCode string - GetProvincesByCodes string - GetDistrictsByCodes string - GetWardsByCodes string - FindOneProvinceByCondition string - FindOneDistrictByCondition string - FindOneWardByCondition string - FindProvinceByCondition string - FindDistrictByCondition string - FindWardByCondition string - CountProvinceByCondition string - CountDistrictByCondition string - CountWardByCondition string + GetLocationByCode string + GetProvincesByCodes string + GetDistrictsByCodes string + GetWardsByCodes string + GetProvinceByCondition string + GetProvincesByCondition string + GetDistrictByCondition string + GetDistrictsByCondition string + GetWardByCondition string + GetWardsByCondition string + CountProvinceByCondition string + CountDistrictByCondition string + CountWardByCondition string }{ - GetLocationByCode: getLocationValue("get_location_warehouse"), - GetProvincesByCodes: getLocationValue("get_provinces_by_codes"), - GetDistrictsByCodes: getLocationValue("get_districts_by_codes"), - GetWardsByCodes: getLocationValue("get_wards_by_codes"), - FindOneProvinceByCondition: getLocationValue("find_one_province_by_condition"), - FindOneDistrictByCondition: getLocationValue("find_one_district_by_condition"), - FindOneWardByCondition: getLocationValue("find_one_ward_by_condition"), - FindProvinceByCondition: getLocationValue("find_province_by_condition"), - FindDistrictByCondition: getLocationValue("find_district_by_condition"), - FindWardByCondition: getLocationValue("find_ward_by_condition"), - CountProvinceByCondition: getLocationValue("count_province_by_condition"), - CountDistrictByCondition: getLocationValue("count_district_by_condition"), - CountWardByCondition: getLocationValue("count_ward_by_condition"), + GetLocationByCode: getLocationValue("get_location_warehouse"), + GetProvincesByCodes: getLocationValue("get_provinces_by_codes"), + GetDistrictsByCodes: getLocationValue("get_districts_by_codes"), + GetWardsByCodes: getLocationValue("get_wards_by_codes"), + GetProvinceByCondition: getLocationValue("get_province_by_condition"), + GetProvincesByCondition: getLocationValue("get_provinces_by_condition"), + GetDistrictByCondition: getLocationValue("get_district_by_condition"), + GetDistrictsByCondition: getLocationValue("get_districts_byCondition"), + GetWardByCondition: getLocationValue("get_ward_by_condition"), + GetWardsByCondition: getLocationValue("get_wards_by_condition"), + CountProvinceByCondition: getLocationValue("count_province_by_condition"), + CountDistrictByCondition: getLocationValue("count_district_by_condition"), + CountWardByCondition: getLocationValue("count_ward_by_condition"), }