/
users.go
197 lines (185 loc) · 5.89 KB
/
users.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package actions
import (
"github.com/bscott/golangflow/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v5"
"github.com/pkg/errors"
)
// This file is generated by Buffalo. It offers a basic structure for
// adding, editing and deleting a page. If your model is more
// complex or you need more than the basic implementation you need to
// edit this file.
// Following naming logic is implemented in Buffalo:
// Model: Singular (User)
// DB Table: Plural (Users)
// Resource: Plural (Users)
// Path: Plural (/users)
// View Template Folder: Plural (/templates/users/)
// UsersResource is the resource for the user model
type UsersResource struct {
buffalo.Resource
}
// List gets all Users. This function is mapped to the path
// GET /users
func (v UsersResource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
// //
// userid := c.Session().Get("current_user_id")
// u := &models.User{}
// erru := tx.Find(&u, userid)
// if erru != nil {
// return errors.WithStack(erru)
// }
// if "Brian Scott" != u.Name {
// c.Flash().Add("danger", "Unauthorized User")
// return c.Redirect(302, "/")
// }
users := &models.Users{}
// You can order your list here. Just change
err := tx.All(users)
// to:
// err := tx.Order("create_at desc").All(users)
if err != nil {
return errors.WithStack(err)
}
// Make users available inside the html template
c.Set("users", users)
return c.Render(200, r.HTML("users/index.html"))
}
// Show gets the data for one User. This function is mapped to
// the path GET /users/{user_id}
func (v UsersResource) Show(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
// Allocate an empty User
user := &models.User{}
// To find the User the parameter user_id is used.
err := tx.Find(user, c.Param("user_id"))
if err != nil {
return errors.WithStack(err)
}
if "Brian Scott" != user.Name {
c.Flash().Add("danger", "Unauthorized User")
return c.Redirect(302, "/")
}
// Make user available inside the html template
c.Set("user", user)
return c.Render(200, r.HTML("users/show.html"))
}
// New renders the formular for creating a new user.
// This function is mapped to the path GET /users/new
func (v UsersResource) New(c buffalo.Context) error {
// Make user available inside the html template
c.Set("user", &models.User{})
return c.Render(200, r.HTML("users/new.html"))
}
// Create adds a user to the DB. This function is mapped to the
// path POST /users
func (v UsersResource) Create(c buffalo.Context) error {
// Allocate an empty User
user := &models.User{}
// Bind user to the html form elements
err := c.Bind(user)
if err != nil {
return errors.WithStack(err)
}
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
// Validate the data from the html form
verrs, err := tx.ValidateAndCreate(user)
if err != nil {
return errors.WithStack(err)
}
if verrs.HasAny() {
// Make user available inside the html template
c.Set("user", user)
// Make the errors available inside the html template
c.Set("errors", verrs)
// Render again the new.html template that the user can
// correct the input.
return c.Render(422, r.HTML("users/new.html"))
}
// If there are no errors set a success message
c.Flash().Add("success", "User was created successfully")
// and redirect to the users index page
return c.Redirect(302, "/users/%s", user.ID)
}
// Edit renders a edit formular for a user. This function is
// mapped to the path GET /users/{user_id}/edit
func (v UsersResource) Edit(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
// Allocate an empty User
user := &models.User{}
err := tx.Find(user, c.Param("user_id"))
if err != nil {
return errors.WithStack(err)
}
// Make user available inside the html template
c.Set("user", user)
return c.Render(200, r.HTML("users/edit.html"))
}
// Update changes a user in the DB. This function is mapped to
// the path PUT /users/{user_id}
func (v UsersResource) Update(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
// Allocate an empty User
user := &models.User{}
err := tx.Find(user, c.Param("user_id"))
if err != nil {
return errors.WithStack(err)
}
if "Brian Scott" != user.Name {
c.Flash().Add("danger", "Unauthorized User")
return c.Redirect(302, "/")
}
// Bind user to the html form elements
err = c.Bind(user)
if err != nil {
return errors.WithStack(err)
}
verrs, err := tx.ValidateAndUpdate(user)
if err != nil {
return errors.WithStack(err)
}
if verrs.HasAny() {
// Make user available inside the html template
c.Set("user", user)
// Make the errors available inside the html template
c.Set("errors", verrs)
// Render again the edit.html template that the user can
// correct the input.
return c.Render(422, r.HTML("users/edit.html"))
}
// If there are no errors set a success message
c.Flash().Add("success", "User was updated successfully")
// and redirect to the users index page
return c.Redirect(302, "/users/%s", user.ID)
}
// Destroy deletes a user from the DB. This function is mapped
// to the path DELETE /users/{user_id}
func (v UsersResource) Destroy(c buffalo.Context) error {
// Get the DB connection from the context
tx := c.Value("tx").(*pop.Connection)
// Allocate an empty User
user := &models.User{}
// To find the User the parameter user_id is used.
err := tx.Find(user, c.Param("user_id"))
if err != nil {
return errors.WithStack(err)
}
if "Brian Scott" != user.Name {
c.Flash().Add("danger", "Unauthorized User")
return c.Redirect(302, "/")
}
err = tx.Destroy(user)
if err != nil {
return errors.WithStack(err)
}
// If there are no errors set a flash message
c.Flash().Add("success", "User was destroyed successfully")
// Redirect to the users index page
return c.Redirect(302, "/users")
}