github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/examples/html-crud/actions/users.go (about) 1 package actions 2 3 import ( 4 "github.com/gobuffalo/buffalo" 5 "github.com/gobuffalo/buffalo/examples/html-crud/models" 6 "github.com/markbates/pop" 7 "github.com/pkg/errors" 8 ) 9 10 func findUserMW(h buffalo.Handler) buffalo.Handler { 11 return func(c buffalo.Context) error { 12 id, err := c.ParamInt("user_id") 13 if err == nil { 14 u := &models.User{} 15 tx := c.Get("tx").(*pop.Connection) 16 err = tx.Find(u, id) 17 if err != nil { 18 return c.Error(404, errors.WithStack(err)) 19 } 20 c.Set("user", u) 21 } 22 return h(c) 23 } 24 } 25 26 // UsersList renders an html page that shows users 27 func UsersList(c buffalo.Context) error { 28 users := &models.Users{} 29 tx := c.Get("tx").(*pop.Connection) 30 err := tx.All(users) 31 if err != nil { 32 return c.Error(404, errors.WithStack(err)) 33 } 34 35 c.Set("users", users) 36 return c.Render(200, r.HTML("users/index.html")) 37 } 38 39 // UsersShow renders an html form for viewing a user 40 func UsersShow(c buffalo.Context) error { 41 return c.Render(200, r.HTML("users/show.html")) 42 } 43 44 // UsersNew renders a form for adding a new user 45 func UsersNew(c buffalo.Context) error { 46 c.Set("user", models.User{}) 47 return c.Render(200, r.HTML("users/new.html")) 48 } 49 50 // UsersCreate creates a user 51 func UsersCreate(c buffalo.Context) error { 52 u := &models.User{} 53 err := c.Bind(u) 54 if err != nil { 55 return errors.WithStack(err) 56 } 57 58 tx := c.Get("tx").(*pop.Connection) 59 verrs, err := u.ValidateNew(tx) 60 if err != nil { 61 return errors.WithStack(err) 62 } 63 if verrs.HasAny() { 64 c.Set("verrs", verrs.Errors) 65 c.Set("user", u) 66 return c.Render(422, r.HTML("users/new.html")) 67 } 68 err = tx.Create(u) 69 if err != nil { 70 return errors.WithStack(err) 71 } 72 73 return c.Redirect(301, "/users/%d", u.ID) 74 } 75 76 // UsersEdit renders the editing page for a target user 77 func UsersEdit(c buffalo.Context) error { 78 return c.Render(200, r.HTML("users/edit.html")) 79 } 80 81 // UsersUpdate updates a target user 82 func UsersUpdate(c buffalo.Context) error { 83 tx := c.Get("tx").(*pop.Connection) 84 u := c.Get("user").(*models.User) 85 86 err := c.Bind(u) 87 if err != nil { 88 return errors.WithStack(err) 89 } 90 91 verrs, err := u.ValidateUpdate(tx) 92 if err != nil { 93 return errors.WithStack(err) 94 } 95 if verrs.HasAny() { 96 c.Set("verrs", verrs.Errors) 97 c.Set("user", u) 98 return c.Render(422, r.HTML("users/edit.html")) 99 } 100 err = tx.Update(u) 101 if err != nil { 102 return errors.WithStack(err) 103 } 104 105 err = tx.Reload(u) 106 if err != nil { 107 return errors.WithStack(err) 108 } 109 return c.Redirect(301, "/users/%d", u.ID) 110 } 111 112 // UsersDelete removes a target user 113 func UsersDelete(c buffalo.Context) error { 114 tx := c.Get("tx").(*pop.Connection) 115 u := c.Get("user").(*models.User) 116 117 err := tx.Destroy(u) 118 if err != nil { 119 return errors.WithStack(err) 120 } 121 122 return c.Redirect(301, "/users") 123 }