-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_ledger_changes.go
70 lines (62 loc) · 1.72 KB
/
actions_ledger_changes.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
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 LedgerChangesAction struct {
Action
PagingParams db2.PageQuery
Records []history.Transaction
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *LedgerChangesAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.checkAllowed,
action.ValidateCursorWithinHistory,
action.loadRecords,
action.loadPage,
func() { hal.Render(action.W, action.Page) },
)
}
func (action *LedgerChangesAction) loadParams() {
action.ValidateCursorAsDefault()
action.PagingParams = action.GetPageQuery()
}
func (action *LedgerChangesAction) checkAllowed() {
action.IsAllowed("")
}
func (action *LedgerChangesAction) loadRecords() {
err := action.HistoryQ().Transactions().
Page(action.PagingParams).
Select(&action.Records)
if err != nil {
action.Log.WithError(err).Error("failed to load transaction records")
action.Err = &problem.ServerError
return
}
}
func (action *LedgerChangesAction) loadPage() {
for _, record := range action.Records {
var res resource.LedgerChanges
if err := res.Populate(record); err != nil {
action.Log.WithError(err).Error("failed to populate ledger changes")
action.Err = &problem.ServerError
return
}
if len(res.Changes) > 0 {
action.Page.Add(res)
}
}
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()
}