Merge pull request #6 from Selly-Modules/get-location-warehouse

nats-location-supplier
This commit is contained in:
Sinh Luu 2022-08-26 17:37:24 +07:00 committed by GitHub
commit 3da067b159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 141 additions and 0 deletions

38
client/location.go Normal file
View File

@ -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.Location.GetLocationByCode, 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
}

34
client/supplier.go Normal file
View File

@ -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.Supplier.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
}

View File

@ -0,0 +1,8 @@
package model
// LocationRequestPayload ...
type LocationRequestPayload struct {
Province int `json:"province"`
District int `json:"district"`
Ward int `json:"ward"`
}

View File

@ -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"`
}

View File

@ -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",
}

13
subject/location.go Normal file
View File

@ -0,0 +1,13 @@
package subject
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"),
}

13
subject/supplier.go Normal file
View File

@ -0,0 +1,13 @@
package subject
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"),
}