-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_balance_index.go
79 lines (67 loc) · 1.85 KB
/
actions_balance_index.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
package horizon
import (
"gitlab.com/tokend/horizon/db2"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type BalanceIndexAction struct {
Action
AccountFilter string
ExchangeFilter string
Asset string
PagingParams db2.PageQuery
Records []history.Balance
Page hal.Page
}
func (action *BalanceIndexAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *BalanceIndexAction) loadParams() {
action.ValidateCursorAsDefault()
action.AccountFilter = action.GetString("account")
action.ExchangeFilter = action.GetString("exchange")
action.PagingParams = action.GetPageQuery()
action.Asset = action.GetString("asset")
action.Page.Filters = map[string]string{
"account": action.AccountFilter,
"exchange": action.ExchangeFilter,
"asset": action.Asset,
}
}
func (action *BalanceIndexAction) loadRecords() {
balances := action.HistoryQ().Balances()
if action.AccountFilter != "" {
balances.ForAccount(action.AccountFilter)
}
if action.Asset != "" {
balances.ForAsset(action.Asset)
}
err := balances.Page(action.PagingParams).Select(&action.Records)
if err != nil {
action.Log.WithError(err).Error("failed to get balances")
action.Err = &problem.ServerError
return
}
}
func (action *BalanceIndexAction) loadPage() {
for _, record := range action.Records {
var balance resource.BalancePublic
balance.Populate(record)
action.Page.Add(balance)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}