-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_history_operation.go
119 lines (104 loc) · 3.05 KB
/
actions_history_operation.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package horizon
import (
"gitlab.com/tokend/go/xdr"
"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"
)
// This file contains the actions:
//
// OperationIndexAction: pages of operations
// OperationShowAction: single operation by id
// OperationIndexAction renders a page of operations resources, identified by
// a normal page query and optionally filtered by an account, ledger, or
// transaction.
type HistoryOperationIndexAction struct {
Action
Types []xdr.OperationType
PagingParams db2.PageQuery
Records []history.Operation
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *HistoryOperationIndexAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *HistoryOperationIndexAction) loadParams() {
action.PagingParams = action.GetPageQuery()
}
func (action *HistoryOperationIndexAction) loadRecords() {
err := action.HistoryQ().Operations().Page(action.PagingParams).Select(&action.Records)
if err != nil {
action.Log.WithError(err).Error("failed to get operations")
action.Err = &problem.ServerError
return
}
}
func (action *HistoryOperationIndexAction) loadPage() {
for _, record := range action.Records {
var res hal.Pageable
res, action.Err = resource.NewPublicOperation(action.Ctx, record, nil)
if action.Err != nil {
return
}
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()
}
// OperationShowAction renders a ledger found by its sequence number.
type HistoryOperationShowAction struct {
Action
ID int64
Record history.Operation
Resource interface{}
Participants map[int64]*history.OperationParticipants
}
func (action *HistoryOperationShowAction) loadParams() {
action.ID = action.GetInt64("id")
}
func (action *HistoryOperationShowAction) loadRecord() {
action.Err = action.HistoryQ().OperationByID(&action.Record, action.ID)
if action.Err != nil {
return
}
action.Participants = map[int64]*history.OperationParticipants{
action.ID: {},
}
switch action.Record.Type {
case xdr.OperationTypeManageOffer:
// workaround for load participants
action.IsAdmin = true
action.LoadParticipants("", action.Participants)
// reverting workaround, just in case
action.IsAdmin = false
}
}
func (action *HistoryOperationShowAction) loadResource() {
action.Resource, action.Err = resource.NewPublicOperation(
action.Ctx, action.Record, action.Participants[action.Record.ID].Participants)
}
// JSON is a method for actions.JSON
func (action *HistoryOperationShowAction) JSON() {
action.Do(
action.loadParams,
action.loadRecord,
action.loadResource,
func() {
hal.Render(action.W, action.Resource)
},
)
}