natsio/client/location.go

296 lines
7.2 KiB
Go
Raw Normal View History

2022-08-26 10:15:49 +00:00
package client
import (
"encoding/json"
"errors"
"github.com/Selly-Modules/natsio"
"github.com/Selly-Modules/natsio/model"
"github.com/Selly-Modules/natsio/subject"
)
// Location ...
type Location struct{}
// GetLocation ...
func GetLocation() Location {
return Location{}
}
2022-09-07 07:01:29 +00:00
// GetLocationByCode ...
2022-08-26 10:15:49 +00:00
func (l Location) GetLocationByCode(payload model.LocationRequestPayload) (*model.ResponseLocationAddress, error) {
2022-08-26 10:36:02 +00:00
msg, err := natsio.GetServer().Request(subject.Location.GetLocationByCode, toBytes(payload))
2022-08-26 10:15:49 +00:00
if err != nil {
return nil, err
}
var r struct {
Data *model.ResponseLocationAddress `json:"data"`
Error string `json:"error"`
}
2022-10-05 10:59:44 +00:00
if err = json.Unmarshal(msg.Data, &r); err != nil {
2022-08-26 10:15:49 +00:00
return nil, err
}
if r.Error != "" {
return nil, errors.New(r.Error)
}
return r.Data, nil
}
2022-09-07 07:01:29 +00:00
2022-10-05 10:59:44 +00:00
// GetProvincesByCodes ...
2022-09-07 07:01:29 +00:00
func (l Location) GetProvincesByCodes(p model.ProvinceRequestPayload) (*model.LocationProvinceResponse, error) {
msg, err := natsio.GetServer().Request(subject.Location.GetProvincesByCodes, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
2022-10-05 10:59:44 +00:00
Data *model.LocationProvinceResponse `json:"data"`
2022-09-07 07:01:29 +00:00
Error string `json:"error"`
}
2022-10-05 10:59:44 +00:00
if err = json.Unmarshal(msg.Data, &r); err != nil {
2022-09-07 07:01:29 +00:00
return nil, err
}
if r.Error != "" {
return nil, errors.New(r.Error)
}
return r.Data, nil
}
// GetDistrictsByCodes ...
func (l Location) GetDistrictsByCodes(p model.DistrictRequestPayload) (*model.LocationDistrictResponse, error) {
msg, err := natsio.GetServer().Request(subject.Location.GetDistrictsByCodes, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
Data *model.LocationDistrictResponse `json:"data"`
Error string `json:"error"`
}
2022-10-05 10:59:44 +00:00
if err = json.Unmarshal(msg.Data, &r); err != nil {
2022-09-07 07:01:29 +00:00
return nil, err
}
if r.Error != "" {
return nil, errors.New(r.Error)
}
return r.Data, nil
}
// GetWardsByCodes ...
func (l Location) GetWardsByCodes(p model.WardRequestPayload) (*model.LocationWardResponse, error) {
msg, err := natsio.GetServer().Request(subject.Location.GetWardsByCodes, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
Data *model.LocationWardResponse `json:"data"`
Error string `json:"error"`
}
2022-10-05 10:59:44 +00:00
if err = json.Unmarshal(msg.Data, &r); err != nil {
2022-09-07 07:01:29 +00:00
return nil, err
}
if r.Error != "" {
return nil, errors.New(r.Error)
}
return r.Data, nil
}
2022-10-05 10:59:44 +00:00
// 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 ...
2022-10-05 11:12:42 +00:00
func (l Location) GetProvincesByCondition(p model.ProvinceRequestCondition) ([]*model.LocationProvinceDetailResponse, error) {
2022-10-05 10:59:44 +00:00
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 ...
2022-10-06 03:20:47 +00:00
func (l Location) GetDistrictByCondition(p model.DistrictRequestCondition) (*model.LocationDistrictDetailResponse, error) {
2022-10-05 10:59:44 +00:00
msg, err := natsio.GetServer().Request(subject.Location.GetDistrictByCondition, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
2022-10-06 03:20:47 +00:00
Data *model.LocationDistrictDetailResponse `json:"data"`
2022-10-05 10:59:44 +00:00
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 ...
2022-10-05 11:12:42 +00:00
func (l Location) GetDistrictsByCondition(p model.DistrictRequestCondition) ([]*model.LocationDistrictDetailResponse, error) {
2022-10-05 10:59:44 +00:00
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 ...
2022-10-05 11:12:42 +00:00
func (l Location) GetWardByCondition(p model.WardRequestCondition) (*model.LocationWardDetailResponse, error) {
2022-10-05 10:59:44 +00:00
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 ...
2022-10-05 11:12:42 +00:00
func (l Location) GetWardsByCondition(p model.WardRequestCondition) ([]*model.LocationWardDetailResponse, error) {
2022-10-05 10:59:44 +00:00
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 ...
2022-10-05 11:12:42 +00:00
func (l Location) CountProvinceByCondition(p model.ProvinceRequestCondition) (int64, error) {
2022-10-05 10:59:44 +00:00
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 ...
2022-10-05 11:12:42 +00:00
func (l Location) CountDistrictByCondition(p model.DistrictRequestCondition) (int64, error) {
2022-10-05 10:59:44 +00:00
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 ...
2022-10-05 11:12:42 +00:00
func (l Location) CountWardByCondition(p model.WardRequestCondition) (int64, error) {
2022-10-05 10:59:44 +00:00
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
}