-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget-data.go
62 lines (50 loc) · 1.6 KB
/
widget-data.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
package wsHandler
import (
"context"
"errors"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/bson"
)
func HandleWidgetDataGetReq(req *protos.WidgetDataGetReq, hctx wsHelpers.HandlerContext) ([]*protos.WidgetDataGetResp, error) {
result := hctx.Svcs.MongoDB.Collection(dbCollections.WidgetDataName).FindOne(context.TODO(), bson.M{
"_id": req.Id,
})
if result.Err() != nil {
return nil, result.Err()
}
widgetData := &protos.WidgetData{}
err := result.Decode(&widgetData)
if err != nil {
return nil, err
}
return []*protos.WidgetDataGetResp{&protos.WidgetDataGetResp{
WidgetData: widgetData,
}}, nil
}
func HandleWidgetDataWriteReq(req *protos.WidgetDataWriteReq, hctx wsHelpers.HandlerContext) ([]*protos.WidgetDataWriteResp, error) {
if req.WidgetData.Id == "" {
return nil, errors.New("widget data must have a predefined id to write to")
}
// Check if exists
result := hctx.Svcs.MongoDB.Collection(dbCollections.WidgetDataName).FindOne(context.TODO(), bson.M{
"_id": req.WidgetData.Id,
})
// The widget must already exist for us to write to it (created by screen configuration)
if result.Err() != nil {
return nil, result.Err()
}
// Update
_, err := hctx.Svcs.MongoDB.Collection(dbCollections.WidgetDataName).UpdateOne(context.TODO(), bson.M{
"_id": req.WidgetData.Id,
}, bson.M{
"$set": req.WidgetData,
})
if err != nil {
return nil, err
}
return []*protos.WidgetDataWriteResp{&protos.WidgetDataWriteResp{
WidgetData: req.WidgetData,
}}, nil
}