courses.go 3.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
canvasctl: Use Canvas LMS from command line
    Copyright (C) 2020 Marcus Gelderie

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
package canvas

import (
	"fmt"
Marcus Gelderie's avatar
Marcus Gelderie committed
22
	"github.com/sirupsen/logrus"
23
24
25
	"gitlab.com/mgelde/canvasctl/rest"
)

Marcus Gelderie's avatar
Marcus Gelderie committed
26
type CanvasCourse interface {
27
	AsName() string
28
	AsId() (rest.CanvasId, error)
29
30
}

Marcus Gelderie's avatar
Marcus Gelderie committed
31
type integerCourseId struct {
32
	Id   rest.CanvasId
33
	API  rest.API
34
	name string
35
36
}

Marcus Gelderie's avatar
Marcus Gelderie committed
37
func (i integerCourseId) AsId() (rest.CanvasId, error) {
38
39
	return i.Id, nil
}
40
41
42
43

const UnknownCourse = "UNKNOWN"

func (i integerCourseId) AsName() string {
44
45
46
47
	if i.name == "" {
		id, _ := i.AsId() // cannot fail
		course, err := i.API.GetSingleCourse(id)
		if err != nil {
48
			if rest.NeedsAuthentication(err) {
49
				return UnknownCourse
50
			}
Marcus Gelderie's avatar
Marcus Gelderie committed
51
			logrus.Error(err)
52
			return UnknownCourse
53
54
55
		}
		i.name = course.Name
	}
56
	return i.name
57
58
}

Marcus Gelderie's avatar
Marcus Gelderie committed
59
type stringCourseId struct {
60
	Name string
61
	API  rest.API
62
63
}

64
65
func (i stringCourseId) AsName() string {
	return i.Name
66
}
67

Marcus Gelderie's avatar
Marcus Gelderie committed
68
func (i stringCourseId) AsId() (rest.CanvasId, error) {
Marcus Gelderie's avatar
Marcus Gelderie committed
69
	logrus.Info("stringCourseId. Have no integer ID yet. Performing lookup.")
70
	courses, err := i.API.GetCourses()
71
	if err != nil {
72
73
74
		if rest.NeedsAuthentication(err) {
			return 0, fmt.Errorf("Error. Not authenticated. Is your token valid?")
		}
75
76
77
78
		return 0, err
	}
	for _, course := range courses {
		if course.Name == i.Name {
Marcus Gelderie's avatar
Marcus Gelderie committed
79
			logrus.Debugf("Success... Found course with id %d", course.Id)
80
81
82
83
84
85
			return course.Id, nil
		}
	}
	return 0, fmt.Errorf("No course with name %s found.", i.Name)
}

86
func CourseFromId(api rest.API, id rest.CanvasId) CanvasCourse {
Marcus Gelderie's avatar
Marcus Gelderie committed
87
88
89
90
91
92
	return &integerCourseId{
		Id:  id,
		API: api,
	}
}

93
func CourseFromName(api rest.API, name string) CanvasCourse {
Marcus Gelderie's avatar
Marcus Gelderie committed
94
95
96
97
98
99
	return &stringCourseId{
		Name: name,
		API:  api,
	}
}

100
101
func (ctx *Context) ListCourses() error {
	if ctx == nil {
102
		panic("Context in ListCourses() is nil")
103
104
	}
	courses, err := ctx.API.GetCourses()
105
	if err != nil {
106
107
108
		if rest.NeedsAuthentication(err) {
			return fmt.Errorf("Error. Not authenticated. Is your token valid?")
		}
109
110
111
112
113
114
115
116
		return err
	}
	for _, c := range courses {
		fmt.Println(c)
	}
	return nil
}

117
func (ctx *CourseContext) ShowCourse() error {
118
	if ctx == nil {
119
		panic("Context in ShowCourse() is nil")
120
	}
121
	course := ctx.Course()
122
	id, err := course.AsId()
123
124
125
	if err != nil {
		return err
	}
126
	courseDetail, err := ctx.API.GetSingleCourse(id)
127
	if err != nil {
128
129
130
		if rest.NeedsAuthentication(err) {
			return fmt.Errorf("Error. Not authenticated. Is your token valid?")
		}
Marcus Gelderie's avatar
Marcus Gelderie committed
131
		logrus.Error("Error getting single course:", err)
132
133
134
		return err
	}
	fmt.Println("Course:\n--------")
Marcus Gelderie's avatar
Marcus Gelderie committed
135
136
137
138
	fmt.Println("- Name:", courseDetail.Name)
	fmt.Println("- Id:", courseDetail.Id)
	fmt.Println("- Code:", courseDetail.Code)
	fmt.Println("- Started:", courseDetail.StartedAt.Local())
139
	return nil
140
}