-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrades.go
88 lines (71 loc) · 1.75 KB
/
grades.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
package librus_api_go
import (
"encoding/json"
"strconv"
"sync"
)
// GetUserGrades returns user grades
func (l *Librus) GetUserGrades() ([]*GradeDetails, error) {
res, err := l.GetData("Grades")
if err != nil {
return nil, err
}
defer res.Body.Close()
// grades response
gradesResponse := new(GradesResponse)
err = json.NewDecoder(res.Body).Decode(gradesResponse)
if err != nil {
return nil, err
}
var wg sync.WaitGroup
// detailed grades slice
var detailedGrades []*GradeDetails
// get grade subject
for _, g := range *gradesResponse.Grades {
grade := new(GradeDetails)
wg.Add(1)
// set default grades values
grade.GradeDefault = g.GradeDefault
// get subject
go func(subjectID, categoryID, addedByID int, grade *GradeDetails) {
defer wg.Done()
// SUBJECT
subject, err := l.GetSubject(subjectID)
if err != nil {
return
}
grade.Subject = subject // set grade subject
// CATEGORY
category, err := l.GetGradeCategory(categoryID)
if err != nil {
return
}
grade.Category = category // set grade category
// ADDED BY
addedBy, err := l.GetUser(addedByID)
if err != nil {
return
}
grade.AddedBy = addedBy
// append grade
detailedGrades = append(detailedGrades, grade)
}(g.Subject.ID, g.Category.ID, g.AddedBy.ID, grade)
}
wg.Wait()
return detailedGrades, nil
}
// GetGradeCategory name
func (l *Librus) GetGradeCategory(id int) (string, error) {
res, err := l.GetData("Grades/Categories/" + strconv.Itoa(id))
if err != nil {
return "", err
}
defer res.Body.Close()
// get category data
categoryResponse := new(CategoryResponse)
err = json.NewDecoder(res.Body).Decode(categoryResponse)
if err != nil {
return "", err
}
return categoryResponse.Category.Name, nil
}