From 0a1c76bb88ad7375cfdbdf40419dd5c4e1e5abe0 Mon Sep 17 00:00:00 2001 From: anbuiselly <105765792+anbuiselly@users.noreply.github.com> Date: Tue, 22 Nov 2022 05:43:02 +0700 Subject: [PATCH] add get segment info --- client/segment.go | 40 +++++++++++++++++++++++++++++++++++++++ model/segment_request.go | 6 ++++++ model/segment_response.go | 12 ++++++++++++ subject/config.go | 1 + subject/segment.go | 14 ++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 client/segment.go create mode 100644 model/segment_request.go create mode 100644 model/segment_response.go create mode 100644 subject/segment.go diff --git a/client/segment.go b/client/segment.go new file mode 100644 index 0000000..ec937c8 --- /dev/null +++ b/client/segment.go @@ -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 +} diff --git a/model/segment_request.go b/model/segment_request.go new file mode 100644 index 0000000..138f347 --- /dev/null +++ b/model/segment_request.go @@ -0,0 +1,6 @@ +package model + +// GetListSegmentRequest ... +type GetListSegmentRequest struct { + SegmentIds []string `json:"segmentIds"` +} diff --git a/model/segment_response.go b/model/segment_response.go new file mode 100644 index 0000000..84a1571 --- /dev/null +++ b/model/segment_response.go @@ -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"` +} diff --git a/subject/config.go b/subject/config.go index 6988929..0df1b9e 100644 --- a/subject/config.go +++ b/subject/config.go @@ -18,6 +18,7 @@ var prefixes = struct { Notification string SocialPost string Staff string + Segment string }{ Communication: "communication", Order: "order", diff --git a/subject/segment.go b/subject/segment.go new file mode 100644 index 0000000..54d5b7e --- /dev/null +++ b/subject/segment.go @@ -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"), +}