From 40533e180544950fcb43b0d72c6f570ee821d455 Mon Sep 17 00:00:00 2001 From: Nguyen Minh Date: Thu, 15 Sep 2022 14:09:54 +0700 Subject: [PATCH] seller by id --- client/seller.go | 40 ++++++++++++++++++++++++++++++++++++++++ model/seller_request.go | 8 ++++++++ model/seller_response.go | 8 ++++++++ subject/config.go | 2 ++ subject/seller.go | 14 ++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 client/seller.go create mode 100644 model/seller_request.go create mode 100644 model/seller_response.go create mode 100644 subject/seller.go diff --git a/client/seller.go b/client/seller.go new file mode 100644 index 0000000..ec3ab83 --- /dev/null +++ b/client/seller.go @@ -0,0 +1,40 @@ +package client + +import ( + "encoding/json" + "errors" + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// Seller ... +type Seller struct{} + +// GetSeller ... +func GetSeller() Seller { + return Seller{} +} + +// GetSellerInfoByID ... +func (s Seller) GetSellerInfoByID(p model.GetSellerByIDRequest) (*model.ResponseSellerInfo, error) { + msg, err := natsio.GetServer().Request(subject.Seller.GetSellerInfoByID, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResponseSellerInfo `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/seller_request.go b/model/seller_request.go new file mode 100644 index 0000000..997cd97 --- /dev/null +++ b/model/seller_request.go @@ -0,0 +1,8 @@ +package model + +import "go.mongodb.org/mongo-driver/bson/primitive" + +// GetSellerByIDRequest ... +type GetSellerByIDRequest struct { + SellerID primitive.ObjectID `json:"sellerId"` +} diff --git a/model/seller_response.go b/model/seller_response.go new file mode 100644 index 0000000..6b7d698 --- /dev/null +++ b/model/seller_response.go @@ -0,0 +1,8 @@ +package model + +// ResponseSellerInfo ... +type ResponseSellerInfo struct { + ID string `json:"id"` + Name string `json:"name"` + Code string `json:"code"` +} diff --git a/subject/config.go b/subject/config.go index dfeea56..cb215bc 100644 --- a/subject/config.go +++ b/subject/config.go @@ -6,10 +6,12 @@ var prefixes = struct { Warehouse string Location string Supplier string + Seller string }{ Communication: "communication", Order: "order", Warehouse: "warehouse", Location: "location", Supplier: "supplier", + Seller: "seller", } diff --git a/subject/seller.go b/subject/seller.go new file mode 100644 index 0000000..033f993 --- /dev/null +++ b/subject/seller.go @@ -0,0 +1,14 @@ +package subject + +import "fmt" + +func getSellerValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Seller, val) +} + +// Seller ... +var Seller = struct { + GetSellerInfoByID string +}{ + GetSellerInfoByID: getSellerValue("get_seller_info_by_id"), +}