From 021df099ccfc55b15d0bbc52b6f391b8d6d85197 Mon Sep 17 00:00:00 2001 From: quang1472001 Date: Thu, 22 Sep 2022 14:56:31 +0700 Subject: [PATCH] update location --- client/location_dao.go | 180 +++++++++++++++++++++++++++++++++++++ model/location_response.go | 120 +++++++++++++++++-------- subject/location.go | 34 +++++-- 3 files changed, 291 insertions(+), 43 deletions(-) create mode 100644 client/location_dao.go diff --git a/client/location_dao.go b/client/location_dao.go new file mode 100644 index 0000000..3c09c55 --- /dev/null +++ b/client/location_dao.go @@ -0,0 +1,180 @@ +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_response.go b/model/location_response.go index 484d89e..2674041 100644 --- a/model/location_response.go +++ b/model/location_response.go @@ -1,43 +1,93 @@ package model -type ResponseLocationAddress struct { - Province LocationProvince `json:"province"` - District LocationDistrict `json:"district"` - Ward LocationWard `json:"ward"` -} +import "time" -// LocationProvince ... -type LocationProvince struct { - ID string `json:"id"` - Name string `json:"name"` - Code int `json:"code"` -} +type ( + // ResponseLocationAddress ... + ResponseLocationAddress struct { + Province LocationProvince `json:"province"` + District LocationDistrict `json:"district"` + Ward LocationWard `json:"ward"` + } -// LocationDistrict ... -type LocationDistrict struct { - ID string `json:"id"` - Name string `json:"name"` - Code int `json:"code"` -} + // LocationProvince ... + LocationProvince struct { + ID string `json:"id"` + Name string `json:"name"` + Code int `json:"code"` + RegionCode string `json:"regionCode"` + MainRegionCode string `json:"mainRegionCode"` + } -// LocationWard ... -type LocationWard struct { - ID string `json:"id"` - Name string `json:"name"` - Code int `json:"code"` -} + // LocationDistrict ... + LocationDistrict struct { + ID string `json:"id"` + Name string `json:"name"` + Code int `json:"code"` + } -// LocationProvinceResponse ... -type LocationProvinceResponse struct { - Provinces []LocationProvince `json:"provinces"` -} + // LocationWard ... + LocationWard struct { + ID string `json:"id"` + Name string `json:"name"` + Code int `json:"code"` + } -// LocationDistrictResponse ... -type LocationDistrictResponse struct { - Districts []LocationDistrict `json:"districts"` -} + // LocationProvinceResponse ... + LocationProvinceResponse struct { + Provinces []LocationProvince `json:"provinces"` + } -// LocationWardResponse ... -type LocationWardResponse struct { - Wards []LocationWard `json:"wards"` -} + // LocationDistrictResponse ... + LocationDistrictResponse struct { + 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"` + 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"` + 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"` + Code int `json:"code"` + DistrictCode int `json:"districtCode"` + ProvinceCode int `json:"provinceCode"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + } +) diff --git a/subject/location.go b/subject/location.go index b19a59b..7525c25 100644 --- a/subject/location.go +++ b/subject/location.go @@ -7,13 +7,31 @@ func getLocationValue(val string) string { } var Location = struct { - GetLocationByCode string - GetProvincesByCodes string - GetDistrictsByCodes string - GetWardsByCodes string + 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: getLocationValue("get_location_warehouse"), - GetProvincesByCodes: getLocationValue("get_provinces_by_codes"), - GetDistrictsByCodes: getLocationValue("get_districts_by_codes"), - GetWardsByCodes: getLocationValue("get_wards_by_codes"), + 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"), }