/
repository.go
97 lines (74 loc) · 2.29 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
package repository
import (
"context"
"errors"
"time"
"github.com/flussrd/fluss-back/app/river-management/models"
repository "github.com/flussrd/fluss-back/app/river-management/repositories/rivers"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
const (
databaseName = "fluss-db"
mongoDuplicateCode = 11000
)
type mongoRepository struct {
client *mongo.Client
}
// New returns a new mongo repository instance
func New(client *mongo.Client) repository.Repository {
return mongoRepository{
client: client,
}
}
// SaveRiver saves a river on the database
func (r mongoRepository) SaveRiver(ctx context.Context, river models.River) (models.River, error) {
riversCollection := r.getRiversCollection()
river.CreationDate = time.Now()
river.UpdateDate = time.Now()
_, err := riversCollection.InsertOne(ctx, river)
if errors.As(err, &mongo.WriteException{}) {
mongoErr, _ := err.(mongo.WriteException)
switch mongoErr.WriteErrors[0].Code {
case mongoDuplicateCode:
return models.River{}, repository.ErrDuplicateFields
}
}
if err != nil {
return models.River{}, err
}
return river, nil
}
// GetRiver finds a river on the database based in the id
func (r mongoRepository) GetRiver(ctx context.Context, riverID string) (models.River, error) {
riversCollection := r.getRiversCollection()
river := models.River{}
err := riversCollection.FindOne(ctx, bson.M{
"_id": riverID,
}).Decode(&river)
if errors.Is(err, mongo.ErrNoDocuments) {
return models.River{}, repository.ErrNotFound
}
return river, nil
}
// GetAllRiversNotPaginated temporal method for retrieving all the rivers from the database
func (r mongoRepository) GetAllRiversNotPaginated(ctx context.Context) ([]models.River, error) {
riversCollection := r.getRiversCollection()
results, err := riversCollection.Find(ctx, bson.M{})
if err != nil {
return nil, err
}
rivers := []models.River{}
err = results.All(ctx, &rivers)
if err != nil {
return nil, err
}
return rivers, nil
}
// GetAllRivers returns all rivers from the database, paginated
func (r mongoRepository) GetAllRivers(ctx context.Context) ([]models.River, string, error) {
return nil, "", nil
}
func (r mongoRepository) getRiversCollection() *mongo.Collection {
return r.client.Database(databaseName).Collection("rivers")
}