-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
121 lines (100 loc) · 3.1 KB
/
global.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
package db
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/bin-work/go-example/mongo/model"
)
func InitDb(addr []string, maxpollsize uint64, username, password, source string) error {
return dbmr.InitDb(addr, maxpollsize, username, password, source)
}
func Close(ctx context.Context) {
dbmr.CloseDb(ctx)
}
func FindRoom(ctx context.Context, roomId string) bool {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
collection := dbmr.Mongo.Database(dbmr.DbName).Collection("record.cache")
filter := bson.D{
{"room", roomId},
}
video := &model.RecordCache{}
collection.FindOne(ctx, filter).Decode(&video)
return !video.Id.IsZero()
}
func GetCache(ctx context.Context, roomId string) (cache *model.RecordCache, err error) {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
collection := dbmr.Mongo.Database(dbmr.DbName).Collection("record.cache")
filter := bson.D{
{"room", roomId},
}
cache = &model.RecordCache{}
err = collection.FindOne(ctx, filter).Decode(&cache)
return cache, err
}
func CreateRecordCache(ctx context.Context, cache *model.RecordCache) error {
cache.Id = primitive.NewObjectID()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
_, err := dbmr.Mongo.Database(dbmr.DbName).Collection("record.cache").InsertOne(ctx, cache)
if err != nil {
return err
}
return nil
}
func AppendRecordForRecordCache(ctx context.Context, roomId string, record *model.Record) error {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
collection := dbmr.Mongo.Database(dbmr.DbName).Collection("record.cache")
filter := bson.M{"room": roomId}
update := bson.D{
{"$push", bson.D{{"records", record}}},
}
_, err := collection.UpdateOne(ctx, filter, update)
return err
}
func SetEnd(ctx context.Context, roomId, display string, startTime uint64) error {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
collection := dbmr.Mongo.Database(dbmr.DbName).Collection("record.cache")
filter := bson.M{"room": roomId}
update := bson.D{
{"$set", bson.D{
{"records.$[record].end", true},
},
},
}
opts := options.Update().SetArrayFilters(options.ArrayFilters{
Filters: append(make([]interface{}, 0), bson.D{
{"record.display", display},
{"record.startTime", startTime},
},
),
})
_, err := collection.UpdateOne(ctx, filter, update, opts)
return err
}
func SetUrl(ctx context.Context, roomId, display string, startTime uint64, url string) error {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
collection := dbmr.Mongo.Database(dbmr.DbName).Collection("record.cache")
filter := bson.M{"room": roomId}
update := bson.D{
{"$set", bson.D{
{"records.$[record].url", url},
},
},
}
opts := options.Update().SetArrayFilters(options.ArrayFilters{
Filters: append(make([]interface{}, 0), bson.D{
{"record.display", display},
{"record.startTime", startTime},
},
),
})
_, err := collection.UpdateOne(ctx, filter, update, opts)
return err
}