add get segment info

This commit is contained in:
anbuiselly 2022-11-22 05:43:02 +07:00
parent f8658523a2
commit 0a1c76bb88
5 changed files with 73 additions and 0 deletions

40
client/segment.go Normal file
View File

@ -0,0 +1,40 @@
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"
)
// Segment ...
type Segment struct{}
// GetSegment ...
func GetSegment() Segment {
return Segment{}
}
func (s Segment) GetListSegmentInfoByIds(p model.GetListSegmentRequest) (*model.ResponseListSegmentInfo, error) {
msg, err := natsio.GetServer().Request(subject.Segment.GetListSegmentInfo, toBytes(p))
if err != nil {
return nil, err
}
var r struct {
Data *model.ResponseListSegmentInfo `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
}

6
model/segment_request.go Normal file
View File

@ -0,0 +1,6 @@
package model
// GetListSegmentRequest ...
type GetListSegmentRequest struct {
SegmentIds []string `json:"segmentIds"`
}

12
model/segment_response.go Normal file
View File

@ -0,0 +1,12 @@
package model
// ResponseSegmentInfo ...
type ResponseSegmentInfo struct {
ID string `json:"_id"`
Name string `json:"name"`
}
// ResponseListSegmentInfo ...
type ResponseListSegmentInfo struct {
Segments []ResponseSegmentInfo `json:"segments"`
}

View File

@ -18,6 +18,7 @@ var prefixes = struct {
Notification string
SocialPost string
Staff string
Segment string
}{
Communication: "communication",
Order: "order",

14
subject/segment.go Normal file
View File

@ -0,0 +1,14 @@
package subject
import "fmt"
func getSegmentValue(val string) string {
return fmt.Sprintf("%s.%s", prefixes.Segment, val)
}
// Segment ...
var Segment = struct {
GetListSegmentInfo string
}{
GetListSegmentInfo: getSegmentValue("get_list_segment_info"),
}