define order client

This commit is contained in:
Sinh 2022-08-20 21:21:51 +07:00
parent 0eb670a0c5
commit 56e810378e
4 changed files with 54 additions and 0 deletions

36
client/order.go Normal file
View File

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

View File

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

9
model/order_request.go Normal file
View File

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

7
subject/order.go Normal file
View File

@ -0,0 +1,7 @@
package subject
const orderPrefix = "order_"
const (
OrderUpdateORStatus = orderPrefix + "update_outbound_request_status"
)