-
Notifications
You must be signed in to change notification settings - Fork 0
/
studyRules.go
150 lines (121 loc) · 3.6 KB
/
studyRules.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
package study
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
studyTypes "github.com/case-framework/case-backend/pkg/study/types"
)
func (dbService *StudyDBService) CreateIndexForStudyRulesCollection(instanceID string) error {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
indexes := []mongo.IndexModel{
{
Keys: bson.D{
{Key: "studyKey", Value: 1},
},
},
{
Keys: bson.D{
{Key: "uploadedAt", Value: 1},
{Key: "studyKey", Value: 1},
},
},
}
_, err := collection.Indexes().CreateMany(ctx, indexes)
return err
}
func (dbService *StudyDBService) deleteStudyRules(instanceID string, studyKey string) error {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
_, err := collection.DeleteMany(ctx, bson.M{"studyKey": studyKey})
return err
}
func (dbService *StudyDBService) SaveStudyRules(instanceID string, studyKey string, rules studyTypes.StudyRules) error {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
_, err := collection.InsertOne(ctx, rules)
return err
}
func (dbService *StudyDBService) GetCurrentStudyRules(instanceID string, studyKey string) (rules studyTypes.StudyRules, err error) {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
sortByPublished := bson.D{
primitive.E{Key: "uploadedAt", Value: -1},
}
filter := bson.M{
"studyKey": studyKey,
}
opts := &options.FindOneOptions{
Sort: sortByPublished,
}
err = collection.FindOne(ctx, filter, opts).Decode(&rules)
if err != nil {
return rules, err
}
err = rules.UnmarshalRules()
return rules, err
}
func (dbService *StudyDBService) GetStudyRulesByID(instanceID string, studyKey string, id string) (rules studyTypes.StudyRules, err error) {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
_id, err := primitive.ObjectIDFromHex(id)
if err != nil {
return rules, err
}
filter := bson.M{
"studyKey": studyKey,
"_id": _id,
}
err = collection.FindOne(ctx, filter).Decode(&rules)
if err != nil {
return rules, err
}
err = rules.UnmarshalRules()
return rules, err
}
func (dbService *StudyDBService) DeleteStudyRulesByID(instanceID string, studyKey string, id string) error {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
_id, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
filter := bson.M{
"studyKey": studyKey,
"_id": _id,
}
res, err := collection.DeleteOne(ctx, filter)
if res.DeletedCount < 1 {
return mongo.ErrNoDocuments
}
return err
}
func (dbService *StudyDBService) GetStudyRulesHistory(instanceID string, studyKey string) (ruleHistory []studyTypes.StudyRules, err error) {
ctx, cancel := dbService.getContext()
defer cancel()
collection := dbService.collectionStudyRules(instanceID)
filter := bson.M{
"studyKey": studyKey,
}
opts := options.Find().SetSort(bson.D{{Key: "uploadedAt", Value: -1}})
opts.SetProjection(bson.D{
primitive.E{Key: "rules", Value: 0},
primitive.E{Key: "serialisedRules", Value: 0},
})
cursor, err := collection.Find(ctx, filter, opts)
if err != nil {
return ruleHistory, err
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &ruleHistory); err != nil {
return ruleHistory, err
}
return ruleHistory, nil
}