/
courses.go
79 lines (74 loc) · 2.13 KB
/
courses.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 tum
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/TUM-Dev/CampusProxy/client"
"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model/search"
"github.com/TUM-Dev/gocast/tools"
)
// PrefetchCourses loads all courses from tumonline, so we can use them in the course creation from search
func PrefetchCourses(dao dao.DaoWrapper) func() {
return func() {
client, err := tools.Cfg.GetMeiliClient()
if err != nil {
logger.Info("Skipping course prefetching, reason: ", "err", err)
return
}
if tools.Cfg.Campus.CampusProxy == nil || tools.Cfg.Campus.RelevantOrgs == nil {
return
}
var res []*search.PrefetchedCourse
for _, org := range *tools.Cfg.Campus.RelevantOrgs {
r, err := getCoursesForOrg(org)
if err != nil {
logger.Error("Error getting courses for organisation "+org, "err", err)
} else {
res = append(res, r...)
}
}
index := client.Index("PREFETCHED_COURSES")
_, err = index.AddDocuments(&res, "courseID")
logger.Info(string(rune(len(res))))
if err != nil {
logger.Error("issue adding documents to meili", "err", err)
}
}
}
func getCoursesForOrg(org string) ([]*search.PrefetchedCourse, error) {
conf := client.NewConfiguration()
conf.Host = "campus-proxy.mm.rbg.tum.de"
conf.Scheme = "https"
c := client.NewAPIClient(conf)
courses, _, err := c.OrganizationApi.
OrganizationCoursesGet(context.WithValue(context.Background(), client.ContextAPIKeys, map[string]client.APIKey{"ApiKeyAuth": {Key: tools.Cfg.Campus.Tokens[0]}})).
IncludeChildren(true).
OrgUnitID(org).
Execute()
if err.Error() != "" {
return nil, fmt.Errorf("load Course: %v", err.Error())
}
var res []*search.PrefetchedCourse
for _, c := range courses {
t := "W"
if strings.Contains(c.GetTeachingTerm(), "Sommer") {
t = "S"
}
if !strings.Contains(c.GetTeachingTerm(), " ") {
continue
}
y, err := strconv.Atoi(strings.Split(strings.Split(c.GetTeachingTerm(), " ")[1], "/")[0])
if err != nil {
continue
}
res = append(res, &search.PrefetchedCourse{
Name: c.CourseName.GetText(),
CourseID: c.GetCourseId(),
Term: t,
Year: y,
})
}
return res, nil
}