-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_asset_show.go
84 lines (71 loc) · 1.78 KB
/
actions_asset_show.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
package horizon
import (
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type AssetsShowAction struct {
Action
Code string
Asset resource.Asset
}
func (action *AssetsShowAction) JSON() {
action.Do(
action.loadParams,
action.loadData,
func() {
hal.Render(action.W, action.Asset)
},
)
}
func (action *AssetsShowAction) loadParams() {
action.Code = action.GetString("code")
}
func (action *AssetsShowAction) loadData() {
asset, err := action.CoreQ().Assets().ByCode(action.Code)
if err != nil {
action.Err = &problem.ServerError
action.Log.WithStack(err).WithError(err).Error("Failed to load asset by code")
return
}
if asset == nil {
action.Err = &problem.NotFound
return
}
action.Asset.Populate(asset)
}
type AssetHoldersShowAction struct {
Action
code string
balances []resource.Balance
}
func (action *AssetHoldersShowAction) JSON() {
action.Do(
action.loadParams,
action.checkIsAllowed,
action.loadData,
func() {
hal.Render(action.W, action.balances)
},
)
}
func (action *AssetHoldersShowAction) loadParams() {
action.code = action.GetNonEmptyString("code")
}
func (action *AssetHoldersShowAction) checkIsAllowed() {
action.isAllowed("")
}
func (action *AssetHoldersShowAction) loadData() {
balances, err := action.CoreQ().Balances().ByAsset(action.code).NonZero().Select()
if err != nil {
action.Err = &problem.ServerError
action.Log.WithStack(err).WithError(err).Error("Failed to load asset by code")
return
}
action.balances = make([]resource.Balance, 0, len(balances))
for _, coreBalance := range balances {
resourceBalance := resource.Balance{}
resourceBalance.Populate(coreBalance)
action.balances = append(action.balances, resourceBalance)
}
}