From 3458f885ec240e518d55e101a2468128dd457bf9 Mon Sep 17 00:00:00 2001 From: phuanbui Date: Fri, 26 Aug 2022 17:15:49 +0700 Subject: [PATCH 1/2] nats-location-supplier --- client/location.go | 38 ++++++++++++++++++++++++++++++++++++++ client/supplier.go | 34 ++++++++++++++++++++++++++++++++++ model/location_request.go | 8 ++++++++ model/location_response.go | 31 +++++++++++++++++++++++++++++++ subject/location.go | 8 ++++++++ subject/supplier.go | 8 ++++++++ 6 files changed, 127 insertions(+) create mode 100644 client/location.go create mode 100644 client/supplier.go create mode 100644 model/location_request.go create mode 100644 model/location_response.go create mode 100644 subject/location.go create mode 100644 subject/supplier.go diff --git a/client/location.go b/client/location.go new file mode 100644 index 0000000..f5a6688 --- /dev/null +++ b/client/location.go @@ -0,0 +1,38 @@ +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{} +} + +func (l Location) GetLocationByCode(payload model.LocationRequestPayload) (*model.ResponseLocationAddress, error) { + msg, err := natsio.GetServer().Request(subject.GetLocationWarehouse, toBytes(payload)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResponseLocationAddress `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 +} diff --git a/client/supplier.go b/client/supplier.go new file mode 100644 index 0000000..07c7a39 --- /dev/null +++ b/client/supplier.go @@ -0,0 +1,34 @@ +package client + +import ( + "encoding/json" + "errors" + + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// Supplier ... +type Supplier struct{} + +func (s Supplier) GetSupplierInfo(supplierID string) (*model.ResponseSupplierInfo, error) { + msg, err := natsio.GetServer().Request(subject.GetSupplierInfo, toBytes(supplierID)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResponseSupplierInfo `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 +} diff --git a/model/location_request.go b/model/location_request.go new file mode 100644 index 0000000..e6781ca --- /dev/null +++ b/model/location_request.go @@ -0,0 +1,8 @@ +package model + +// LocationRequestPayload ... +type LocationRequestPayload struct { + Province int `json:"province"` + District int `json:"district"` + Ward int `json:"ward"` +} diff --git a/model/location_response.go b/model/location_response.go new file mode 100644 index 0000000..fcb4dbb --- /dev/null +++ b/model/location_response.go @@ -0,0 +1,31 @@ +package model + +type ResponseLocationAddress struct { + Province LocationProvince `json:"province"` + District LocationDistrict `json:"district"` + Ward LocationWard `json:"ward"` +} + +// LocationProvince ... +type LocationProvince struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// LocationDistrict ... +type LocationDistrict struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// LocationWard ... +type LocationWard struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// ResponseSupplierInfo ... +type ResponseSupplierInfo struct { + ID string `json:"id"` + Name string `json:"name"` +} diff --git a/subject/location.go b/subject/location.go new file mode 100644 index 0000000..d24a86b --- /dev/null +++ b/subject/location.go @@ -0,0 +1,8 @@ +package subject + +const ( + locationPrefix = "location_" +) +const ( + GetLocationWarehouse = locationPrefix + "get_address" +) diff --git a/subject/supplier.go b/subject/supplier.go new file mode 100644 index 0000000..93ce7de --- /dev/null +++ b/subject/supplier.go @@ -0,0 +1,8 @@ +package subject + +const ( + supplierPrefix = "supplier" +) +const ( + GetSupplierInfo = supplierPrefix + "get_info" +) From d79739aa0f9a74da06b154efbba07251e0369055 Mon Sep 17 00:00:00 2001 From: phuanbui Date: Fri, 26 Aug 2022 17:36:02 +0700 Subject: [PATCH 2/2] refactor-code --- client/location.go | 2 +- client/supplier.go | 2 +- subject/config.go | 4 ++++ subject/location.go | 17 +++++++++++------ subject/supplier.go | 17 +++++++++++------ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/client/location.go b/client/location.go index f5a6688..7fc3474 100644 --- a/client/location.go +++ b/client/location.go @@ -18,7 +18,7 @@ func GetLocation() Location { } func (l Location) GetLocationByCode(payload model.LocationRequestPayload) (*model.ResponseLocationAddress, error) { - msg, err := natsio.GetServer().Request(subject.GetLocationWarehouse, toBytes(payload)) + msg, err := natsio.GetServer().Request(subject.Location.GetLocationByCode, toBytes(payload)) if err != nil { return nil, err } diff --git a/client/supplier.go b/client/supplier.go index 07c7a39..23390e0 100644 --- a/client/supplier.go +++ b/client/supplier.go @@ -13,7 +13,7 @@ import ( type Supplier struct{} func (s Supplier) GetSupplierInfo(supplierID string) (*model.ResponseSupplierInfo, error) { - msg, err := natsio.GetServer().Request(subject.GetSupplierInfo, toBytes(supplierID)) + msg, err := natsio.GetServer().Request(subject.Supplier.GetSupplierInfo, toBytes(supplierID)) if err != nil { return nil, err } diff --git a/subject/config.go b/subject/config.go index fd6ccdf..dfeea56 100644 --- a/subject/config.go +++ b/subject/config.go @@ -4,8 +4,12 @@ var prefixes = struct { Communication string Order string Warehouse string + Location string + Supplier string }{ Communication: "communication", Order: "order", Warehouse: "warehouse", + Location: "location", + Supplier: "supplier", } diff --git a/subject/location.go b/subject/location.go index d24a86b..1989df3 100644 --- a/subject/location.go +++ b/subject/location.go @@ -1,8 +1,13 @@ package subject -const ( - locationPrefix = "location_" -) -const ( - GetLocationWarehouse = locationPrefix + "get_address" -) +import "fmt" + +func getLocationValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Location, val) +} + +var Location = struct { + GetLocationByCode string +}{ + GetLocationByCode: getLocationValue("get_location_warehouse"), +} diff --git a/subject/supplier.go b/subject/supplier.go index 93ce7de..4b4cf52 100644 --- a/subject/supplier.go +++ b/subject/supplier.go @@ -1,8 +1,13 @@ package subject -const ( - supplierPrefix = "supplier" -) -const ( - GetSupplierInfo = supplierPrefix + "get_info" -) +import "fmt" + +func getSupplierValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Supplier, val) +} + +var Supplier = struct { + GetSupplierInfo string +}{ + GetSupplierInfo: getSupplierValue("get_supplier_info"), +}