-
Notifications
You must be signed in to change notification settings - Fork 3
/
filters.go
285 lines (233 loc) · 7.18 KB
/
filters.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package rawg
import (
"fmt"
"strings"
"time"
)
// DateRange - two dates: from and to
type DateRange struct {
From time.Time
To time.Time
}
// GamesFilter is a filter to search games in RAWG
type GamesFilter struct {
page int
pageSize int
search string
parentPlatforms []int
platforms []int
stores []int
developers []interface{}
publishers []interface{}
genres []interface{}
tags []interface{}
creators []interface{}
dates []DateRange
platformsCount int
excludeCollection int
excludeAdditions bool
excludeParents bool
excludeGameSeries bool
searchPrecise bool
searchExact bool
ordering string
metacriticFrom int
metacriticTo int
updated []time.Time
}
// NewGamesFilter creates filter
func NewGamesFilter() *GamesFilter {
return &GamesFilter{}
}
// SetPage sets "page" parameter
func (filter *GamesFilter) SetPage(page int) *GamesFilter {
filter.page = page
return filter
}
// SetPageSize sets "page_size" parameter
func (filter *GamesFilter) SetPageSize(pageSize int) *GamesFilter {
filter.pageSize = pageSize
return filter
}
// SetSearch sets "search" parameter
func (filter *GamesFilter) SetSearch(search string) *GamesFilter {
filter.search = search
return filter
}
// SetParentPlatforms sets "parent_platforms" parameter
func (filter *GamesFilter) SetParentPlatforms(parentPlatforms ...int) *GamesFilter {
filter.parentPlatforms = parentPlatforms
return filter
}
// SetPlatforms sets "platforms" parameter
func (filter *GamesFilter) SetPlatforms(platforms ...int) *GamesFilter {
filter.platforms = platforms
return filter
}
// SetStores sets "stores" parameter
func (filter *GamesFilter) SetStores(stores ...int) *GamesFilter {
filter.stores = stores
return filter
}
// SetDevelopers sets "developers" parameter
func (filter *GamesFilter) SetDevelopers(developers ...interface{}) *GamesFilter {
filter.developers = developers
return filter
}
// SetPublishers sets "publishers" parameter
func (filter *GamesFilter) SetPublishers(publishers ...interface{}) *GamesFilter {
filter.publishers = publishers
return filter
}
// SetGenres sets "genres" parameter
func (filter *GamesFilter) SetGenres(genres ...interface{}) *GamesFilter {
filter.genres = genres
return filter
}
// SetTags sets "tags" parameter
func (filter *GamesFilter) SetTags(tags ...interface{}) *GamesFilter {
filter.tags = tags
return filter
}
// SetCreators sets "creators" parameter
func (filter *GamesFilter) SetCreators(creators ...interface{}) *GamesFilter {
filter.creators = creators
return filter
}
// SetMetacritic sets "metacritic" parameter
func (filter *GamesFilter) SetMetacritic(from int, to int) *GamesFilter {
filter.metacriticFrom = from
filter.metacriticTo = to
return filter
}
// SetDates sets "dates" parameter
func (filter *GamesFilter) SetDates(ranges ...DateRange) *GamesFilter {
filter.dates = ranges
return filter
}
// SetUpdated sets "updated" parameter
func (filter *GamesFilter) SetUpdated(dates ...time.Time) *GamesFilter {
filter.updated = dates
return filter
}
// SetPlatformsCount sets "platforms_count" parameter
func (filter *GamesFilter) SetPlatformsCount(count int) *GamesFilter {
filter.platformsCount = count
return filter
}
// ExcludeCollection sets "exclude_collection" parameter
func (filter *GamesFilter) ExcludeCollection(collection int) *GamesFilter {
filter.excludeCollection = collection
return filter
}
// WithoutAdditions sets "exclude_additions" parameter
func (filter *GamesFilter) WithoutAdditions() *GamesFilter {
filter.excludeAdditions = true
return filter
}
// WithoutParents sets "exclude_parents" parameter
func (filter *GamesFilter) WithoutParents() *GamesFilter {
filter.excludeParents = true
return filter
}
// WithoutGameSeries sets "exclude_game_series" parameter
func (filter *GamesFilter) WithoutGameSeries() *GamesFilter {
filter.excludeGameSeries = true
return filter
}
// WithSearchExact sets "search_exact" parameter
func (filter *GamesFilter) WithSearchExact() *GamesFilter {
filter.searchExact = true
return filter
}
// WithSearchPrecise sets "search_precise" parameter
func (filter *GamesFilter) WithSearchPrecise() *GamesFilter {
filter.searchPrecise = true
return filter
}
// SetOrdering sets results ordering
func (filter *GamesFilter) SetOrdering(ordering string) *GamesFilter {
filter.ordering = ordering
return filter
}
// GetParams returns filter parameters as Map
func (filter *GamesFilter) GetParams() map[string]interface{} {
params := make(map[string]interface{})
params["page"] = 1
if filter.page != 0 {
params["page"] = filter.page
}
params["page_size"] = 20
if filter.pageSize != 0 {
params["page_size"] = filter.pageSize
}
if filter.search != "" {
params["search"] = filter.search
}
if len(filter.parentPlatforms) != 0 {
params["parent_platforms"] = strings.Trim(strings.Replace(fmt.Sprint(filter.parentPlatforms), " ", ",", -1), "[]")
}
if len(filter.platforms) != 0 {
params["platforms"] = strings.Trim(strings.Replace(fmt.Sprint(filter.platforms), " ", ",", -1), "[]")
}
if len(filter.stores) != 0 {
params["stores"] = strings.Trim(strings.Replace(fmt.Sprint(filter.stores), " ", ",", -1), "[]")
}
if len(filter.developers) != 0 {
params["developers"] = strings.Trim(strings.Replace(fmt.Sprint(filter.developers), " ", ",", -1), "[]")
}
if len(filter.publishers) != 0 {
params["publishers"] = strings.Trim(strings.Replace(fmt.Sprint(filter.publishers), " ", ",", -1), "[]")
}
if len(filter.genres) != 0 {
params["genres"] = strings.Trim(strings.Replace(fmt.Sprint(filter.genres), " ", ",", -1), "[]")
}
if len(filter.tags) != 0 {
params["tags"] = strings.Trim(strings.Replace(fmt.Sprint(filter.tags), " ", ",", -1), "[]")
}
if len(filter.creators) != 0 {
params["creators"] = strings.Trim(strings.Replace(fmt.Sprint(filter.creators), " ", ",", -1), "[]")
}
if len(filter.dates) != 0 {
parts := make([]string, len(filter.dates))
for i, dateRange := range filter.dates {
parts[i] = dateRange.From.Format("2006-01-02") + "," + dateRange.To.Format("2006-01-02")
}
params["dates"] = strings.Join(parts, ".")
}
if len(filter.updated) != 0 {
parts := make([]string, len(filter.updated))
for i, d := range filter.updated {
parts[i] = d.Format("2006-01-02")
}
params["updated"] = strings.Join(parts, ",")
}
if filter.platformsCount != 0 {
params["platforms_count"] = filter.platformsCount
}
if filter.excludeCollection != 0 {
params["exclude_collection"] = filter.excludeCollection
}
if filter.excludeAdditions {
params["exclude_additions"] = filter.excludeAdditions
}
if filter.excludeParents {
params["exclude_parents"] = filter.excludeParents
}
if filter.excludeGameSeries {
params["exclude_game_series"] = filter.excludeGameSeries
}
if filter.ordering != "" {
params["ordering"] = filter.ordering
}
if filter.metacriticFrom != 0 || filter.metacriticTo != 0 {
params["metacritic"] = fmt.Sprintf("%d,%d", filter.metacriticFrom, filter.metacriticTo)
}
if filter.searchExact {
params["search_exact"] = filter.searchExact
}
if filter.searchPrecise {
params["search_precise"] = filter.searchPrecise
}
return params
}