/
repository.go
341 lines (262 loc) · 7.95 KB
/
repository.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package repository
import (
"context"
"fmt"
"time"
"github.com/yellyoshua/elections-app/commons/logger"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// TODO: considerar cambiar el filtro de interface{} -> map[string]interface{}
// y bson.M{[field]: [filter_value]}
type Collection interface {
UpdateOne(filter interface{}, update map[string]interface{}) error
FindOne(filter interface{}, dest interface{}) error
Find(filter interface{}, dest interface{}) error
FindByID(id primitive.ObjectID, dest interface{}) error
InsertOne(data interface{}) (primitive.ObjectID, error)
InsertMany(data []interface{}) error
Drop() error
}
type MongoCollection struct {
current *mongo.Collection
name string
db *mongo.Database
}
type Client interface {
Col(collection string) Collection
}
type RepositoryClient struct {
Conf RepositoryConf
Db *mongo.Database
}
type RepositoryConf struct {
DatabaseURI string
DatabaseName string
}
func NoBootstrap(db *mongo.Database) error {
return nil
}
func New(repository_conf RepositoryConf, database_bootstrap func(*mongo.Database) error) Client {
db := MongoDatabase(repository_conf.DatabaseURI, repository_conf.DatabaseName)
if errBootstrap := database_bootstrap(db); errBootstrap != nil {
logger.Panic("error on bootstrap database repository -> %s", errBootstrap)
}
return &RepositoryClient{Db: db, Conf: repository_conf}
}
func NewWithCollection(name string, col *mongo.Collection) Collection {
return &MongoCollection{
name: name,
db: &mongo.Database{},
current: col,
}
}
func (c *RepositoryClient) Col(collection string) Collection {
return &MongoCollection{
name: collection,
db: c.Db,
current: c.Db.Collection(collection),
}
}
func (col *MongoCollection) Drop() error {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
return col.current.Drop(ctx)
}
func (col *MongoCollection) FindByID(id primitive.ObjectID, dest interface{}) error {
filter := bson.M{"_id": id}
if findError := col.current.FindOne(context.TODO(), filter).Decode(dest); findError != nil {
return findError
}
return nil
}
func (col *MongoCollection) FindOne(filter interface{}, dest interface{}) error {
if err := col.current.FindOne(context.TODO(), filter).Decode(dest); err != nil {
return err
}
return nil
}
func (col *MongoCollection) Find(filter interface{}, dest interface{}) error {
finderCtx, cancelFinderCtx := context.WithCancel(context.TODO())
cursor, findError := col.current.Find(finderCtx, filter)
defer cancelFinderCtx()
if findError != nil {
return findError
}
cursorCtx, cancancelCursorCtx := context.WithCancel(context.TODO())
cursorError := cursor.All(cursorCtx, dest)
defer cancancelCursorCtx()
defer cursor.Close(context.TODO())
if cursorError != nil {
return cursorError
}
return nil
}
func (col *MongoCollection) InsertOne(data interface{}) (primitive.ObjectID, error) {
ctx, cancel := context.WithCancel(context.TODO())
result, err := col.current.InsertOne(ctx, data)
defer cancel()
if err != nil {
return primitive.ObjectID{}, err
}
id, _ := result.InsertedID.(primitive.ObjectID)
return id, err
}
func (col *MongoCollection) InsertMany(data []interface{}) error {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
// TODO: verificar si es necesario devolver un array de ID's
_, err := col.current.InsertMany(ctx, data)
return err
}
func (col *MongoCollection) UpdateOne(filter interface{}, update map[string]interface{}) error {
ctx, cancel := context.WithCancel(context.TODO())
updater, err := col.current.UpdateOne(ctx, filter, bson.M{"$set": primitive.M(update)})
defer cancel()
if err != nil {
return err
}
if updater.MatchedCount == 0 {
return fmt.Errorf("ERROR no matched documents")
}
return err
}
/// ---
// package repository
// import (
// "context"
// "fmt"
// "time"
// "github.com/yellyoshua/elections-app/commons/logger"
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/bson/primitive"
// "go.mongodb.org/mongo-driver/mongo"
// )
// // Collection __
// type Collection interface {
// UpdateOne(filter interface{}, update map[string]interface{}) error
// FindOne(filter interface{}, dest interface{}) error
// Find(filter interface{}, dest interface{}) error
// FindByID(id primitive.ObjectID, dest interface{}) error
// InsertOne(data interface{}) (primitive.ObjectID, error)
// InsertMany(data []interface{}) error
// Drop() error
// }
// type clientStruct struct {
// col *mongo.Collection
// collection string
// client *mongo.Database
// }
// // Repository __
// type Repository interface {
// Col(collection string) Collection
// DatabaseDrop(ctx context.Context) error
// }
// type repositoryStruct struct {
// client *mongo.Database
// }
// // Initialize start database connection
// func Initialize(indexes bool) {
// setup, db := clientMongodb()
// s := &mongo.Collection{}
// s.Database()
// if indexes {
// setup.SetupIndexes()
// }
// if db == nil {
// logger.Fatal("Database connection is nil")
// }
// }
// // New _
// func New() Repository {
// _, client := clientMongodb()
// return NewWithClient(client) // provide real implementation here as argument
// }
// // NewWithClient creates a new Storage client with a custom implementation
// // This is the function you use in your unit tests
// func NewWithClient(client *mongo.Database) Repository {
// return &repositoryStruct{
// client: client,
// }
// }
// func (r *repositoryStruct) Col(collection string) Collection {
// // r.client.Collection()
// return &clientStruct{
// client: r.client,
// col: r.client.Collection(collection),
// collection: collection,
// }
// }
// func (r *repositoryStruct) DatabaseDrop(ctx context.Context) error {
// return r.client.Drop(ctx)
// }
// func (client *clientStruct) Drop() error {
// ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
// defer cancel()
// err := client.col.Drop(ctx)
// return err
// }
// func (client *clientStruct) FindByID(id primitive.ObjectID, dest interface{}) error {
// var err error
// if err != nil {
// return err
// }
// err = client.col.FindOne(context.TODO(), bson.M{"_id": id}).Decode(dest)
// if err != nil {
// return err
// }
// return nil
// }
// func (client *clientStruct) FindOne(filter interface{}, dest interface{}) error {
// err := client.col.FindOne(context.TODO(), filter).Decode(dest)
// if err != nil {
// return err
// }
// return nil
// }
// func (client *clientStruct) Find(filter interface{}, dest interface{}) error {
// var cursor *mongo.Cursor
// var err error
// ctx := context.Background()
// cursor, err = client.col.Find(context.TODO(), filter)
// if err != nil {
// return err
// }
// err = cursor.All(ctx, dest)
// defer cursor.Close(ctx)
// if err != nil {
// return err
// }
// return nil
// }
// func (client *clientStruct) InsertOne(data interface{}) (primitive.ObjectID, error) {
// ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
// defer cancel()
// result, err := client.col.InsertOne(ctx, data)
// if err != nil {
// return primitive.ObjectID{}, err
// }
// id, _ := result.InsertedID.(primitive.ObjectID)
// return id, err
// }
// func (client *clientStruct) InsertMany(data []interface{}) error {
// ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
// defer cancel()
// _, err := client.col.InsertMany(ctx, data)
// return err
// }
// func (client *clientStruct) UpdateOne(filter interface{}, update map[string]interface{}) error {
// updater, err := client.col.UpdateOne(context.TODO(), filter, bson.M{"$set": primitive.M(update)})
// if err != nil {
// return err
// }
// if updater.MatchedCount == 0 {
// return fmt.Errorf("ERROR no matched documents")
// }
// return err
// }
// func clientMongodb() (Connection, *mongo.Database) {
// return connect()
// }
// // Contraseña pc 1457