This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
120 lines (103 loc) · 3.95 KB
/
filter.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
package emvi
import (
"net/url"
"strconv"
"strings"
"time"
)
const (
DateFormat = "2006-01-02"
SortAscending = SortDirection("asc")
SortDescending = SortDirection("desc")
)
type SortDirection string
// Filter is used to filter results.
type Filter interface {
addParams(values *url.Values)
}
type BaseSearch struct {
CreatedStart time.Time `json:"created_start"`
CreatedEnd time.Time `json:"created_end"`
UpdatedStart time.Time `json:"updated_start"`
UpdatedEnd time.Time `json:"updated_end"`
SortCreated SortDirection `json:"sort_created"`
SortUpdated SortDirection `json:"sort_updated"`
Offset int `json:"offset"`
Limit int `json:"limit"`
}
func (filter *BaseSearch) addParams(query *url.Values) {
addParamIfNotEmpty(query, "created_start", dateToString(filter.CreatedStart))
addParamIfNotEmpty(query, "created_end", dateToString(filter.CreatedEnd))
addParamIfNotEmpty(query, "updated_start", dateToString(filter.UpdatedStart))
addParamIfNotEmpty(query, "updated_end", dateToString(filter.UpdatedEnd))
addParamIfNotEmpty(query, "sort_created", string(filter.SortCreated))
addParamIfNotEmpty(query, "sort_updated", string(filter.SortUpdated))
addParamIfNotEmpty(query, "offset", intToString(filter.Offset))
addParamIfNotEmpty(query, "limit", intToString(filter.Limit))
}
type ArticleFilter struct {
BaseSearch
LanguageId string `json:"language_id"`
Archived bool `json:"archived"`
WIP bool `json:"wip"`
ClientAccess bool `json:"client_access"`
Preview bool `json:"preview"`
PreviewParagraph bool `json:"preview_paragraph"`
PreviewImage bool `json:"preview_image"`
Title string `json:"title"`
Content string `json:"content"`
Tags string `json:"tags"`
TagIds []string `json:"tag_ids"`
AuthorUserIds []string `json:"authors"`
Commits string `json:"commits"`
PublishedStart time.Time `json:"published_start"`
PublishedEnd time.Time `json:"published_end"`
SortTitle SortDirection `json:"sort_title"`
SortPublished SortDirection `json:"sort_published"`
}
func (filter *ArticleFilter) addParams(query *url.Values) {
filter.BaseSearch.addParams(query)
addParamIfNotEmpty(query, "language_id", filter.LanguageId)
addParamIfNotEmpty(query, "archived", boolToString(filter.Archived))
addParamIfNotEmpty(query, "wip", boolToString(filter.WIP))
addParamIfNotEmpty(query, "client_access", boolToString(filter.ClientAccess))
addParamIfNotEmpty(query, "preview", boolToString(filter.Preview))
addParamIfNotEmpty(query, "preview_paragraph", boolToString(filter.PreviewParagraph))
addParamIfNotEmpty(query, "preview_image", boolToString(filter.PreviewImage))
addParamIfNotEmpty(query, "title", filter.Title)
addParamIfNotEmpty(query, "content", filter.Content)
addParamIfNotEmpty(query, "tags", filter.Tags)
addParamIfNotEmpty(query, "tag_ids", sliceToString(filter.TagIds))
addParamIfNotEmpty(query, "authors", sliceToString(filter.AuthorUserIds))
addParamIfNotEmpty(query, "commits", filter.Commits)
addParamIfNotEmpty(query, "published_start", dateToString(filter.PublishedStart))
addParamIfNotEmpty(query, "published_end", dateToString(filter.PublishedEnd))
addParamIfNotEmpty(query, "sort_title", string(filter.SortTitle))
addParamIfNotEmpty(query, "sort_published", string(filter.SortPublished))
}
func addParamIfNotEmpty(query *url.Values, key, value string) {
if value != "" {
query.Add(key, value)
}
}
func dateToString(date time.Time) string {
if date.IsZero() {
return "" // ignore in URL
}
return date.Format(DateFormat)
}
func boolToString(b bool) string {
if b {
return "true"
}
return "" // ignore in URL
}
func intToString(i int) string {
if i == 0 {
return ""
}
return strconv.Itoa(i)
}
func sliceToString(slice []string) string {
return strings.Join(slice, ",")
}