-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalendar.go
227 lines (182 loc) · 7.41 KB
/
calendar.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package guildedgo
import (
"errors"
"net/url"
"strconv"
)
type CalendarEvent struct {
ID int `json:"id"`
ServerID string `json:"serverId"`
ChannelID string `json:"channelId"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Location string `json:"location,omitempty"`
URL string `json:"url,omitempty"`
Color int `json:"color,omitempty"`
RSVPLimit int `json:"rsvpLimit,omitempty"`
StartsAt string `json:"startsAt"`
Duration int `json:"duration,omitempty"`
IsPrivate bool `json:"isPrivate,omitempty"`
Mentions `json:"mentions,omitempty"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
Cancellation struct {
Description string `json:"description,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
} `json:"cancellation,omitempty"`
}
type CalendarEventRsvp struct {
CalendarEventID int `json:"calendarEventId"`
ChannelID string `json:"channelId"`
ServerID string `json:"serverId"`
UserID string `json:"userId"`
Status string `json:"status"`
CreatedBy string `json:"createdBy"`
CreatedAt string `json:"createdAt"`
UpdatedBy string `json:"updatedBy"`
UpdatedAt string `json:"updatedAt"`
}
type CalenderEventObject struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Location string `json:"location,omitempty"`
StartsAt string `json:"startsAt,omitempty"`
URL string `json:"url,omitempty"`
Color int `json:"color,omitempty"`
RSVPLimit int `json:"rsvpLimit,omitempty"`
Duration int `json:"duration,omitempty"`
IsPrivate bool `json:"isPrivate,omitempty"`
}
type CalendarEventResponse struct {
Event CalendarEvent `json:"calendarEvent"`
}
type CalendarEventsResponse struct {
Events []CalendarEvent `json:"calendarEvents"`
}
type CalendarEventRsvpResponse struct {
Rsvp CalendarEventRsvp `json:"calendarEventRsvp"`
}
type CalendarEventRsvpsResponse struct {
Rsvps []CalendarEventRsvp `json:"calendarEventRsvps"`
}
type calenderEndpoints struct{}
func (e *calenderEndpoints) Default(channelId string) string {
return guildedApi + "/channels/" + channelId + "/events"
}
func (e *calenderEndpoints) Get(channelId string, eventId int) string {
return guildedApi + "/channels/" + channelId + "/events/" + strconv.Itoa(eventId)
}
func (e *calenderEndpoints) RsvpDefault(channelId string, eventId int, userId string) string {
return guildedApi + "/channels/" + channelId + "/events/" + strconv.Itoa(eventId) + "/rsvps/" + userId
}
func (e *calenderEndpoints) Rsvps(channelId string, eventId int) string {
return guildedApi + "/channels/" + channelId + "/events/" + strconv.Itoa(eventId) + "/rsvps"
}
type calendarService struct {
client *Client
endpoints *calenderEndpoints
}
type CalendarService interface {
CreateEvent(channelId string, event *CalenderEventObject) (*CalendarEvent, error)
GetEvents(channelId string, options *GetEventsOptions) ([]CalendarEvent, error)
GetEvent(channelId string, eventId int) (*CalendarEvent, error)
UpdateEvent(channelId string, eventId int, event *CalenderEventObject) (*CalendarEvent, error)
DeleteEvent(channelId string, eventId int) error
GetEventRSVP(channelId string, eventId int, userId string) (*CalendarEventRsvp, error)
CreateOrUpdateEventRSVP(channelId string, eventId int, userId string) (*CalendarEventRsvp, error)
DeleteEventRSVP(channelId string, eventId int, userId string) error
GetEventRSVPs(channelId string, eventId int) ([]CalendarEventRsvp, error)
}
var _ CalendarService = &calendarService{
endpoints: &calenderEndpoints{},
}
func (service *calendarService) CreateEvent(channelId string, event *CalenderEventObject) (*CalendarEvent, error) {
var calendarEvent CalendarEventResponse
err := service.client.PostRequestV2(service.endpoints.Default(channelId), event, &calendarEvent)
if err != nil {
return nil, errors.New("Failed to create calendar event: " + err.Error())
}
return &calendarEvent.Event, nil
}
type GetEventsOptions struct {
Before string
After string
Limit int
}
func (service *calendarService) GetEvents(channelId string, options *GetEventsOptions) ([]CalendarEvent, error) {
var calendarEvents CalendarEventsResponse
url, err := url.Parse(service.endpoints.Default(channelId))
if err != nil {
return nil, err
}
values := url.Query()
if options.Before != "" {
values.Add("before", options.Before)
}
if options.After != "" {
values.Add("after", options.After)
}
if options.Limit != 0 {
values.Add("limit", strconv.Itoa(options.Limit))
}
url.RawQuery = values.Encode()
err = service.client.GetRequestV2(url.String(), &calendarEvents)
if err != nil {
return nil, errors.New("Failed to get calendar events: " + err.Error())
}
return calendarEvents.Events, nil
}
func (service *calendarService) GetEvent(channelId string, eventId int) (*CalendarEvent, error) {
var calendarEvent CalendarEventResponse
err := service.client.GetRequestV2(service.endpoints.Get(channelId, eventId), &calendarEvent)
if err != nil {
return nil, errors.New("Failed to get calendar event: " + err.Error())
}
return &calendarEvent.Event, nil
}
func (service *calendarService) UpdateEvent(channelId string, eventId int, event *CalenderEventObject) (*CalendarEvent, error) {
var calendarEvent CalendarEventResponse
err := service.client.PatchRequest(service.endpoints.Get(channelId, eventId), event, &calendarEvent)
if err != nil {
return nil, errors.New("Failed to update calendar event: " + err.Error())
}
return &calendarEvent.Event, nil
}
func (service *calendarService) DeleteEvent(channelId string, eventId int) error {
_, err := service.client.DeleteRequest(service.endpoints.Get(channelId, eventId))
if err != nil {
return errors.New("Failed to delete calendar event: " + err.Error())
}
return nil
}
func (service *calendarService) GetEventRSVP(channelId string, eventId int, userId string) (*CalendarEventRsvp, error) {
var calendarEventRsvp CalendarEventRsvpResponse
err := service.client.GetRequestV2(service.endpoints.RsvpDefault(channelId, eventId, userId), &calendarEventRsvp)
if err != nil {
return nil, errors.New("Failed to get calendar event rsvp: " + err.Error())
}
return &calendarEventRsvp.Rsvp, nil
}
func (service *calendarService) CreateOrUpdateEventRSVP(channelId string, eventId int, userId string) (*CalendarEventRsvp, error) {
var calendarEventRsvp CalendarEventRsvpResponse
err := service.client.GetRequestV2(service.endpoints.RsvpDefault(channelId, eventId, userId), &calendarEventRsvp)
if err != nil {
return nil, errors.New("Failed to create or update calendar event RSVP. Error: " + err.Error())
}
return &calendarEventRsvp.Rsvp, nil
}
func (service *calendarService) DeleteEventRSVP(channelId string, eventId int, userId string) error {
_, err := service.client.DeleteRequest(service.endpoints.RsvpDefault(channelId, eventId, userId))
if err != nil {
return errors.New("Failed to delete calendar event RSVP. Error: " + err.Error())
}
return nil
}
func (service *calendarService) GetEventRSVPs(channelId string, eventId int) ([]CalendarEventRsvp, error) {
var calendarEventRsvps CalendarEventRsvpsResponse
err := service.client.GetRequestV2(service.endpoints.Rsvps(channelId, eventId), &calendarEventRsvps)
if err != nil {
return nil, errors.New("Failed to get calendar event RSVPs. Error: " + err.Error())
}
return calendarEventRsvps.Rsvps, nil
}