Skip to content

Commit

Permalink
feat: add announcement
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip committed Jul 2, 2023
1 parent 0205da3 commit 9d05195
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
133 changes: 133 additions & 0 deletions announcement.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Client struct {
DocComments DocCommentService
Docs DocsService
Socials SocialsService
Announcements AnnouncementService

events map[string][]Event
commands map[string]Command
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion guildedgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

0 comments on commit 9d05195

Please sign in to comment.