-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_limits_v2.go
79 lines (70 loc) · 2.01 KB
/
actions_limits_v2.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/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"
)
// This file contains the actions:
//
// AccountTypeLimitsShowAction: renders AccountTypeLimits for operationType
type LimitsV2ShowAction struct {
Action
StatsOpType int32
Asset string
AccountID *string
AccountType *int32
Result resource.LimitsV2
}
// JSON is a method for actions.JSON
func (action *LimitsV2ShowAction) JSON() {
action.Do(
action.loadParams,
action.loadData,
func() {
hal.Render(action.W, action.Result)
},
)
}
func (action *LimitsV2ShowAction) loadParams() {
action.StatsOpType = action.GetInt32("stats_op_type")
action.Asset = action.GetNonEmptyString("asset")
accountID := action.GetString("account_id")
if accountID != "" {
action.AccountID = &accountID
}
accountType := action.GetInt32("account_type")
if accountType != 0 {
action.AccountType = &accountType
}
if (action.AccountID != nil) && (action.AccountType != nil) {
action.SetInvalidField("account_id", errors.New("It's not allowed to specify both account_id and account type"))
return
}
}
func (action *LimitsV2ShowAction) loadData() {
q := action.CoreQ().LimitsV2().ForStatsOpType(action.StatsOpType).ForAsset(action.Asset)
if action.AccountID != nil {
q = q.ForAccountID(*action.AccountID)
} else if action.AccountType != nil {
q = q.ForAccountType(*action.AccountType)
} else {
q = q.Global()
}
result, err := q.Select()
if err != nil {
action.Log.WithError(err).Error("Failed to select limits")
action.Err = &problem.ServerError
return
}
if len(result) > 1 {
action.Log.WithField("limits", result).Error("Expected 1 limit at max")
action.Err = &problem.ServerError
return
}
if len(result) == 0 {
result = append(result, core.DefaultLimits(action.AccountType, action.AccountID, action.StatsOpType, action.Asset))
}
action.Result.Populate(result[0])
}