-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse.go
90 lines (85 loc) · 2.67 KB
/
course.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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"encoding/json"
"strconv"
)
type CourseSchedule struct {
Module string `json:"module,omitempty"`
Day Day `json:"day,omitempty"`
Period int `json:"period,omitempty"`
Room string `json:"room,omitempty"`
}
type Course struct {
ID string
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
Methods []string `json:"methods,omitempty"`
Schedules []CourseSchedule `json:"schedules,omitempty"`
Tags []string `json:"tags,omitempty"`
Memo string `json:"memo,omitempty"`
}
func GetCourses(ctx context.Context, year int) ([]Course, error) {
var data []struct {
ID string `json:"id"`
UserID string `json:"userId"`
Course struct {
ID string `json:"id"`
Year int `json:"year"`
Code string `json:"code"`
Name string `json:"name"`
Instructor string `json:"instructor"`
Credit json.Number `json:"credit"`
Overview string `json:"overview"`
Remarks string `json:"remarks"`
RecommendedGrades []int `json:"recommendedGrades"`
Methods []string `json:"methods"`
Schedules []CourseSchedule `json:"schedules"`
Isannual bool `json:"isAnnual"`
HasParseError bool `json:"hasParseError"`
} `json:"course"`
Year int `json:"year"`
Name *string `json:"name"`
Instructor *string `json:"instructor"`
Credit *json.Number `json:"credit"`
Methods *[]string `json:"methods"`
Schedules *[]CourseSchedule `json:"schedules"`
Memo string `json:"memo"`
Attendance int `json:"attendance"`
Absence int `json:"absence"`
Late int `json:"late"`
Tags []struct {
ID string `json:"id"`
} `json:"tags"`
}
err := GetAPI(ctx, "/registered-courses?year="+strconv.Itoa(year), &data)
if err != nil {
return nil, err
}
result := make([]Course, len(data))
for i, v := range data {
c := Course{
ID: v.ID,
Code: v.Course.Code,
Name: v.Course.Name,
Methods: v.Course.Methods,
Schedules: v.Course.Schedules,
Tags: make([]string, len(v.Tags)),
Memo: v.Memo,
}
if v.Name != nil {
c.Name = *v.Name
}
if v.Methods != nil {
c.Methods = *v.Methods
}
if v.Schedules != nil {
c.Schedules = *v.Schedules
}
for i, t := range v.Tags {
c.Tags[i] = t.ID
}
result[i] = c
}
return result, nil
}