-
Notifications
You must be signed in to change notification settings - Fork 1
/
announcement.go
133 lines (102 loc) · 3.45 KB
/
announcement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package guildedgo
import (
"net/url"
"strconv"
)
type Announcement struct {
ID string `json:"id"`
ServerID string `json:"serverId"`
ChannelID string `json:"channelId"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
Content string `json:"content"`
Mentions Mentions `json:"mentions,omitempty"`
Title string `json:"title"`
}
type GetAnnouncementParams struct {
Before string
Limit int
}
type AnncounmentResponse struct {
Announcement `json:"announcement"`
}
type AnnouncementService interface {
CreateAnnouncement(serverID string, channelID string, title string, content string) (*Announcement, error)
GetAnnouncements(channelID string, query *GetAnnouncementParams) ([]Announcement, error)
ReadAnnouncement(channelID string, announcementID string) (*Announcement, error)
UpdateAnnouncement(channelID string, announcementID string, title string, content string) (*Announcement, error)
DeleteAnnouncement(channelID string, announcementID string) error
}
type announcementEndpoints struct{}
func (endpoints *announcementEndpoints) Get(channelID string) string {
return guildedApi + "/channels/" + channelID + "/announcements"
}
type announcementService struct {
client *Client
endpoints *announcementEndpoints
}
var _ AnnouncementService = &announcementService{}
func (service *announcementService) CreateAnnouncement(serverID string, channelID string, title string, content string) (*Announcement, error) {
endpoint := service.endpoints.Get(channelID)
body := map[string]interface{}{
"title": title,
"content": content,
}
var response AnncounmentResponse
err := service.client.PostRequestV2(endpoint, body, &response)
if err != nil {
return nil, err
}
return &response.Announcement, nil
}
func (service *announcementService) GetAnnouncements(channelID string, query *GetAnnouncementParams) ([]Announcement, error) {
endpoint := service.endpoints.Get(channelID)
params := url.Values{}
if query != nil {
if query.Before != "" {
params.Add("before", query.Before)
}
if query.Limit != 0 {
params.Add("limit", strconv.Itoa(query.Limit))
}
}
endpoint = endpoint + "?" + params.Encode()
var response struct {
Announcements []Announcement `json:"announcements"`
}
err := service.client.GetRequestV2(endpoint, &response)
if err != nil {
return nil, err
}
return response.Announcements, nil
}
func (service *announcementService) ReadAnnouncement(channelID string, announcementID string) (*Announcement, error) {
endpoint := service.endpoints.Get(channelID) + "/" + announcementID
var response AnncounmentResponse
err := service.client.GetRequestV2(endpoint, &response)
if err != nil {
return nil, err
}
return &response.Announcement, nil
}
func (service *announcementService) UpdateAnnouncement(channelID string, announcementID string, title string, content string) (*Announcement, error) {
endpoint := service.endpoints.Get(channelID) + "/" + announcementID
body := map[string]interface{}{
"title": title,
"content": content,
}
var response AnncounmentResponse
err := service.client.PatchRequest(endpoint, body, &response)
if err != nil {
return nil, err
}
return &response.Announcement, nil
}
func (service *announcementService) DeleteAnnouncement(channelID string, announcementID string) error {
endpoint := service.endpoints.Get(channelID) + "/" + announcementID
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return err
}
return nil
}