From 9d05195f55964a737933b9d7d32011a5b218721c Mon Sep 17 00:00:00 2001 From: itschip Date: Sun, 2 Jul 2023 02:11:09 +0200 Subject: [PATCH] feat: add announcement --- announcement.go | 133 ++++++++++++++++++++++++++++++++++++++++++++++ client.go | 2 + guildedgo_test.go | 5 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 announcement.go diff --git a/announcement.go b/announcement.go new file mode 100644 index 0000000..8b70070 --- /dev/null +++ b/announcement.go @@ -0,0 +1,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 +} diff --git a/client.go b/client.go index e48a49b..cd4f48d 100644 --- a/client.go +++ b/client.go @@ -25,6 +25,7 @@ type Client struct { DocComments DocCommentService Docs DocsService Socials SocialsService + Announcements AnnouncementService events map[string][]Event commands map[string]Command @@ -61,6 +62,7 @@ func NewClient(config *Config) *Client { c.Docs = &docsService{client: c} c.DocComments = &docCommentService{client: c} c.Socials = &socialsService{client: c} + c.Announcements = &announcementService{client: c} c.events = make(map[string][]Event) diff --git a/guildedgo_test.go b/guildedgo_test.go index 06df8f3..57eca81 100644 --- a/guildedgo_test.go +++ b/guildedgo_test.go @@ -17,5 +17,8 @@ func TestNewClient(t *testing.T) { c := NewClient(config) - c.Channel.CreateChannel(&NewChannelObject{}) + c.Announcements.GetAnnouncements("123", &GetAnnouncementParams{ + Before: "123", + Limit: 1, + }) }