/
iolayer.go
167 lines (153 loc) · 5.03 KB
/
iolayer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package models
import (
"context"
"log"
"encoding/json" // Use JSON encoding for bson. M string
"go.mongodb.org/mongo-driver/bson"
)
// Trainer struct represent a trainer details
type Trainer struct {
Name string
Age int
City string
}
// InsertOneDoc insert a new document
func InsertOneDoc(trainer Trainer, dbName, collectionName string) interface{} {
collection := Connclient.Database(dbName).Collection(collectionName)
insertResult, err := collection.InsertOne(context.TODO(), trainer)
if err != nil {
log.Fatalln("Error on inserting new trainer", err)
} else {
log.Println("Trainer inserted sucessfully: ", insertResult)
}
return insertResult.InsertedID
}
// InsertManyDoc insert a new document
func InsertManyDoc(trainer []interface{}, dbName, collectionName string) interface{} {
collection := Connclient.Database(dbName).Collection(collectionName)
insertResult, err := collection.InsertMany(context.TODO(), trainer)
if err != nil {
log.Fatalln("Error on inserting new trainer", err)
} else {
log.Println("Trainer inserted sucessfully: ", insertResult)
}
return insertResult.InsertedIDs
}
// FindOneDoc will retune find results
func FindOneDoc(dbName, collectionName, query string) Trainer {
var trainer Trainer
var err error
var bsonMap bson.M
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(query), &bsonMap)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
collection := Connclient.Database(dbName).Collection(collectionName)
documentReturned := collection.FindOne(context.TODO(), bsonMap)
documentReturned.Decode(&trainer)
return trainer
}
// FindAllDocs return all documents from the collection Heroes
func FindAllDocs(dbName, collectionName, query string) []*Trainer {
var trainers []*Trainer
var err error
var bsonMap bson.M
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(query), &bsonMap)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
collection := Connclient.Database(dbName).Collection(collectionName)
cur, err := collection.Find(context.TODO(), bsonMap)
if err != nil {
log.Fatal("Error on Finding all the documents", err)
}
for cur.Next(context.TODO()) {
var trainer Trainer
err = cur.Decode(&trainer)
if err != nil {
log.Fatal("Error on Decoding the document", err)
}
trainers = append(trainers, &trainer)
}
return trainers
}
// UpdateOneDoc update the info of a informed Hero
func UpdateOneDoc(dbName, collectionName, updatedData, query string) int64 {
var filter bson.M
var updateData bson.M
var err error
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(query), &filter)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(updatedData), &updateData)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
collection := Connclient.Database(dbName).Collection(collectionName)
atualizacao := bson.D{{Key: "$set", Value: updateData}}
updatedResult, err := collection.UpdateOne(context.TODO(), filter, atualizacao)
if err != nil {
log.Fatal("Error on updating", err)
}
return updatedResult.ModifiedCount
}
// UpdateAllDoc update the info of a informed Hero
func UpdateAllDoc(dbName, collectionName, updatedData, query string) int64 {
var filter bson.M
var updateData bson.M
var err error
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(query), &filter)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(updatedData), &updateData)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
collection := Connclient.Database(dbName).Collection(collectionName)
atualizacao := bson.D{{Key: "$set", Value: updateData}}
updatedResult, err := collection.UpdateMany(context.TODO(), filter, atualizacao)
if err != nil {
log.Fatal("Error on updating", err)
}
return updatedResult.ModifiedCount
}
// DeleteOneDoc will retune find results
func DeleteOneDoc(dbName, collectionName, query string) int64 {
var err error
var bsonMap bson.M
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(query), &bsonMap)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
collection := Connclient.Database(dbName).Collection(collectionName)
deleteResult, errDelete := collection.DeleteOne(context.TODO(), bsonMap)
if err != nil {
log.Fatal("Error on deleting one Doc", errDelete)
}
return deleteResult.DeletedCount
}
// DeleteAllDoc will retune find results
func DeleteAllDoc(dbName, collectionName, query string) int64 {
var err error
var bsonMap bson.M
// Use the JSON package's Unmarshal() method
err = json. Unmarshal([]byte(query), &bsonMap)
if err != nil {
log. Fatal("json. Unmarshal() ERROR:", err)
}
collection := Connclient.Database(dbName).Collection(collectionName)
deleteResult, errDelete := collection.DeleteMany(context.TODO(), bsonMap)
if err != nil {
log.Fatal("Error on deleting many Doc", errDelete)
}
return deleteResult.DeletedCount
}