natsio/client/supplier_role.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-10-28 02:31:39 +00:00
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"
)
// SupplierRole ...
type SupplierRole struct{}
// GetSupplierRole ...
func GetSupplierRole() SupplierRole {
return SupplierRole{}
}
func (s SupplierRole) CreateRole(p model.CreateRoleRequest) (*model.CreateRoleResponse, error) {
msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateOwner, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
Data *model.CreateRoleResponse `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
}
func (s SupplierRole) UpdateRole(p model.UpdateRoleRequest) error {
msg, err := natsio.GetServer().Request(subject.SupplierRole.Update, 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
}