-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.go
146 lines (127 loc) · 3.01 KB
/
forms.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"errors"
"strconv"
uos "github.com/mschoebel/urban-octo-succotash"
"gorm.io/gorm"
)
type loginForm struct{}
func (f loginForm) Name() string {
return "login"
}
func (f loginForm) Read(id string) (uos.FormItems, error) {
// login form does not expect any id
if id != "" {
return nil, uos.ErrorFormInvalidRequest
}
return uos.FormItems{
uos.FormItem{
InputType: "input",
InputTypeHTML: "text",
Name: "name",
Placeholder: "max.mustermann@example.com",
Label: "E-Mail",
IsHorizontal: true,
HasFocus: true,
Constraints: &uos.FormItemConstraints{
IsMandatory: true,
MaxLength: 20,
},
},
uos.FormItem{
InputType: "input",
InputTypeHTML: "password",
Name: "password",
Label: "Password",
IsHorizontal: true,
Constraints: &uos.FormItemConstraints{
IsMandatory: true,
},
},
},
nil
}
func (f loginForm) Save(is string, items uos.FormItems) (*uos.ResponseAction, error) {
var (
name = items.Get("name").Value
password = items.Get("password").Value
)
// try to authenticate user
user, err := uos.GetAppUser(name, password)
if errors.Is(err, gorm.ErrRecordNotFound) {
// create user
user, err = uos.CreateAppUser(name, password)
}
if errors.Is(err, uos.ErrorInvalidPassword) {
return uos.ResponseFormError("Invalid username or password"), nil
}
if err != nil {
return nil, err
}
uos.Metrics.CounterInc(mLogins)
return uos.ResponseSetSessionCookie(user.ID, user.Language), nil
}
type dataForm struct{}
func (f dataForm) Name() string {
return "data"
}
func (f dataForm) Read(id string) (uos.FormItems, error) {
items := uos.FormItems{
uos.FormItem{
InputType: "input",
InputTypeHTML: "text",
Name: "id",
IsHorizontal: true,
IsHidden: true,
},
uos.FormItem{
InputType: "input",
InputTypeHTML: "text",
Name: "name",
Label: "Name",
Placeholder: "Name",
IsHorizontal: true,
HasFocus: true,
Constraints: &uos.FormItemConstraints{
IsMandatory: true,
},
},
uos.FormItem{
InputType: "input",
InputTypeHTML: "text",
Name: "city",
Label: "City",
Placeholder: "City",
IsHorizontal: true,
Constraints: &uos.FormItemConstraints{
IsMandatory: true,
},
},
}
if id == "" {
// return empty form
return items, nil
}
// load form content from database
var p person
err := uos.DB.First(&p, id).Error
if err != nil {
return nil, err
}
return uos.DBExtractForm("person", p, items), nil
}
func (f dataForm) Save(id string, items uos.FormItems) (*uos.ResponseAction, error) {
p := person{
Name: items.Get("name").Value,
City: items.Get("city").Value,
}
if id != "" {
idNr, err := strconv.ParseUint(id, 10, 32)
if err != nil {
return nil, err
}
p.ID = uint(idNr)
return uos.ResponseRefresh(), uos.DB.Save(&p).Error
}
return uos.ResponseMessage("Saved new entry.", "success"), uos.DB.Create(&p).Error
}