From 56e810378ee0740d0bd9cd15e32560fd18559529 Mon Sep 17 00:00:00 2001 From: Sinh Date: Sat, 20 Aug 2022 21:21:51 +0700 Subject: [PATCH] define order client --- client/order.go | 36 ++++++++++++++++++++++++++++++++++++ json_encoder.go | 2 ++ model/order_request.go | 9 +++++++++ subject/order.go | 7 +++++++ 4 files changed, 54 insertions(+) create mode 100644 client/order.go create mode 100644 model/order_request.go create mode 100644 subject/order.go diff --git a/client/order.go b/client/order.go new file mode 100644 index 0000000..adae205 --- /dev/null +++ b/client/order.go @@ -0,0 +1,36 @@ +package client + +import ( + "encoding/json" + "errors" + + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// Order ... +type Order struct{} + +// GetOrder ... +func GetOrder() Order { + return Order{} +} + +// UpdateORStatus ... +func (w Order) UpdateORStatus(p model.OrderUpdateORStatus) error { + msg, err := natsio.GetServer().Request(subject.OrderUpdateORStatus, toBytes(p)) + if err != nil { + return err + } + var r struct { + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + return nil +} diff --git a/json_encoder.go b/json_encoder.go index 379b408..4a0bdb8 100644 --- a/json_encoder.go +++ b/json_encoder.go @@ -16,6 +16,8 @@ func (e JSONEncoder) Subscribe(subject string, cb nats.Handler) (*nats.Subscript sub, err := e.encConn.Subscribe(subject, cb) if err != nil { log.Printf("natsio.JSONEncoder.Subscribe err: %v\n", err) + } else { + log.Printf("natsio.JSONEncoder - subscribed to subject %s successfully\n", subject) } return sub, err } diff --git a/model/order_request.go b/model/order_request.go new file mode 100644 index 0000000..e664b1a --- /dev/null +++ b/model/order_request.go @@ -0,0 +1,9 @@ +package model + +// OrderUpdateORStatus ... +type OrderUpdateORStatus struct { + OrderCode string `json:"orderCode"` + ORCode string `json:"orCode"` + Status string `json:"status"` + Reason string `json:"reason"` +} diff --git a/subject/order.go b/subject/order.go new file mode 100644 index 0000000..f13be90 --- /dev/null +++ b/subject/order.go @@ -0,0 +1,7 @@ +package subject + +const orderPrefix = "order_" + +const ( + OrderUpdateORStatus = orderPrefix + "update_outbound_request_status" +)