github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/examples/html-resource/actions/users.go (about)

     1  package actions
     2  
     3  import (
     4  	"github.com/gobuffalo/buffalo"
     5  	"github.com/gobuffalo/buffalo/examples/html-resource/models"
     6  	"github.com/markbates/pop"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // UsersResource allows CRUD with HTTP against the User model
    11  type UsersResource struct {
    12  	buffalo.BaseResource
    13  }
    14  
    15  func findUserMW(n string) buffalo.MiddlewareFunc {
    16  	return func(h buffalo.Handler) buffalo.Handler {
    17  		return func(c buffalo.Context) error {
    18  			id, err := c.ParamInt(n)
    19  			if err == nil {
    20  				u := &models.User{}
    21  				tx := c.Get("tx").(*pop.Connection)
    22  				err = tx.Find(u, id)
    23  				if err != nil {
    24  					return c.Error(404, errors.WithStack(err))
    25  				}
    26  				c.Set("user", u)
    27  			}
    28  			return h(c)
    29  		}
    30  	}
    31  }
    32  
    33  // List shows all users in an HTML page
    34  func (ur *UsersResource) List(c buffalo.Context) error {
    35  	users := &models.Users{}
    36  	tx := c.Get("tx").(*pop.Connection)
    37  	err := tx.All(users)
    38  	if err != nil {
    39  		return c.Error(404, errors.WithStack(err))
    40  	}
    41  
    42  	c.Set("users", users)
    43  	return c.Render(200, r.HTML("users/index.html"))
    44  }
    45  
    46  // Show renders a target user in an HTML page
    47  func (ur *UsersResource) Show(c buffalo.Context) error {
    48  	return c.Render(200, r.HTML("users/show.html"))
    49  }
    50  
    51  // New renders a form for adding a new user
    52  func (ur *UsersResource) New(c buffalo.Context) error {
    53  	c.Set("user", models.User{})
    54  	return c.Render(200, r.HTML("users/new.html"))
    55  }
    56  
    57  // Create is a JSON API endpoint that adds a new user
    58  func (ur *UsersResource) Create(c buffalo.Context) error {
    59  	u := &models.User{}
    60  	err := c.Bind(u)
    61  	if err != nil {
    62  		return errors.WithStack(err)
    63  	}
    64  
    65  	tx := c.Get("tx").(*pop.Connection)
    66  	verrs, err := u.ValidateNew(tx)
    67  	if err != nil {
    68  		return errors.WithStack(err)
    69  	}
    70  	if verrs.HasAny() {
    71  		c.Set("verrs", verrs.Errors)
    72  		c.Set("user", u)
    73  		return c.Render(422, r.HTML("users/new.html"))
    74  	}
    75  	err = tx.Create(u)
    76  	if err != nil {
    77  		return errors.WithStack(err)
    78  	}
    79  
    80  	return c.Redirect(301, "/users/%d", u.ID)
    81  }
    82  
    83  // Edit renders an html form for editing a user
    84  func (ur *UsersResource) Edit(c buffalo.Context) error {
    85  	return c.Render(200, r.HTML("users/edit.html"))
    86  }
    87  
    88  // Update is a JSON API endpoint that updates a user
    89  func (ur *UsersResource) Update(c buffalo.Context) error {
    90  	tx := c.Get("tx").(*pop.Connection)
    91  	u := c.Get("user").(*models.User)
    92  
    93  	err := c.Bind(u)
    94  	if err != nil {
    95  		return errors.WithStack(err)
    96  	}
    97  
    98  	verrs, err := u.ValidateUpdate(tx)
    99  	if err != nil {
   100  		return errors.WithStack(err)
   101  	}
   102  	if verrs.HasAny() {
   103  		c.Set("verrs", verrs.Errors)
   104  		c.Set("user", u)
   105  		return c.Render(422, r.HTML("users/edit.html"))
   106  	}
   107  	err = tx.Update(u)
   108  	if err != nil {
   109  		return errors.WithStack(err)
   110  	}
   111  
   112  	err = tx.Reload(u)
   113  	if err != nil {
   114  		return errors.WithStack(err)
   115  	}
   116  	return c.Redirect(301, "/users/%d", u.ID)
   117  }
   118  
   119  // Destroy is an API endpoint that deletes a user
   120  func (ur *UsersResource) Destroy(c buffalo.Context) error {
   121  	tx := c.Get("tx").(*pop.Connection)
   122  	u := c.Get("user").(*models.User)
   123  
   124  	err := tx.Destroy(u)
   125  	if err != nil {
   126  		return errors.WithStack(err)
   127  	}
   128  
   129  	return c.Redirect(301, "/users")
   130  }