-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_sale_ante.go
107 lines (91 loc) · 2.74 KB
/
actions_sale_ante.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
package horizon
import (
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type SaleAnteAction struct {
Action
q core.SaleAnteQI
SaleID string
ParticipantBalanceID string
Records []core.SaleAnte
Page hal.Page
}
func (action *SaleAnteAction) JSON() {
action.Do(
action.loadParams,
action.checkAllowed,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *SaleAnteAction) loadParams() {
action.SaleID = action.GetString("sale_id")
action.ParticipantBalanceID = action.GetString("participant_balance_id")
action.Page.Filters = map[string]string{
"sale_id": action.SaleID,
"participant_balance_id": action.ParticipantBalanceID,
}
}
func (action *SaleAnteAction) checkAllowed() {
if action.ParticipantBalanceID == "" {
action.IsAllowed("")
return
}
participantBalance, err := action.CoreQ().Balances().ByID(action.ParticipantBalanceID)
if err != nil {
action.Log.WithError(err).Error("failed to get sale ante participant balance from core DB")
action.Err = &problem.ServerError
return
}
if participantBalance == nil {
action.SetInvalidField("participant_balance_id", errors.New("sale ante participant balance does not exist in core DB"))
return
}
action.IsAllowed(participantBalance.AccountID)
}
func (action *SaleAnteAction) loadRecords() {
action.q = action.CoreQ().SaleAntes()
if action.SaleID != "" {
action.q = action.q.ForSale(action.SaleID)
}
if action.ParticipantBalanceID != "" {
action.q = action.q.ForBalance(action.ParticipantBalanceID)
}
if action.Err != nil {
return
}
var err error
action.Records, err = action.q.Select()
if err != nil {
action.Log.WithError(err).Error("failed to get sale antes from core DB")
action.Err = &problem.ServerError
return
}
}
func (action *SaleAnteAction) loadPage() {
for _, saleAnte := range action.Records {
participantBalance, err := action.CoreQ().Balances().ByID(saleAnte.ParticipantBalanceID)
if err != nil || participantBalance == nil {
action.Log.WithError(err).WithFields(logan.F{
"sale_id": saleAnte.SaleID,
"participant_balance_id": saleAnte.ParticipantBalanceID,
}).Error("failed to get participant balance for sale ante from core DB")
action.Err = &problem.ServerError
return
}
var res resource.SaleAnte
res.Populate(&saleAnte, participantBalance.Asset)
action.Page.Add(&res)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.PopulateLinks()
}