This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
/
actions_offer.go
78 lines (69 loc) · 1.87 KB
/
actions_offer.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
package horizon
import (
"github.com/stellar/horizon/db2"
"github.com/stellar/horizon/db2/core"
"github.com/stellar/horizon/render/hal"
"github.com/stellar/horizon/render/sse"
"github.com/stellar/horizon/resource"
)
// This file contains the actions:
// OffersByAccountAction renders a page of offer resources, for a given
// account. These offers are present in the ledger as of the latest validated
// ledger.
type OffersByAccountAction struct {
Action
Address string
PageQuery db2.PageQuery
Records []core.Offer
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *OffersByAccountAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
// SSE is a method for actions.SSE
func (action *OffersByAccountAction) SSE(stream sse.Stream) {
action.Do(
action.loadParams,
action.loadRecords,
func() {
stream.SetLimit(int(action.PageQuery.Limit))
for _, record := range action.Records[stream.SentCount():] {
var res resource.Offer
res.Populate(action.Ctx, record)
stream.Send(sse.Event{ID: res.PagingToken(), Data: res})
}
},
)
}
func (action *OffersByAccountAction) loadParams() {
action.PageQuery = action.GetPageQuery()
action.Address = action.GetString("account_id")
}
func (action *OffersByAccountAction) loadRecords() {
action.Err = action.CoreQ().OffersByAddress(
&action.Records,
action.Address,
action.PageQuery,
)
}
func (action *OffersByAccountAction) loadPage() {
for _, record := range action.Records {
var res resource.Offer
res.Populate(action.Ctx, record)
action.Page.Add(res)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PageQuery.Limit
action.Page.Cursor = action.PageQuery.Cursor
action.Page.Order = action.PageQuery.Order
action.Page.PopulateLinks()
}