-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_key_value.go
100 lines (89 loc) · 2.33 KB
/
actions_key_value.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
package horizon
import (
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/regources"
)
type KeyValueShowAction struct {
Action
key string
keyValueRecord *core.KeyValue
}
func (action *KeyValueShowAction) JSON() {
action.Do(
action.loadParams,
action.loadRecord,
func() {
res, err := resource.PopulateKeyValue(action.keyValueRecord)
if err != nil {
action.Log.WithError(err).Error("Failed to populate key_value")
action.Err = &problem.ServerError
return
}
hal.Render(action.W, *res)
},
)
}
func (action *KeyValueShowAction) loadParams() {
action.key = action.GetString("key")
}
func (action *KeyValueShowAction) loadRecord() {
var err error
q := action.CoreQ().KeyValue()
action.keyValueRecord, err = q.ByKey(action.key)
if err != nil {
action.Log.WithError(err).Error("Failed to get key_value from core DB")
action.Err = &problem.ServerError
return
}
if action.keyValueRecord == nil {
action.Err = &problem.NotFound
return
}
}
type KeyValueShowAllAction struct {
Action
coreRecords []core.KeyValue
recordsToRender []regources.KeyValue
}
func (action *KeyValueShowAllAction) JSON() {
action.Do(
action.loadRecord,
func() {
hal.Render(action.W, action.recordsToRender)
},
)
}
func (action *KeyValueShowAllAction) loadRecord() {
var err error
q := action.CoreQ().KeyValue()
action.coreRecords, err = q.Select()
if err != nil {
action.Log.WithError(err).Error("Failed to get all key_values from core DB")
action.Err = &problem.ServerError
return
}
action.recordsToRender, err = action.getPopulatedKeyValues()
if err != nil {
action.Log.WithError(err).Error("Failed to populate all key_values")
action.Err = &problem.ServerError
return
}
}
func (action *KeyValueShowAllAction) getPopulatedKeyValues() ([]regources.KeyValue, error) {
var res []regources.KeyValue
for _, keyValue := range action.coreRecords {
keyValueEntry, err := resource.PopulateKeyValue(&keyValue)
if err != nil {
return nil, errors.Wrap(err, "Failed to populate key_value", logan.F{
"keyValue": keyValue,
})
}
res = append(res, *keyValueEntry)
}
return res, nil
}