-
Notifications
You must be signed in to change notification settings - Fork 4
/
filter.go
57 lines (49 loc) · 1.02 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
/*
Filter maintains various filters from http forms (get/post)
*/
package core
import (
"net/http"
"github.com/gorilla/schema"
"github.com/jinzhu/gorm"
)
/*
BindFilter binds filter to given url values
*/
func BindFilter(r *http.Request, filter Filter) (err error) {
decoder := schema.NewDecoder()
if err = decoder.Decode(filter, r.URL.Query()); err != nil {
return
}
return
}
/*
Filter is interface to filter database querysets
*/
type Filter interface {
// returns list of filter funcs
Apply(queryset *gorm.DB) *gorm.DB
}
/*
NewUserListFilter returns new UserListFilter
*/
func NewUserListFilter(r *http.Request) Filter {
return UserListFilter{
IsActive: StringParseBool(r.URL.Query().Get("is_active")),
}
}
/*
UserListFilter filters users from url
*/
type UserListFilter struct {
IsActive *bool
}
/*
Apply applies filter to queryset
*/
func (u UserListFilter) Apply(queryset *gorm.DB) *gorm.DB {
if u.IsActive != nil {
queryset = ApplyFilterFuncs(queryset, FFWhere("is_active = ?", u.IsActive))
}
return queryset
}