flamingo.me/flamingo-commerce/v3@v3.11.0/search/domain/filter.go (about) 1 package domain 2 3 import ( 4 "strconv" 5 ) 6 7 type ( 8 // Filter interface for search queries 9 Filter interface { 10 // Value very generic method for filters - returning typical Parameter Name and its setted values 11 Value() (string, []string) 12 } 13 14 // KeyValueFilter allows simple k -> []values filtering 15 KeyValueFilter struct { 16 k string 17 v []string 18 } 19 20 // SortFilter - specifies the request to sort by some criteria(label) in a certain direction. Possible values for label and direction should be in SearchMeta.SortOption 21 SortFilter struct { 22 field string 23 direction string 24 } 25 26 // QueryFilter - represents a query string, normally given by a user in the search result 27 QueryFilter struct { 28 query string 29 } 30 31 // PaginationPage - if search supports pagination this filter tells which page to return 32 PaginationPage struct { 33 page int 34 } 35 36 // PaginationPageSize - if search supports setting the amount (limit) per page 37 PaginationPageSize struct { 38 pageSize int 39 } 40 ) 41 42 var ( 43 _ Filter = NewKeyValueFilter("a", []string{"b", "c"}) 44 ) 45 46 const ( 47 // SortDirectionAscending general asc value 48 SortDirectionAscending = "A" 49 50 // SortDirectionDescending general desc value 51 SortDirectionDescending = "D" 52 53 // SortDirectionNone general not set value 54 SortDirectionNone = "" 55 ) 56 57 // NewKeyValueFilters - Factory method that you can use to get a list of KeyValueFilter based from url.Values 58 func NewKeyValueFilters(params map[string][]string) []Filter { 59 var result []Filter 60 for k, v := range params { 61 if len(v) == 0 { 62 continue 63 } 64 result = append(result, NewKeyValueFilter(k, v)) 65 } 66 return result 67 } 68 69 // NewKeyValueFilter factory 70 func NewKeyValueFilter(k string, v []string) *KeyValueFilter { 71 return &KeyValueFilter{ 72 k: k, 73 v: v, 74 } 75 } 76 77 // Value of the current filter 78 func (f *KeyValueFilter) Value() (string, []string) { 79 return f.k, f.v 80 } 81 82 // KeyValues of the current filter 83 func (f *KeyValueFilter) KeyValues() []string { 84 return f.v 85 } 86 87 // Key of the current filter 88 func (f *KeyValueFilter) Key() string { 89 return f.k 90 } 91 92 // NewSortFilter factory 93 func NewSortFilter(label string, direction string) *SortFilter { 94 if direction != SortDirectionNone && direction != SortDirectionDescending && direction != SortDirectionAscending { 95 direction = SortDirectionNone 96 } 97 return &SortFilter{ 98 field: label, 99 direction: direction, 100 } 101 } 102 103 // Value of the current filter 104 func (f *SortFilter) Value() (string, []string) { 105 return f.field, []string{f.direction} 106 } 107 108 // Field of the current filter 109 func (f *SortFilter) Field() string { 110 return f.field 111 } 112 113 // Direction of the current filter 114 func (f *SortFilter) Direction() string { 115 return f.direction 116 } 117 118 // Descending returns true if sort order is descending 119 func (f *SortFilter) Descending() bool { 120 return f.direction == SortDirectionDescending 121 } 122 123 // NewQueryFilter factory 124 func NewQueryFilter(query string) *QueryFilter { 125 return &QueryFilter{ 126 query: query, 127 } 128 } 129 130 // Value of the current filter 131 func (f *QueryFilter) Value() (string, []string) { 132 return "q", []string{f.query} 133 } 134 135 // Query of the current filter 136 func (f *QueryFilter) Query() string { 137 return f.query 138 } 139 140 // NewPaginationPageFilter factory 141 func NewPaginationPageFilter(page int) *PaginationPage { 142 return &PaginationPage{ 143 page: page, 144 } 145 } 146 147 // Value of the current filter 148 func (f *PaginationPage) Value() (string, []string) { 149 return "page", []string{strconv.Itoa(f.page)} 150 } 151 152 // GetPage of the current filter 153 func (f *PaginationPage) GetPage() int { 154 return f.page 155 } 156 157 // NewPaginationPageSizeFilter factory 158 func NewPaginationPageSizeFilter(page int) *PaginationPageSize { 159 return &PaginationPageSize{ 160 pageSize: page, 161 } 162 } 163 164 // Value of the current filter 165 func (f *PaginationPageSize) Value() (string, []string) { 166 return "limit", []string{strconv.Itoa(f.pageSize)} 167 } 168 169 // GetPageSize of the current filter 170 func (f *PaginationPageSize) GetPageSize() int { 171 return f.pageSize 172 }