-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_offers.go
90 lines (75 loc) · 2.1 KB
/
actions_offers.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
package horizon
import (
"github.com/go-errors/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 OffersAction struct {
Action
accountID string
baseAsset string
quoteAsset string
isBuy *bool
offerID string
orderBookID *uint64
onlyPrimaryMarket bool
coreRecords []core.Offer
page hal.Page
}
// JSON is a method for actions.JSON
func (action *OffersAction) JSON() {
action.Do(
action.loadParams,
action.checkAllowed,
action.loadRecords,
func() {
hal.Render(action.W, action.page)
},
)
}
func (action *OffersAction) loadParams() {
action.accountID = action.GetNonEmptyString("account_id")
action.baseAsset = action.GetString("base_asset")
action.quoteAsset = action.GetString("quote_asset")
action.isBuy = action.GetOptionalBool("is_buy")
action.offerID = action.GetString("offer_id")
action.orderBookID = action.GetOptionalUint64("order_book_id")
if (action.baseAsset == "") != (action.quoteAsset == "") {
action.SetInvalidField("base_asset", errors.New("base and quote assets must be both set or both not set"))
return
}
action.onlyPrimaryMarket = action.GetBool("only_primary")
}
func (action *OffersAction) checkAllowed() {
action.IsAllowed(action.accountID)
}
func (action *OffersAction) loadRecords() {
q := action.CoreQ().Offers().ForAccount(action.accountID)
if action.baseAsset != "" {
q = q.ForAssets(action.baseAsset, action.quoteAsset)
}
if action.isBuy != nil {
q = q.IsBuy(*action.isBuy)
}
if action.offerID != "" {
q = q.ForOfferID(action.offerID)
}
if action.orderBookID != nil {
q = q.ForOrderBookID(*action.orderBookID)
}
if action.onlyPrimaryMarket {
q = q.OnlyPrimaryMarket()
}
err := q.Select(&action.coreRecords)
if err != nil {
action.Log.WithError(err).Error("Failed to get offers from core DB")
action.Err = &problem.ServerError
return
}
action.page.Init()
for i := range action.coreRecords {
action.page.Add(resource.PopulateOffer(action.coreRecords[i]))
}
}