-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
123 lines (111 loc) · 3.41 KB
/
protocol.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
/*
* Copyright 2019 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mongo
import (
"context"
"github.com/SENERGY-Platform/models/go/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"strings"
)
const protocolIdFieldName = "Id"
const protocolNameFieldName = "Name"
var protocolIdKey string
var protocolNameKey string
func init() {
CreateCollections = append(CreateCollections, func(db *Mongo) error {
var err error
protocolIdKey, err = getBsonFieldName(models.Protocol{}, protocolIdFieldName)
if err != nil {
return err
}
protocolNameKey, err = getBsonFieldName(models.Protocol{}, protocolNameFieldName)
if err != nil {
return err
}
collection := db.client.Database(db.config.MongoTable).Collection(db.config.MongoProtocolCollection)
err = db.ensureIndex(collection, "protocolidindex", protocolIdKey, true, true)
if err != nil {
return err
}
err = db.ensureIndex(collection, "protocolnameindex", protocolNameKey, true, false)
if err != nil {
return err
}
return nil
})
}
func (this *Mongo) protocolCollection() *mongo.Collection {
return this.client.Database(this.config.MongoTable).Collection(this.config.MongoProtocolCollection)
}
func (this *Mongo) GetProtocol(ctx context.Context, id string) (protocol models.Protocol, exists bool, err error) {
result := this.protocolCollection().FindOne(ctx, bson.M{protocolIdKey: id})
err = result.Err()
if err == mongo.ErrNoDocuments {
return protocol, false, nil
}
if err != nil {
return
}
err = result.Decode(&protocol)
if err == mongo.ErrNoDocuments {
return protocol, false, nil
}
return protocol, true, err
}
func (this *Mongo) ListProtocols(ctx context.Context, limit int64, offset int64, sort string) (result []models.Protocol, err error) {
opt := options.Find()
opt.SetLimit(limit)
opt.SetSkip(offset)
parts := strings.Split(sort, ".")
sortby := protocolIdKey
switch parts[0] {
case "id":
sortby = protocolIdKey
case "name":
sortby = protocolNameKey
default:
sortby = protocolIdKey
}
direction := int32(1)
if len(parts) > 1 && parts[1] == "desc" {
direction = int32(-1)
}
opt.SetSort(bson.D{{sortby, direction}})
cursor, err := this.protocolCollection().Find(ctx, bson.M{}, opt)
if err != nil {
return nil, err
}
for cursor.Next(context.Background()) {
protocol := models.Protocol{}
err = cursor.Decode(&protocol)
if err != nil {
return nil, err
}
result = append(result, protocol)
}
err = cursor.Err()
return
}
func (this *Mongo) SetProtocol(ctx context.Context, protocol models.Protocol) error {
_, err := this.protocolCollection().ReplaceOne(ctx, bson.M{protocolIdKey: protocol.Id}, protocol, options.Replace().SetUpsert(true))
return err
}
func (this *Mongo) RemoveProtocol(ctx context.Context, id string) error {
_, err := this.protocolCollection().DeleteOne(ctx, bson.M{protocolIdKey: id})
return err
}