-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.go
102 lines (90 loc) · 2.86 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
package cosy
import (
"github.com/uozi-tech/cosy/filter"
"github.com/uozi-tech/cosy/logger"
"github.com/uozi-tech/cosy/model"
"gorm.io/gorm"
)
func (c *Ctx[T]) SetFussy(keys ...string) *Ctx[T] {
c.fussy = append(c.fussy, keys...)
for _, key := range keys {
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToFussySearch(c.Context, tx, key)
})
}
return c
}
func (c *Ctx[T]) SetSearchFussyKeys(keys ...string) *Ctx[T] {
c.search = append(c.search, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToFussyKeysSearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetEqual(keys ...string) *Ctx[T] {
c.eq = append(c.eq, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToEqualSearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetIn(keys ...string) *Ctx[T] {
c.in = append(c.in, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueriesToInSearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetInWithKey(value string, key string) *Ctx[T] {
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToInSearch(c.Context, tx, value, key)
})
return c
}
func (c *Ctx[T]) SetOrFussy(keys ...string) *Ctx[T] {
c.orFussy = append(c.orFussy, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToOrFussySearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetOrEqual(keys ...string) *Ctx[T] {
c.orEq = append(c.orEq, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToOrEqualSearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetBetween(keys ...string) *Ctx[T] {
c.between = append(c.between, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueriesToBetweenSearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetBetweenWithKey(value string, key string) *Ctx[T] {
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToBetweenSearch(c.Context, tx, value, key)
})
return c
}
func (c *Ctx[T]) SetOrIn(keys ...string) *Ctx[T] {
c.orIn = append(c.orIn, keys...)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
return filter.QueryToOrInSearch(c.Context, tx, keys...)
})
return c
}
func (c *Ctx[T]) SetCustomFilter(key string, filterName string) *Ctx[T] {
customFilter := filter.FilterMap[filterName]
if customFilter == nil {
logger.Errorf("Filter not found: %s", filterName)
return c
}
c.customFilters.Set(key, filterName)
c.gormScopes = append(c.gormScopes, func(tx *gorm.DB) *gorm.DB {
resolvedModel := model.GetResolvedModel[T]()
return customFilter.Filter(c.Context, tx, key, resolvedModel.Fields[key], resolvedModel)
})
return c
}