-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
120 lines (80 loc) · 2.2 KB
/
action.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 happyngine
import (
"github.com/wayt/happyngine/validator"
)
type ActionHandler func(*Context) ActionInterface
type ActionInterface interface {
Run()
IsValid() bool
Send(int, string, ...string)
}
type Action struct {
Context *Context
Form *Form
Parameters []*Parameter
}
func (this *Action) IsValid() bool {
if !this.oldIsValid() {
return false
}
// In case of custom AddError in action New
if this.HasErrors() {
return false
}
if this.Form == nil {
return true
}
return this.Form.IsValid()
}
func (this *Action) oldIsValid() bool {
request := this.Context.Request
isValid := true
for _, parameter := range this.Parameters {
name := parameter.Name
err := parameter.IsValid(request.FormValue(name))
if err != nil {
this.AddError(400, err.Error())
isValid = false
}
}
return isValid
}
func (this *Action) HasErrors() bool {
return this.Context.HasErrors()
}
func (this *Action) GetErrors() ([]string, int) {
return this.Context.GetErrors()
}
func (this *Action) AddParameter(name string, required bool, validators ...*validator.Validator) {
this.Parameters = append(this.Parameters, NewParameter(name, required, validators...))
}
func (this *Action) AddError(code int, text string) {
this.Context.AddError(code, text)
}
func (this *Action) GetParam(key string) string {
return this.Context.GetParam(key)
}
func (this *Action) GetIntParam(key string) int {
return this.Context.GetIntParam(key)
}
func (this *Action) GetInt64Param(key string) int64 {
return this.Context.GetInt64Param(key)
}
func (this *Action) GetURLParam(key string) string {
return this.Context.GetURLParam(key)
}
func (this *Action) GetURLIntParam(key string) int {
return this.Context.GetURLIntParam(key)
}
func (this *Action) GetURLInt64Param(key string) int64 {
return this.Context.GetURLInt64Param(key)
}
func (this *Action) Send(code int, text string, headers ...string) {
this.Context.SendByte(code, []byte(text), headers...)
}
func (this *Action) SendByte(code int, data []byte, headers ...string) {
this.Context.SendByte(code, data, headers...)
}
func (this *Action) JSON(code int, obj interface{}, headers ...string) {
this.Context.JSON(code, obj, headers...)
}