-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_sale_index.go
170 lines (147 loc) · 3.9 KB
/
actions_sale_index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package horizon
import (
"fmt"
"gitlab.com/tokend/horizon/db2"
"time"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type Sort int64
const (
SortTypeDefaultPage Sort = iota
SortTypeMostFounded
SortTypeByEndTime
SortTypeByPopularity
SortTypeStartTime
)
// SaleIndexAction renders slice of reviewable requests
type SaleIndexAction struct {
Action
Owner string
BaseAsset string
OpenOnly bool
Upcoming bool
Voting bool
Promotions bool
SortType *int64
Name string
Records []history.Sale
PagingParams db2.PageQueryV2
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *SaleIndexAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.loadRecord,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *SaleIndexAction) loadParams() {
action.PagingParams = action.GetPageQueryV2()
action.Owner = action.GetString("owner")
action.BaseAsset = action.GetString("base_asset")
action.Name = action.GetString("name")
// TODO: refactoring required: switch to state
action.OpenOnly = action.GetBool("open_only")
action.Upcoming = action.GetBool("upcoming")
action.Voting = action.GetBool("voting")
action.Promotions = action.GetBool("promotions")
action.SortType = action.GetOptionalInt64("sort_by")
action.Page.Filters = map[string]string{
"owner": action.Owner,
"base_asset": action.BaseAsset,
"name": action.Name,
"open_only": action.GetString("open_only"),
"upcoming": action.GetString("upcoming"),
"voting": action.GetString("voting"),
"promotions": action.GetString("promotions"),
"sort_by": action.GetString("sort_by"),
}
}
func (action *SaleIndexAction) loadRecord() {
q := action.HistoryQ().Sales().PageV2(action.PagingParams)
if action.Owner != "" {
q = q.ForOwner(action.Owner)
}
if action.BaseAsset != "" {
q = q.ForBaseAsset(action.BaseAsset)
}
if action.Name != "" {
q = q.ForName(action.Name)
}
if action.OpenOnly {
q = q.Open(time.Now().UTC())
}
if action.Upcoming {
q = q.Upcoming(time.Now().UTC())
}
if action.Voting {
q = q.Voting()
}
if action.Promotions {
q = q.Promotions()
}
sortBy := SortTypeDefaultPage
if action.SortType != nil {
sortBy = Sort(*action.SortType)
}
switch sortBy {
case SortTypeDefaultPage:
// FIXME tmp:
q = q.OrderById("asc")
case SortTypeStartTime:
q = q.OrderByStartTime()
case SortTypeByEndTime:
q = q.OrderByEndTime()
case SortTypeByPopularity:
values, err := action.CoreQ().OrderBook().InvestorsCount()
if err != nil {
action.Log.WithError(err).Error("Unable to load investors count")
action.Err = &problem.ServerError
return
}
q = q.OrderByPopularity(values)
default:
action.SetInvalidField("sort_by", fmt.Errorf("invalid value %d", sortBy))
return
}
converter, err := action.CreateConverter()
if err != nil {
action.Log.WithError(err).Error("Failed to init converter")
action.Err = &problem.ServerError
return
}
action.Records, err = selectSalesWithCurrentCap(q, converter)
if err != nil {
action.Log.WithError(err).Error("failed to load sales")
action.Err = &problem.ServerError
return
}
}
func (action *SaleIndexAction) loadPage() {
for i := range action.Records {
var res resource.Sale
res.Populate(&action.Records[i])
err := populateSaleWithStats(action.Records[i].ID, &res, action.CoreQ())
if err != nil {
action.Log.WithError(err).
WithField("sale_id", action.Records[i].ID).
Error("failed to populate stat for sale")
action.Err = &problem.ServerError
return
}
action.Page.Add(&res)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Page = action.PagingParams.Page
action.Page.Limit = action.PagingParams.Limit
action.Page.PopulateLinksV2()
}