natsio/client/product.go

87 lines
1.8 KiB
Go

package client
import (
"encoding/json"
"errors"
"git.selly.red/Selly-Modules/natsio"
"git.selly.red/Selly-Modules/natsio/model"
"git.selly.red/Selly-Modules/natsio/subject"
)
// Product ...
type Product struct{}
// GetProduct ...
func GetProduct() Product {
return Product{}
}
func (c Product) ApplyRequest(p model.ProductApplyRequestPayload) error {
msg, err := natsio.GetServer().Request(subject.Product.ApplyRequest, 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
}
func (c Product) CreateRequestSteps(p model.ProductCreateStepsPayload) error {
msg, err := natsio.GetServer().Request(subject.Product.CreateRequestStep, 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
}
func (c Product) ProcessApplyRequest(p model.ProductApplyRequestPayload) error {
msg, err := natsio.GetServer().Request(subject.Product.ProcessApplyRequest, 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
}
func (c Product) RequestChangeStatus(p model.ProductRequestChangeStatus) error {
msg, err := natsio.GetServer().Request(subject.Product.RequestChangeStatus, 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
}