/
repository.go
68 lines (58 loc) · 1.58 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
package mongo
import (
"context"
"github.com/vinllen/mgo/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/Bio-OS/bioos/internal/context/notebookserver/domain"
)
const notebookServerCollection = "notebookserver"
type repository struct {
collection *mongo.Collection
}
// NewRepository ...
func NewRepository(ctx context.Context, mongoDB *mongo.Database) (domain.Repository, error) {
collection := mongoDB.Collection(notebookServerCollection)
if _, err := collection.Indexes().CreateMany(ctx, []mongo.IndexModel{
{Keys: bson.M{"id": 1}, Options: options.Index().SetUnique(true)},
}); err != nil {
return nil, err
}
return &repository{
collection: collection,
}, nil
}
func (r *repository) Get(ctx context.Context, id string) (*domain.NotebookServer, error) {
filter := bson.M{
"id": id,
}
var result notebookServer
if err := r.collection.FindOne(ctx, filter).Decode(&result); err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return result.toDO(), nil
}
func (r *repository) Save(ctx context.Context, do *domain.NotebookServer) error {
po := newNotebookServer(do)
filter := bson.M{"id": po.ID}
result, err := r.collection.ReplaceOne(ctx, filter, po)
if err != nil {
return err
}
if result.ModifiedCount == 0 {
_, err = r.collection.InsertOne(ctx, po)
}
return err
}
func (r *repository) Delete(ctx context.Context, do *domain.NotebookServer) error {
filter := bson.M{
"id": do.ID,
}
if _, err := r.collection.DeleteOne(ctx, filter); err != nil {
return err
}
return nil
}