-
Notifications
You must be signed in to change notification settings - Fork 189
/
meta_datetime.go
81 lines (71 loc) · 1.93 KB
/
meta_datetime.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
package admin
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
)
// DatetimeConfig meta configuration used for datetime
type DatetimeConfig struct {
MinTime *time.Time
MaxTime *time.Time
ShowTime bool
}
// ConfigureQorMeta configure datetime meta
func (datetimeConfig *DatetimeConfig) ConfigureQorMeta(metaor resource.Metaor) {
if meta, ok := metaor.(*Meta); ok {
timeFormat := "2006-01-02"
if meta.Type == "datetime" {
datetimeConfig.ShowTime = true
}
if meta.Type == "" {
meta.Type = "datetime"
}
if datetimeConfig.ShowTime {
timeFormat = "2006-01-02 15:04"
}
if meta.FormattedValuer == nil {
meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
switch date := meta.GetValuer()(value, context).(type) {
case *time.Time:
if date == nil {
return ""
}
if date.IsZero() {
return ""
}
return utils.FormatTime(*date, timeFormat, context)
case time.Time:
if date.IsZero() {
return ""
}
return utils.FormatTime(date, timeFormat, context)
default:
return date
}
})
}
}
}
// ConfigureQORAdminFilter configure admin filter for datetime
func (datetimeConfig *DatetimeConfig) ConfigureQORAdminFilter(filter *Filter) {
if filter.Handler == nil {
if dbName := filter.Resource.GetMeta(filter.Name).DBName(); dbName != "" {
filter.Handler = func(tx *gorm.DB, filterArgument *FilterArgument) *gorm.DB {
if metaValue := filterArgument.Value.Get("Start"); metaValue != nil {
if start := utils.ToString(metaValue.Value); start != "" {
tx = tx.Where(fmt.Sprintf("%v > ?", dbName), start)
}
}
if metaValue := filterArgument.Value.Get("End"); metaValue != nil {
if end := utils.ToString(metaValue.Value); end != "" {
tx = tx.Where(fmt.Sprintf("%v < ?", dbName), end)
}
}
return tx
}
}
}
}