diff --git a/client/util.go b/client/util.go index 3e30617..7676610 100644 --- a/client/util.go +++ b/client/util.go @@ -2,6 +2,7 @@ package client import ( "encoding/json" + "go.mongodb.org/mongo-driver/bson" "log" ) @@ -12,3 +13,9 @@ func toBytes(data interface{}) []byte { } return b } + +// bsonToBytes ... +func bsonToBytes(data interface{}) []byte { + b, _ := bson.Marshal(data) + return b +} diff --git a/client/warehouse_dao.go b/client/warehouse_dao.go new file mode 100644 index 0000000..40c10a4 --- /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.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 +} diff --git a/go.sum b/go.sum index edfc98b..f3e58d6 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= diff --git a/model/common_request.go b/model/common_request.go new file mode 100644 index 0000000..4d6a719 --- /dev/null +++ b/model/common_request.go @@ -0,0 +1,17 @@ +package model + +import "go.mongodb.org/mongo-driver/mongo/options" + +type FindWithCondition struct { + Conditions interface{} `json:"conditions"` + Opts []*options.FindOptions `json:"opts"` +} + +type FindOneCondition struct { + Conditions interface{} `json:"conditions"` +} + +type DistinctWithField struct { + Conditions interface{} `json:"conditions"` + Filed string `json:"filed"` +} diff --git a/model/warehouse_response.go b/model/warehouse_response.go index 2e19c9a..e0d35b1 100644 --- a/model/warehouse_response.go +++ b/model/warehouse_response.go @@ -1,5 +1,7 @@ package model +import "time" + // OutboundRequestResponse ... type OutboundRequestResponse struct { // System code @@ -59,3 +61,47 @@ type WarehousePartner struct { Enabled bool `json:"enabled"` Authentication string `json:"authentication"` } + +// ResponseWarehouseContact ... +type ResponseWarehouseContact struct { + Name string `json:"name"` + Phone string `json:"phone"` + Address string `json:"address"` + Email string `json:"email"` +} + +// ResponseWarehouseLocation ... +type ResponseWarehouseLocation struct { + Province CommonLocation `json:"province"` + District CommonLocation `json:"district"` + Ward CommonLocation `json:"ward"` + Address string `json:"address"` + LocationCoordinates ResponseLatLng `json:"locationCoordinates"` +} + +type CommonLocation struct { + ID string `json:"id"` + Name string `json:"name"` + Code int `json:"code"` +} + +// ResponseLatLng ... +type ResponseLatLng struct { + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` +} + +// WarehouseNatsResponse ... +type WarehouseNatsResponse struct { + ID string `json:"_id"` + Name string `json:"name"` + SearchString string `json:"searchString"` + Slug string `json:"slug"` + Status string `json:"status"` + Supplier string `json:"supplier"` + Contact ResponseWarehouseContact `json:"contact"` + Location ResponseWarehouseLocation `json:"location"` + Configurations WarehouseConfiguration `json:"configurations"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` +} diff --git a/subject/warehouse.go b/subject/warehouse.go index b0801b9..d4f8c53 100644 --- a/subject/warehouse.go +++ b/subject/warehouse.go @@ -11,9 +11,17 @@ var Warehouse = struct { UpdateOutboundRequestLogistic string CancelOutboundRequest string GetConfiguration string + FindOne string + FindByCondition string + Distinct string + Count string }{ CreateOutboundRequest: getWarehouseValue("create_outbound_request"), UpdateOutboundRequestLogistic: getWarehouseValue("update_outbound_request_logistic_info"), CancelOutboundRequest: getWarehouseValue("cancel_outbound_request"), GetConfiguration: getWarehouseValue("get_configuration"), + FindOne: getWarehouseValue("find_one"), + FindByCondition: getWarehouseValue("find_all_by_condition"), + Distinct: getWarehouseValue("distinct"), + Count: getWarehouseValue("count"), }