-
Notifications
You must be signed in to change notification settings - Fork 16
/
read_model.go
57 lines (49 loc) · 1.3 KB
/
read_model.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
package mongo
import (
"context"
"github.com/vinllen/mgo/bson"
"go.mongodb.org/mongo-driver/mongo"
"github.com/Bio-OS/bioos/internal/context/notebookserver/application/query"
)
type readModel struct {
collection *mongo.Collection
}
// NewReadModel ...
func NewReadModel(ctx context.Context, mongoDB *mongo.Database) (query.ReadModel, error) {
collection := mongoDB.Collection(notebookServerCollection)
return &readModel{
collection: collection,
}, nil
}
func (r *readModel) ListSettingsByWorkspace(ctx context.Context, workspaceID string) ([]*query.NotebookSettings, error) {
filter := bson.M{
"workspaceID": workspaceID,
}
cursor, err := r.collection.Find(ctx, filter)
if err != nil {
return nil, err
}
var res []*query.NotebookSettings
for cursor.Next(ctx) {
var po notebookServer
if err = cursor.Decode(&po); err != nil {
return nil, err
}
res = append(res, po.toDTO())
}
return res, nil
}
func (r *readModel) GetSettingsByID(ctx context.Context, workspaceID, id string) (*query.NotebookSettings, error) {
filter := bson.M{
"workspaceID": workspaceID,
"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.toDTO(), nil
}