/
generic.go
185 lines (147 loc) · 4.56 KB
/
generic.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package repository
import (
"context"
"errors"
log "github.com/sirupsen/logrus"
"github.com/talesmud/talesmud/pkg/db"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
// GenericRepo ...
type GenericRepo struct {
db *db.Client
collection string
generator func() interface{}
}
type entityGenerator func() interface{}
type elementCollector func(element interface{})
//CreateIndex ..
func (repo *GenericRepo) CreateIndex() {
mod := mongo.IndexModel{
Keys: bson.M{
"id": 1,
},
Options: options.Index().SetUnique(true),
}
name, err := repo.db.C(repo.collection).Indexes().CreateOne(context.Background(), mod)
if err != nil {
log.WithError(err).Info("Failed creating Index")
} else {
log.WithField("Index", name).Info("Created Index")
}
}
// DropCollection ... drops the whole collection
func (repo *GenericRepo) DropCollection() error {
return repo.db.Drop(repo.collection)
}
// FindByID ...
func (repo *GenericRepo) FindByID(id string) (interface{}, error) {
log.WithField("id", id).Info("FindByID called")
result := repo.db.FindByID(repo.collection, id)
if result != nil {
entity := repo.generator()
if err := result.Decode(entity); err != nil {
log.WithField("Error", err).Error("Error decoding entity")
return nil, errors.New("entity not found")
}
return entity, nil
}
return nil, errors.New("entity not found")
}
// FindByField ...
func (repo *GenericRepo) FindByField(key string, value string) (interface{}, error) {
log.WithField(key, value).Info("FindByField called")
result := repo.db.FindOne(repo.collection, key, value)
if result != nil {
entity := repo.generator()
if err := result.Decode(entity); err != nil {
log.WithField("Error", err).Error("Error decoding entity")
return nil, errors.New("entity not found")
}
return entity, nil
}
return nil, errors.New("entity not found")
}
// UpdateByField an existing entity
func (repo *GenericRepo) UpdateByField(item interface{}, key string, value string) error {
if result, err := repo.db.UpdateOne(repo.collection, key, value, item); err != nil {
log.WithError(err).Error("Error during update")
return err
} else {
if result.MatchedCount == 0 {
log.WithField("Generic Update Failed", result).WithField("Key", key).Info("updated entity with key")
} else {
log.WithField("Generic Update Success", result).WithField("Key", key).Info("updated entity with key")
}
}
return nil
}
// FindAllWithParam returns all entities
func (repo *GenericRepo) FindAllWithParam(params *db.QueryParams, collector elementCollector) error {
cursor, err := repo.db.FindAllWithParams(repo.collection, params)
if err != nil {
log.WithField("collection", repo.collection).WithField("cursor", cursor).Error(err)
return err
}
for cursor.Next(context.TODO()) {
elem := repo.generator()
err := cursor.Decode(elem)
if err != nil {
log.WithError(err).Error("Could not decode element")
continue
}
collector(elem)
}
return nil
}
// FindAll returns all entities
func (repo *GenericRepo) FindAll(collector elementCollector) error {
//var results []interface{}
cursor, err := repo.db.FindAll(repo.collection)
if err != nil {
log.WithField("collection", repo.collection).WithField("cursor", cursor).Fatal(err)
}
for cursor.Next(context.TODO()) {
elem := repo.generator()
err := cursor.Decode(elem)
if err != nil {
log.WithError(err).Error("Could not decode element")
continue
}
collector(elem)
}
return nil
}
// Store stores a new entity
func (repo *GenericRepo) Store(entity interface{}) (interface{}, error) {
if _, error := repo.db.InsertOne(repo.collection, entity); error != nil {
log.WithField("Error", error).Error("error during insertion")
return nil, error
}
return entity, nil
}
// Delete an existing entity
func (repo *GenericRepo) Delete(id string) error {
if result, err := repo.db.DeleteByID(repo.collection, id); err != nil {
log.WithError(err).Error("Error during update")
return err
} else {
log.WithField("Generic Update", result).Info("updated entity")
}
return nil
}
// Update an existing entity
func (repo *GenericRepo) Update(item interface{}, id string) error {
if result, err := repo.db.UpdateOneByID(repo.collection, id, item); err != nil {
log.WithError(err).Error("Error during update")
return err
} else {
if result.MatchedCount == 0 {
log.WithField("Generic Update Failed", result).WithField("ID", id).Info("updated entity by id")
} else {
log.WithField("Generic Update Success", result).WithField("ID", id).Info("updated entity by id")
}
}
return nil
}