Merge pull request #13 from Selly-Modules/warehouse-nats

Warehouse nats
This commit is contained in:
trunglt251292 2022-08-30 14:47:51 +07:00 committed by GitHub
commit 9e645a60c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 165 additions and 0 deletions

View File

@ -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
}

85
client/warehouse_dao.go Normal file
View File

@ -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
}

2
go.sum
View File

@ -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=

17
model/common_request.go Normal file
View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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"),
}