/
course.go
141 lines (105 loc) · 2.96 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package database
import (
"github.com/jmoiron/sqlx"
)
func makeCourseTable(db *sqlx.DB) error {
_, err := db.Exec("CREATE TABLE IF NOT EXISTS courses (sertitle TEXT NOT NULL PRIMARY KEY, title TEXT NOT NULL, inst TEXT NOT NULL, description TEXT NOT NULL, comments TEXT NOT NULL)")
return err
}
type Course struct {
Title string // The title of the course
SerTitle string // The serialized version of the course title
Inst string // The institution that offers the course
Description string // The description of the course
Comments string // My personal comments on the course
}
// Making a new course object
func MakeNewCourse(sertitle string, title string, inst string, description string, comments string) *Course {
return &Course{
SerTitle: sertitle,
Title: title,
Inst: inst,
Description: description,
Comments: comments,
}
}
// Getting all of the courses
func GetCourses(db *sqlx.DB) ([]*Course, error) {
rows, err := db.Query("SELECT * FROM courses")
if err != nil {
return nil, err
}
courses := make([]*Course, 0)
var title string
var sertitle string
var inst string
var description string
var comments string
for rows.Next() {
err = rows.Scan(&sertitle, &title, &inst, &description, &comments)
if err != nil {
return nil, err
}
courses = append(courses, MakeNewCourse(sertitle, title, inst, description, comments))
}
return courses, nil
}
// Quickly getting all of the courses
func QuickGetCourses() []*Course {
db := QuickOpenDB()
defer db.Close()
courses, err := GetCourses(db)
if err != nil {
panic(err)
}
return courses
}
// Getting a course by its serialized title
func GetCourseBySerTitle(db *sqlx.DB, sertitle string) (*Course, error) {
stmt, err := db.Prepare("SELECT * FROM courses WHERE sertitle = $1")
if err != nil {
return nil, err
}
row := stmt.QueryRow(sertitle)
if row == nil {
return nil, RowDoesNotExistError
}
var title string
var nsertitle string
var inst string
var description string
var comments string
err = row.Scan(&nsertitle, &title, &inst, &description, &comments)
if err != nil {
return nil, err
}
return MakeNewCourse(nsertitle, title, inst, description, comments), nil
}
// Quickly getting a course by its serialized title
func QuickGetCourseBySerTitle(sertitle string) *Course {
db := QuickOpenDB()
defer db.Close()
course, err := GetCourseBySerTitle(db, sertitle)
if err != nil {
panic(err)
}
return course
}
// Inserting a course
func InsertCourse(db *sqlx.DB, course *Course) error {
stmt, err := db.Prepare("INSERT INTO courses(sertitle, title, inst, description, comments) values($1, $2, $3, $4, $5)")
if err != nil {
return err
}
_, err = stmt.Exec(course.SerTitle, course.Title, course.Inst, course.Description, course.Comments)
return err
}
// Quickly inserting a course
func QuickInsertCourse(course *Course) {
db := QuickOpenDB()
defer db.Close()
err := InsertCourse(db, course)
if err != nil {
panic(err)
}
}