From 868c3166ce855b94888c4921a4fe898ddbe15dcf Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Tue, 30 Aug 2022 14:03:49 +0700 Subject: [PATCH] [Update] Add dao request warehouse --- client/warehouse.go | 76 --------------------------------- client/warehouse_dao.go | 85 +++++++++++++++++++++++++++++++++++++ model/warehouse_response.go | 2 +- 3 files changed, 86 insertions(+), 77 deletions(-) create mode 100644 client/warehouse_dao.go diff --git a/client/warehouse.go b/client/warehouse.go index 5341fb0..ed19faf 100644 --- a/client/warehouse.go +++ b/client/warehouse.go @@ -17,82 +17,6 @@ func GetWarehouse() Warehouse { return Warehouse{} } -// DistinctWithField ... -func (w Warehouse) DistinctWithField(p model.FindWithCondition) ([]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.FindWithCondition) (*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 -} - // CreateOutboundRequest ... func (w Warehouse) CreateOutboundRequest(p model.OutboundRequestPayload) (*model.OutboundRequestResponse, error) { msg, err := natsio.GetServer().Request(subject.Warehouse.CreateOutboundRequest, toBytes(p)) diff --git a/client/warehouse_dao.go b/client/warehouse_dao.go new file mode 100644 index 0000000..10f2add --- /dev/null +++ b/client/warehouse_dao.go @@ -0,0 +1,85 @@ +package client + +import ( + "encoding/json" + "errors" + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// DistinctWithField ... +func (w Warehouse) DistinctWithField(p model.FindWithCondition) ([]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.FindWithCondition) (*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 +} diff --git a/model/warehouse_response.go b/model/warehouse_response.go index 9010be8..e0d35b1 100644 --- a/model/warehouse_response.go +++ b/model/warehouse_response.go @@ -98,7 +98,7 @@ type WarehouseNatsResponse struct { SearchString string `json:"searchString"` Slug string `json:"slug"` Status string `json:"status"` - Supplier WarehouseSupplier `json:"supplier"` + Supplier string `json:"supplier"` Contact ResponseWarehouseContact `json:"contact"` Location ResponseWarehouseLocation `json:"location"` Configurations WarehouseConfiguration `json:"configurations"`