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
|
package api
import (
"context"
"fmt"
"time"
"task-dashboard/internal/models"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
type GoogleCalendarClient struct {
srv *calendar.Service
calendarID string
}
func NewGoogleCalendarClient(ctx context.Context, credentialsFile, calendarID string) (*GoogleCalendarClient, error) {
srv, err := calendar.NewService(ctx, option.WithCredentialsFile(credentialsFile))
if err != nil {
return nil, fmt.Errorf("unable to retrieve Calendar client: %v", err)
}
return &GoogleCalendarClient{
srv: srv,
calendarID: calendarID,
}, nil
}
func (c *GoogleCalendarClient) GetUpcomingEvents(ctx context.Context, maxResults int) ([]models.CalendarEvent, error) {
t := time.Now().Format(time.RFC3339)
events, err := c.srv.Events.List(c.calendarID).ShowDeleted(false).
SingleEvents(true).TimeMin(t).MaxResults(int64(maxResults)).OrderBy("startTime").Do()
if err != nil {
return nil, fmt.Errorf("unable to retrieve events: %v", err)
}
var calendarEvents []models.CalendarEvent
for _, item := range events.Items {
var start, end time.Time
if item.Start.DateTime == "" {
// All-day event
start, _ = time.Parse("2006-01-02", item.Start.Date)
end, _ = time.Parse("2006-01-02", item.End.Date)
} else {
start, _ = time.Parse(time.RFC3339, item.Start.DateTime)
end, _ = time.Parse(time.RFC3339, item.End.DateTime)
}
calendarEvents = append(calendarEvents, models.CalendarEvent{
ID: item.Id,
Summary: item.Summary,
Description: item.Description,
Start: start,
End: end,
HTMLLink: item.HtmlLink,
})
}
return calendarEvents, nil
}
|