Files
helioscal/class.go

78 lines
1.3 KiB
Go
Raw Permalink Normal View History

2020-09-09 04:03:49 +00:00
package main
import (
"fmt"
2020-09-13 21:27:40 +00:00
"log"
2020-09-09 04:03:49 +00:00
"time"
"google.golang.org/api/calendar/v3"
)
type Class struct {
Summary string
Start string
End string
Students []string
Days []time.Weekday
2020-09-13 21:27:40 +00:00
Tags map[string]string
2020-09-09 04:03:49 +00:00
Zoom string
}
func (c Class) happensOnDay(t time.Time) bool {
for _, day := range c.Days {
if day == t.Weekday() {
return true
}
}
return false
}
2020-09-13 21:27:40 +00:00
func (c Class) tagsMatch(t time.Time) bool {
for key, value := range c.Tags {
tagValue, err := getTagAt(key, t)
if err != nil {
log.Fatal(err)
}
if value != tagValue {
return false
}
}
return true
}
2020-09-14 23:25:08 +00:00
func (c Class) buildEvent(t time.Time) (*calendar.Event, error) {
2020-09-09 04:03:49 +00:00
dateStr := t.Format("2006-01-02")
ev := &calendar.Event{
Summary: c.Summary,
Start: &calendar.EventDateTime{
DateTime: fmt.Sprintf("%sT%s:00", dateStr, c.Start),
TimeZone: "US/Pacific",
},
End: &calendar.EventDateTime{
DateTime: fmt.Sprintf("%sT%s:00", dateStr, c.End),
TimeZone: "US/Pacific",
},
Attendees: []*calendar.EventAttendee{},
}
2020-09-14 23:25:08 +00:00
if len(c.Students) == 0 {
return nil, fmt.Errorf("class with no students: %s", c.Summary)
2020-09-09 04:03:49 +00:00
}
2020-09-14 23:25:08 +00:00
for _, student := range c.Students {
2020-09-09 04:03:49 +00:00
ev.Attendees = append(
ev.Attendees,
&calendar.EventAttendee{
Email: student,
},
)
}
if c.Zoom != "" {
ev.Description = fmt.Sprintf(`Zoom: %s`, c.Zoom)
}
2020-09-14 23:25:08 +00:00
return ev, nil
2020-09-09 04:03:49 +00:00
}