[Update] Response search data #2

Merged
trunglt251292 merged 1 commits from v1 into master 2022-03-10 13:51:00 +00:00
2 changed files with 33 additions and 14 deletions
Showing only changes of commit dc6db8daa9 - Show all commits

View File

@ -42,8 +42,12 @@ func NewClient(config Config) (*Client, error) {
func (c *Client) SyncData(data SyncData) (bool, error) {
var (
res Response
req = RequestBody{
ApiKey: c.Config.ApiKey,
Body: toBytes(data),
}
)
msg, err := c.natsServer.Request(SubjectSyncData, toBytes(data))
msg, err := c.natsServer.Request(SubjectSyncData, toBytes(req))
if err != nil {
return false, err
}
@ -58,40 +62,48 @@ func (c *Client) SyncData(data SyncData) (bool, error) {
// Search
// Request search to service es
func (c *Client) Search(query ESQuery) ([]string, error) {
func (c *Client) Search(query ESQuery) (*Response, error) {
var (
res Response
req = RequestBody{
ApiKey: c.Config.ApiKey,
Body: toBytes(query),
}
res *Response
)
msg, err := c.natsServer.Request(SubjectSearch, toBytes(query))
msg, err := c.natsServer.Request(SubjectSearch, toBytes(req))
if err != nil {
return res.Data, err
return nil, err
}
if err = json.Unmarshal(msg.Data, &res); err != nil {
return res.Data, err
return nil, err
}
if res.Message != "" {
return res.Data, errors.New(res.Message)
return nil, errors.New(res.Message)
}
return res.Data, nil
return res, nil
}
// UpdateDocument
// Insert or update document to ES
func (c *Client) UpdateDocument(query UpdateDataPayload) ([]string, error) {
func (c *Client) UpdateDocument(query UpdateDataPayload) (bool, error) {
var (
req = RequestBody{
ApiKey: c.Config.ApiKey,
Body: toBytes(query),
}
res Response
)
msg, err := c.natsServer.Request(SubjectUpdateDocument, toBytes(query))
msg, err := c.natsServer.Request(SubjectUpdateDocument, toBytes(req))
if err != nil {
return res.Data, err
return false, err
}
if err = json.Unmarshal(msg.Data, &res); err != nil {
return res.Data, err
return false, err
}
if res.Message != "" {
return res.Data, errors.New(res.Message)
return res.Success, errors.New(res.Message)
}
return res.Data, nil
return res.Success, nil
}
func toBytes(data interface{}) []byte {

View File

@ -2,6 +2,12 @@ package elasticsearch
import "time"
// RequestBody ...
type RequestBody struct {
ApiKey string `json:"apiKey"`
Body []byte `json:"body"`
}
// Response
// response to service es
type Response struct {
@ -50,6 +56,7 @@ type ESQuery struct {
PaymentMethod string
Source string
FromNewActiveSeller string
FromNewActiveBuyer string
EmailStatus string
MerchantStatus string
IsCalled string