/
facets.go
330 lines (281 loc) · 6.87 KB
/
facets.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package searchdto
import searchdomain "flamingo.me/flamingo-commerce/v3/search/domain"
// CommerceSearchFacet interface for facets
type CommerceSearchFacet interface {
Name() string
Label() string
Position() int
//Items() []CommerceSearchFacetItem
HasSelectedItem() bool
}
// CommerceSearchFacetItem interface for facet items
type CommerceSearchFacetItem interface {
Label() string
Value() string
Selected() bool
Count() int
}
// WrapListFacet wraps the list facet into the graphql dto
func WrapListFacet(facet searchdomain.Facet) *CommerceSearchListFacet {
items := make([]*CommerceSearchListFacetItem, len(facet.Items))
for i, item := range facet.Items {
items[i] = &CommerceSearchListFacetItem{
label: item.Label,
value: item.Value,
selected: item.Selected,
count: int(item.Count),
}
}
return &CommerceSearchListFacet{
name: facet.Name,
label: facet.Label,
position: facet.Position,
items: items,
}
}
// CommerceSearchListFacet dto for list facets
type CommerceSearchListFacet struct {
name string
label string
position int
items []*CommerceSearchListFacetItem
}
// Name getter
func (c *CommerceSearchListFacet) Name() string {
return c.name
}
// Label getter
func (c *CommerceSearchListFacet) Label() string {
return c.label
}
// Position getter
func (c *CommerceSearchListFacet) Position() int {
return c.position
}
// Items getter
func (c *CommerceSearchListFacet) Items() []*CommerceSearchListFacetItem {
return c.items
}
// HasSelectedItem getter
func (c *CommerceSearchListFacet) HasSelectedItem() bool {
for _, item := range c.items {
if item.selected {
return true
}
}
return false
}
// CommerceSearchListFacetItem dto for list facet items
type CommerceSearchListFacetItem struct {
label string
value string
selected bool
count int
}
// Label getter
func (c *CommerceSearchListFacetItem) Label() string {
return c.label
}
// Value getter
func (c *CommerceSearchListFacetItem) Value() string {
return c.value
}
// Selected getter
func (c *CommerceSearchListFacetItem) Selected() bool {
return c.selected
}
// Count getter
func (c *CommerceSearchListFacetItem) Count() int {
return c.count
}
func mapTreeFacetItems(facetItems []*searchdomain.FacetItem) []*CommerceSearchTreeFacetItem {
items := make([]*CommerceSearchTreeFacetItem, len(facetItems))
for i, item := range facetItems {
items[i] = &CommerceSearchTreeFacetItem{
label: item.Label,
value: item.Value,
selected: item.Selected,
count: int(item.Count),
active: item.Active,
items: mapTreeFacetItems(item.Items),
}
}
return items
}
// WrapTreeFacet wraps the tree facet into the graphql dto
func WrapTreeFacet(facet searchdomain.Facet) *CommerceSearchTreeFacet {
return &CommerceSearchTreeFacet{
name: facet.Name,
label: facet.Label,
position: facet.Position,
items: mapTreeFacetItems(facet.Items),
}
}
// CommerceSearchTreeFacet dto for tree facets
type CommerceSearchTreeFacet struct {
name string
label string
position int
items []*CommerceSearchTreeFacetItem
}
// Name getter
func (c *CommerceSearchTreeFacet) Name() string {
return c.name
}
// Label getter
func (c *CommerceSearchTreeFacet) Label() string {
return c.label
}
// Position getter
func (c *CommerceSearchTreeFacet) Position() int {
return c.position
}
// Items getter
func (c *CommerceSearchTreeFacet) Items() []*CommerceSearchTreeFacetItem {
return c.items
}
func hasSelectedItem(items []*CommerceSearchTreeFacetItem) bool {
for _, item := range items {
if item.selected || hasSelectedItem(item.items) {
return true
}
}
return false
}
// HasSelectedItem getter
func (c *CommerceSearchTreeFacet) HasSelectedItem() bool {
if hasSelectedItem(c.items) {
return true
}
return false
}
// CommerceSearchTreeFacetItem dto for tree facet items
type CommerceSearchTreeFacetItem struct {
label string
value string
selected bool
count int
active bool
items []*CommerceSearchTreeFacetItem
}
// Label getter
func (c *CommerceSearchTreeFacetItem) Label() string {
return c.label
}
// Value getter
func (c *CommerceSearchTreeFacetItem) Value() string {
return c.value
}
// Selected getter
func (c *CommerceSearchTreeFacetItem) Selected() bool {
return c.selected
}
// Count getter
func (c *CommerceSearchTreeFacetItem) Count() int {
return c.count
}
// Active getter
func (c *CommerceSearchTreeFacetItem) Active() bool {
return c.active
}
// Items getter
func (c *CommerceSearchTreeFacetItem) Items() []*CommerceSearchTreeFacetItem {
return c.items
}
// WrapRangeFacet wraps the range facet into the graphql dto
func WrapRangeFacet(facet searchdomain.Facet) *CommerceSearchRangeFacet {
items := make([]*CommerceSearchRangeFacetItem, len(facet.Items))
for i, item := range facet.Items {
items[i] = &CommerceSearchRangeFacetItem{
label: item.Label,
value: item.Value,
selected: item.Selected,
count: int(item.Count),
min: int(item.Min),
max: int(item.Max),
selectedMin: int(item.SelectedMin),
selectedMax: int(item.SelectedMax),
}
}
return &CommerceSearchRangeFacet{
name: facet.Name,
label: facet.Label,
position: facet.Position,
items: items,
}
}
// CommerceSearchRangeFacet dto for range facets
type CommerceSearchRangeFacet struct {
name string
label string
position int
items []*CommerceSearchRangeFacetItem
}
// Name getter
func (c *CommerceSearchRangeFacet) Name() string {
return c.name
}
// Label getter
func (c *CommerceSearchRangeFacet) Label() string {
return c.label
}
// Position getter
func (c *CommerceSearchRangeFacet) Position() int {
return c.position
}
// Items getter
func (c *CommerceSearchRangeFacet) Items() []*CommerceSearchRangeFacetItem {
return c.items
}
// HasSelectedItem getter
func (c *CommerceSearchRangeFacet) HasSelectedItem() bool {
for _, item := range c.items {
if item.selected {
return true
}
}
return false
}
// CommerceSearchRangeFacetItem dto for range facet items
type CommerceSearchRangeFacetItem struct {
label string
value string
selected bool
count int
min int
max int
selectedMin int
selectedMax int
}
// Label getter
func (c *CommerceSearchRangeFacetItem) Label() string {
return c.label
}
// Value getter
func (c *CommerceSearchRangeFacetItem) Value() string {
return c.value
}
// Selected getter
func (c *CommerceSearchRangeFacetItem) Selected() bool {
return c.selected
}
// Count getter
func (c *CommerceSearchRangeFacetItem) Count() int {
return c.count
}
// Min getter
func (c *CommerceSearchRangeFacetItem) Min() int {
return c.min
}
// Max getter
func (c *CommerceSearchRangeFacetItem) Max() int {
return c.max
}
// SelectedMin getter
func (c *CommerceSearchRangeFacetItem) SelectedMin() int {
return c.selectedMin
}
// SelectedMax getter
func (c *CommerceSearchRangeFacetItem) SelectedMax() int {
return c.selectedMax
}