Files
helioscal/class.go
2020-09-14 23:25:08 +00:00

78 lines
1.3 KiB
Go

package main
import (
"fmt"
"log"
"time"
"google.golang.org/api/calendar/v3"
)
type Class struct {
Summary string
Start string
End string
Students []string
Days []time.Weekday
Tags map[string]string
Zoom string
}
func (c Class) happensOnDay(t time.Time) bool {
for _, day := range c.Days {
if day == t.Weekday() {
return true
}
}
return false
}
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
}
func (c Class) buildEvent(t time.Time) (*calendar.Event, error) {
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{},
}
if len(c.Students) == 0 {
return nil, fmt.Errorf("class with no students: %s", c.Summary)
}
for _, student := range c.Students {
ev.Attendees = append(
ev.Attendees,
&calendar.EventAttendee{
Email: student,
},
)
}
if c.Zoom != "" {
ev.Description = fmt.Sprintf(`Zoom: %s`, c.Zoom)
}
return ev, nil
}