-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
school_calendar.go
79 lines (69 loc) · 1.76 KB
/
school_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
package main
import (
"context"
"sort"
"strconv"
)
type Module struct {
Name string `json:"module,omitempty"`
Start Date `json:"start,omitempty"`
End Date `json:"end,omitempty"`
Exceptions map[Day][]Date //`json:"-"`
Additions map[Day][]Date //`json:"-"`
}
func (m Module) addException(day Day, date Date) {
for _, d := range m.Exceptions[day] {
if d.Time.Equal(date.Time) {
return // Already exists
}
}
m.Exceptions[day] = append(m.Exceptions[day], date)
}
func GetSchoolCalendar(ctx context.Context, year int) ([]Module, error) {
var ms []Module
err := GetAPI(ctx, "/school-calendar/modules?year="+strconv.Itoa(year), &ms)
if err != nil {
return nil, err
}
for i, m := range ms {
m.Exceptions = make(map[Day][]Date, 7)
m.Additions = make(map[Day][]Date, 7)
ms[i] = m
}
var es []struct {
Date Date `json:"date,omitempty"`
EventType string `json:"eventType,omitempty"`
Description string `json:"description,omitempty"`
ChangeTo *Day `json:"changeTo,omitempty"`
}
err = GetAPI(ctx, "/school-calendar/events?year="+strconv.Itoa(year), &es)
if err != nil {
return nil, err
}
for _, e := range es {
if e.EventType == "Other" {
continue
}
for _, m := range ms {
if !e.Date.In(m.Start, m.End) {
continue
}
if e.EventType == "Exam" && m.Name != "SpringA" && m.Name != "FallA" {
continue
}
if e.ChangeTo != nil {
day := *e.ChangeTo
if day != e.Date.Day() { // Do not add exception and addition to same date
m.addException(e.Date.Day(), e.Date)
m.Additions[day] = append(m.Additions[day], e.Date)
}
} else {
m.addException(e.Date.Day(), e.Date)
}
}
}
sort.Slice(ms, func(i, j int) bool {
return ms[i].Start.Time.Before(ms[j].Start.Time)
})
return ms, nil
}