-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_account_balances.go
53 lines (44 loc) · 1.13 KB
/
actions_account_balances.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
package horizon
import (
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type AccountBalancesAction struct {
Action
AccountID string
Records []history.Balance
Resource []resource.BalancePublic
}
func (action *AccountBalancesAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadResource,
func() {
hal.Render(action.W, action.Resource)
},
)
}
func (action *AccountBalancesAction) loadParams() {
action.AccountID = action.GetNonEmptyString("id")
}
func (action *AccountBalancesAction) loadRecords() {
if err := action.HistoryQ().Balances().ForAccount(action.AccountID).Select(&action.Records); err != nil {
action.Log.WithError(err).Error("failed to get balances")
action.Err = &problem.ServerError
return
}
if len(action.Records) == 0 {
action.Err = &problem.NotFound
return
}
}
func (action *AccountBalancesAction) loadResource() {
for _, record := range action.Records {
var r resource.BalancePublic
r.Populate(record)
action.Resource = append(action.Resource, r)
}
}