forked from dinofizz/todo-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
131 lines (106 loc) · 2.83 KB
/
mongodb.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
package main
// Most of this is code from https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)
type mongodb struct {
client *mongo.Client
collection *mongo.Collection
connectionString string
}
func (m *mongodb) init() {
clientOptions := options.Client().ApplyURI(m.connectionString)
var err error
m.client, err = mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
err = m.client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
m.collection = m.client.Database("todo").Collection("todo_items")
}
func (m *mongodb) createItem(item Item) (Item, error) {
insertResult, err := m.collection.InsertOne(context.TODO(), item)
if err != nil {
return Item{}, errors.New("Unable to insert item into database.")
}
item.Id = insertResult.InsertedID.(primitive.ObjectID).Hex()
return item, nil
}
func (m *mongodb) deleteItem(id string) error {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return errors.New("Invalid ID.")
}
filter := bson.D{{"_id", objID}}
_, err = m.collection.DeleteOne(context.TODO(), filter)
if err != nil {
return &ErrorItemNotFound{Id: id}
}
return nil
}
func (m *mongodb) updateItem(id string, td Item) (Item, error) {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return Item{}, errors.New("Invalid ID.")
}
filter := bson.D{{"_id", objID}}
update := bson.D{
{"$set", bson.M{"Description": td.Description, "Completed": td.Completed}},
}
_, err = m.collection.UpdateOne(context.TODO(), filter, update)
if err != nil {
return Item{}, &ErrorItemNotFound{Id: id}
}
td.Id = id
return td, nil
}
func (m *mongodb) getItem(id string) (Item, error) {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return Item{}, errors.New("Invalid ID.")
}
filter := bson.D{{"_id", objID}}
var item Item
err = m.collection.FindOne(context.TODO(), filter).Decode(&item)
if err != nil {
return Item{}, &ErrorItemNotFound{Id: id}
}
item.Id = id
return item, nil
}
func (m *mongodb) allItems() ([]Item, error) {
findOptions := options.Find()
var results []Item
var emptyResults []Item
cur, err := m.collection.Find(context.TODO(), bson.D{{}}, findOptions)
if err != nil {
return emptyResults, err
}
for cur.Next(context.TODO()) {
var elem Item
err := cur.Decode(&elem)
if err != nil {
log.Fatal(err)
}
elements, _ := cur.Current.Elements()
elem.Id = elements[0].Value().ObjectID().Hex()
results = append(results, elem)
}
if err := cur.Err(); err != nil {
return emptyResults, err
}
cur.Close(context.TODO())
return results, err
}
func (m *mongodb) close() {
m.client.Disconnect(context.TODO())
}