-
Notifications
You must be signed in to change notification settings - Fork 0
/
timetable.go
51 lines (44 loc) · 1.12 KB
/
timetable.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
package librus_api_go
import (
"encoding/json"
"sync"
)
// GetTimetableEntries returns all timetable entries since beginning of school year
func (l *Librus) GetTimetableEntries() ([]*TimetableEntry, error) {
res, err := l.GetData("TimetableEntries")
if err != nil {
return nil, err
}
defer res.Body.Close()
timetableEntriesResponse := new(TimetableEntriesResponse)
if err := json.NewDecoder(res.Body).Decode(timetableEntriesResponse); err != nil {
return nil, err
}
var wg sync.WaitGroup
var entries []*TimetableEntry
for _, te := range timetableEntriesResponse.Entries {
// get lesson
wg.Add(1)
entry := &TimetableEntry{}
entry.TimetableEntryDefault = te.TimetableEntryDefault
go func(lessonID int) {
defer wg.Done()
lesson, err := l.GetLesson(lessonID)
if err != nil {
return
}
// get subject
subject, err := l.GetSubject(lesson.Subject.ID)
if err != nil {
return
}
entry.Subject = subject
// get teacher
teacher, err := l.GetUser(lesson.Teacher.ID)
entry.Teacher = teacher
entries = append(entries, entry)
}(te.Lesson.ID)
}
wg.Wait()
return entries, nil
}