-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (93 loc) · 2.58 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"os"
)
// Course is struct for a course entry
type Course struct {
Campus string `json:"campus"`
Code string `json:"code"`
Credits float32 `json:"credits"`
Title string `json:"title"`
}
func main() {
// Open Connection
uri := os.Getenv("CONNECT_STR")
clientOptions := options.Client().ApplyURI(uri)
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
panic(err)
}
// Connect to db
db := client.Database("HyperPlanner")
courses := db.Collection("Courses")
// Insert course
// testCourse := Course{"hmc", "CSCI134", 3.0, "Operating Systems"}
// insertCourse(courses, testCourse)
// Read course
// var course bson.M
// readCourse(courses, course)
var allCourses []Course
allFilter := bson.M{"campus": "hmc"}
readCoursesFilter(courses, allCourses, allFilter)
var codeCourses []Course
codeFilter := bson.M{"code": "CSCI134"}
readCoursesFilter(courses, codeCourses, codeFilter)
var titleCourses []Course
titleFilter := bson.M{"title": "Software Development"}
readCoursesFilter(courses, titleCourses, titleFilter)
var creditCourses []Course
creditFilter := bson.M{"credits": 3.0}
readCoursesFilter(courses, creditCourses, creditFilter)
}
// Insert given course into db
func insertCourse(courses *mongo.Collection, course Course) {
result, err := courses.InsertOne(
context.Background(),
course)
if err != nil {
log.Fatal(err)
}
fmt.Println("API Result:", result.InsertedID)
}
// Read single course from db
func readCourse(courses *mongo.Collection, course bson.M, filter bson.M) {
err := courses.FindOne(context.Background(), filter).Decode(&course)
if err != nil {
log.Fatal(err)
}
fmt.Println("Found single document:", course)
}
// Read all courses from db and output as json string
func readCoursesFilter(courses *mongo.Collection, results []Course, filter bson.M) {
cursor, err := courses.Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
// Loop through all courses in cursor and append to results
total := 0
for cursor.Next(context.Background()) {
var course Course
if err := cursor.Decode(&course); err != nil {
log.Fatal(err)
}
total++
results = append(results, course)
}
if err = cursor.Err(); err != nil {
log.Fatal(err)
}
cursor.Close(context.Background())
ret, err := json.Marshal(results)
if err != nil {
log.Fatal(err)
}
fmt.Println("Found documents: ", total)
fmt.Println(string(ret))
}