123 lines
2.9 KiB
Go
123 lines
2.9 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"
|
|
)
|
|
|
|
// DistinctWithField ...
|
|
func (w Warehouse) DistinctWithField(p model.DistinctWithField) ([]interface{}, error) {
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.Distinct, bsonToBytes(p))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Data []interface{} `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
|
|
}
|
|
|
|
// FindOneByCondition ...
|
|
func (w Warehouse) FindOneByCondition(p model.FindOneCondition) (*model.WarehouseNatsResponse, error) {
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.FindOne, bsonToBytes(p))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Data *model.WarehouseNatsResponse `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
|
|
}
|
|
|
|
// CountByCondition ...
|
|
func (w Warehouse) CountByCondition(p model.FindWithCondition) (int64, error) {
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.Count, bsonToBytes(p))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
var r struct {
|
|
Data int64 `json:"data"`
|
|
Error string `json:"error"`
|
|
}
|
|
if err = json.Unmarshal(msg.Data, &r); err != nil {
|
|
return 0, err
|
|
}
|
|
if r.Error != "" {
|
|
return 0, errors.New(r.Error)
|
|
}
|
|
return r.Data, nil
|
|
}
|
|
|
|
// FindByCondition ...
|
|
func (w Warehouse) FindByCondition(p model.FindWithCondition) ([]*model.WarehouseNatsResponse, error) {
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.FindByCondition, bsonToBytes(p))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Data []*model.WarehouseNatsResponse `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
|
|
}
|
|
|
|
// UpdateWarehouseConfig ...
|
|
func (w Warehouse) UpdateWarehouseConfig(p model.UpdatePayload) error {
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.UpdateWarehouseConfig, bsonToBytes(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
|
|
}
|
|
|
|
// BulkWriteWarehouseConfig ...
|
|
func (w Warehouse) BulkWriteWarehouseConfig(p model.UpdatePayload) error {
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.BulkWriteWarehouseConfig, bsonToBytes(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
|
|
}
|