-
Notifications
You must be signed in to change notification settings - Fork 0
/
req.go
128 lines (112 loc) · 2.99 KB
/
req.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
121
122
123
124
125
126
127
128
package typ
import (
"fmt"
"time"
)
type LoginReq struct {
// 4-24
Username string `json:"username" binding:"required,gte=4,lte=24"`
// must md5
Password string `json:"password" binding:"required,len=32"`
}
const dateTime = "2006-01-02 15:04:05"
type TimeStringReq struct {
// format 2006-01-02 15:04:05
StartTime string `json:"startTime" form:"startTime"`
EndTime string `json:"endTime" form:"endTime"`
}
func (req TimeStringReq) FieldSQLCond(field string) []string {
cond := make([]string, 0)
if field == "" {
return cond
}
if req.StartTime != "" {
_, err := time.Parse(dateTime, req.StartTime)
if err == nil {
cond = append(cond, fmt.Sprintf("%s >= %s", field, req.StartTime))
}
}
if req.EndTime != "" {
_, err := time.Parse(dateTime, req.EndTime)
if err == nil {
cond = append(cond, fmt.Sprintf("%s < %s", field, req.EndTime))
}
}
return cond
}
func (req TimeStringReq) FieldMgoBson() (map[string]interface{}, bool) {
filter := map[string]interface{}{}
if req.StartTime != "" {
s, err := time.Parse(dateTime, req.StartTime)
if err == nil {
filter["$gte"] = s
}
}
if req.EndTime != "" {
e, err := time.Parse(dateTime, req.EndTime)
if err == nil {
filter["$lt"] = e
}
}
if len(filter) == 0 {
return filter, false
}
return filter, true
}
type TimestampReq struct {
// Unix timestamp Sec
StartTimestamp int64 `json:"startTimestamp" form:"startTimestamp,default=0" binding:"omitempty,min=0"`
EndTimestamp int64 `json:"endTimestamp" form:"endTimestamp,default=0" binding:"omitempty,gtefield=StartTimestamp"`
}
func (req TimestampReq) FieldSQLCond(field string) []string {
cond := make([]string, 0)
if field == "" {
return cond
}
if req.StartTimestamp > 0 {
s := time.Unix(req.StartTimestamp, 0)
cond = append(cond, fmt.Sprintf("%s >= %s", field, s.Format(dateTime)))
}
if req.EndTimestamp > 0 {
e := time.Unix(req.EndTimestamp, 0)
cond = append(cond, fmt.Sprintf("%s < %s", field, e.Format(dateTime)))
}
return cond
}
func (req TimestampReq) FieldMgoBson() (map[string]interface{}, bool) {
filter := map[string]interface{}{}
if req.StartTimestamp > 0 {
s := time.Unix(req.StartTimestamp, 0)
filter["$gte"] = s
}
if req.EndTimestamp > 0 {
e := time.Unix(req.EndTimestamp, 0)
filter["$lt"] = e
}
if len(filter) == 0 {
return filter, false
}
return filter, true
}
type PageReq struct {
Page int `json:"page" form:"page,default=1" binding:"required,gte=1"`
Size int `json:"size" form:"size,default=20" binding:"required,gte=1,lte=1000"`
}
func (req PageReq) LimitStart() (limit, start int) {
return req.Size, (req.Page - 1) * req.Size
}
func (req PageReq) Skip() int64 {
return int64((req.Page - 1) * req.Size)
}
type IdReq struct {
Id int64 `json:"id" form:"id" binding:"required,gt=0"`
}
type IdOmitReq struct {
IdReq `json:"id" form:"id" binding:"omitempty,gt=0"`
}
type UidReq struct {
Uid int64 `json:"uid" form:"uid" binding:"required,min=1"`
}
type UidOmitReq struct {
UidReq `json:"uid" form:"uid" binding:"omitempty,min=1"`
}