[location] refactor code

This commit is contained in:
quang1472001 2022-10-27 14:30:40 +07:00
parent caaf630b5e
commit 0bed9a6dfb
5 changed files with 399 additions and 60 deletions

View File

@ -104,3 +104,211 @@ func (l Location) GetWardsByCodes(p model.WardRequestPayload) (*model.LocationWa
return r.Data, nil return r.Data, nil
} }
// GetProvinceByCondition ...
func (l Location) GetProvinceByCondition(p model.RequestCondition) (*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.RequestCondition) ([]*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.RequestCondition) (*model.LocationDistrictDetailResponse, error) {
msg, err := natsio.GetServer().Request(subject.Location.GetDistrictByCondition, 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
}
// GetDistrictsByCondition ...
func (l Location) GetDistrictsByCondition(p model.RequestCondition) ([]*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.RequestCondition) (*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.RequestCondition) ([]*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.RequestCondition) (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.RequestCondition) (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.RequestCondition) (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
}
// DistinctWithField ...
func (l Location) DistinctWithField(p model.ProvinceDistinctWithField) ([]interface{}, error) {
msg, err := natsio.GetServer().Request(subject.Location.ProvinceDistinctWithField, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
Data []interface{} `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
}

View File

@ -20,3 +20,19 @@ type ActionBy struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }
// RequestCondition ...
type RequestCondition struct {
Code int `json:"code"`
Codes []int `json:"codes"`
DistrictCode int `json:"districtCode"`
ProvinceCode int `json:"provinceCode"`
Slug string `json:"slug"`
Slugs []string `json:"slugs"`
DistrictSlug string `json:"districtSlug"`
ProvinceSlug string `json:"provinceSlug"`
Keyword string `json:"keyword"`
Region string `json:"region"`
}

View File

@ -1,23 +1,65 @@
package model package model
// LocationRequestPayload ... // LocationRequestPayload ...
type LocationRequestPayload struct { type (
Province int `json:"province"` LocationRequestPayload struct {
District int `json:"district"` Province int `json:"province"`
Ward int `json:"ward"` District int `json:"district"`
} Ward int `json:"ward"`
}
// ProvinceRequestPayload ... // ProvinceRequestPayload ...
type ProvinceRequestPayload struct { ProvinceRequestPayload struct {
Codes []int `json:"codes"` Codes []int `json:"codes"`
} }
// DistrictRequestPayload ... // ProvinceRequestCondition ...
type DistrictRequestPayload struct { ProvinceRequestCondition struct {
Codes []int `json:"codes"` Code int `json:"code"`
} Codes []int `json:"codes"`
Slug string `json:"slug"`
Slugs []string `json:"slugs"`
Keyword string `json:"keyword"`
Region string `json:"region"`
}
// WardRequestPayload ... // DistrictRequestPayload ...
type WardRequestPayload struct { DistrictRequestPayload struct {
Codes []int `json:"codes"` Codes []int `json:"codes"`
} }
// DistrictRequestCondition ...
DistrictRequestCondition struct {
Code int `json:"code"`
Codes []int `json:"codes"`
ProvinceCode int `json:"provinceCode"`
Slug string `json:"slug"`
ProvinceSlug string `json:"provinceSlug"`
Keyword string `json:"keyword"`
}
// 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"`
Slug string `json:"slug"`
DistrictSlug string `json:"districtSlug"`
ProvinceSlug string `json:"provinceSlug"`
Keyword string `json:"keyword"`
}
// ProvinceDistinctWithField ...
ProvinceDistinctWithField struct {
Conditions struct {
Region string `json:"region"`
} `json:"conditions"`
Field string `json:"filed"`
}
)

View File

@ -1,43 +1,96 @@
package model package model
type ResponseLocationAddress struct { import "time"
Province LocationProvince `json:"province"`
District LocationDistrict `json:"district"`
Ward LocationWard `json:"ward"`
}
// LocationProvince ... type (
type LocationProvince struct { // ResponseLocationAddress ...
ID string `json:"id"` ResponseLocationAddress struct {
Name string `json:"name"` Province LocationProvince `json:"province"`
Code int `json:"code"` District LocationDistrict `json:"district"`
} Ward LocationWard `json:"ward"`
}
// LocationDistrict ... // LocationProvince ...
type LocationDistrict struct { LocationProvince struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Code int `json:"code"` Code int `json:"code"`
} RegionCode string `json:"regionCode"`
MainRegionCode string `json:"mainRegionCode"`
}
// LocationWard ... // LocationDistrict ...
type LocationWard struct { LocationDistrict struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Code int `json:"code"` Code int `json:"code"`
} }
// LocationProvinceResponse ... // LocationWard ...
type LocationProvinceResponse struct { LocationWard struct {
Provinces []LocationProvince `json:"provinces"` ID string `json:"id"`
} Name string `json:"name"`
Code int `json:"code"`
}
// LocationDistrictResponse ... // LocationProvinceResponse ...
type LocationDistrictResponse struct { LocationProvinceResponse struct {
Districts []LocationDistrict `json:"districts"` Provinces []LocationProvince `json:"provinces"`
} }
// LocationWardResponse ... // LocationDistrictResponse ...
type LocationWardResponse struct { LocationDistrictResponse struct {
Wards []LocationWard `json:"wards"` Districts []LocationDistrict `json:"districts"`
} }
// LocationWardResponse ...
LocationWardResponse struct {
Wards []LocationWard `json:"wards"`
}
// LocationProvinceDetailResponse ...
LocationProvinceDetailResponse struct {
ID string `json:"_id"`
Name string `json:"name"`
SearchString string `json:"searchString"`
Slug string `json:"slug"`
OldSlug string `json:"oldSlug"`
Code int `json:"code"`
CountryCode string `json:"countryCode"`
RegionCode string `json:"regionCode"`
MainRegionCode string `json:"mainRegionCode"`
TotalDistricts int `json:"totalDistricts"`
TotalWards int `json:"totalWards"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// LocationDistrictDetailResponse ...
LocationDistrictDetailResponse struct {
ID string `json:"_id"`
Name string `json:"name"`
SearchString string `json:"searchString"`
Slug string `json:"slug"`
OldSlug string `json:"oldSlug"`
Code int `json:"code"`
ProvinceCode int `json:"provinceCode"`
Area int `json:"area"`
TotalWards int `json:"totalWards"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// LocationWardDetailResponse ...
LocationWardDetailResponse struct {
ID string `json:"_id"`
Name string `json:"name"`
SearchString string `json:"searchString"`
Slug string `json:"slug"`
OldSlugs []string `json:"oldSlugs"`
Code int `json:"code"`
DistrictCode int `json:"districtCode"`
ProvinceCode int `json:"provinceCode"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
)

View File

@ -7,13 +7,33 @@ func getLocationValue(val string) string {
} }
var Location = struct { var Location = struct {
GetLocationByCode string GetLocationByCode string
GetProvincesByCodes string GetProvincesByCodes string
GetDistrictsByCodes string GetDistrictsByCodes string
GetWardsByCodes string GetWardsByCodes string
GetProvinceByCondition string
GetProvincesByCondition string
GetDistrictByCondition string
GetDistrictsByCondition string
GetWardByCondition string
GetWardsByCondition string
CountProvinceByCondition string
CountDistrictByCondition string
CountWardByCondition string
ProvinceDistinctWithField string
}{ }{
GetLocationByCode: getLocationValue("get_location_warehouse"), GetLocationByCode: getLocationValue("get_location_warehouse"),
GetProvincesByCodes: getLocationValue("get_provinces_by_codes"), GetProvincesByCodes: getLocationValue("get_provinces_by_codes"),
GetDistrictsByCodes: getLocationValue("get_districts_by_codes"), GetDistrictsByCodes: getLocationValue("get_districts_by_codes"),
GetWardsByCodes: getLocationValue("get_wards_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"),
ProvinceDistinctWithField: getLocationValue("province_distinct_with_field"),
} }