github.com/rotblauer/buffalo@v0.7.1-0.20170112214545-7aa55ef80dd3/examples/json-crud/actions/users.go (about)

     1  package actions
     2  
     3  import (
     4  	"github.com/gobuffalo/buffalo"
     5  	"github.com/gobuffalo/buffalo/examples/json-crud/models"
     6  	"github.com/gobuffalo/buffalo/render"
     7  	"github.com/markbates/pop"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  func findUserMW(h buffalo.Handler) buffalo.Handler {
    12  	return func(c buffalo.Context) error {
    13  		id, err := c.ParamInt("user_id")
    14  		if err == nil {
    15  			u := &models.User{}
    16  			tx := c.Get("tx").(*pop.Connection)
    17  			err = tx.Find(u, id)
    18  			if err != nil {
    19  				return c.Error(404, errors.WithStack(err))
    20  			}
    21  			c.Set("user", u)
    22  		}
    23  		return h(c)
    24  	}
    25  }
    26  
    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  	return c.Render(200, render.JSON(users))
    36  }
    37  
    38  func UsersShow(c buffalo.Context) error {
    39  	return c.Render(200, render.JSON(c.Get("user")))
    40  }
    41  
    42  func UsersCreate(c buffalo.Context) error {
    43  	u := &models.User{}
    44  	err := c.Bind(u)
    45  	if err != nil {
    46  		return errors.WithStack(err)
    47  	}
    48  
    49  	tx := c.Get("tx").(*pop.Connection)
    50  	verrs, err := u.ValidateNew(tx)
    51  	if err != nil {
    52  		return errors.WithStack(err)
    53  	}
    54  	if verrs.HasAny() {
    55  		c.Set("verrs", verrs.Errors)
    56  		return c.Render(422, render.JSON(verrs))
    57  	}
    58  	err = tx.Create(u)
    59  	if err != nil {
    60  		return errors.WithStack(err)
    61  	}
    62  
    63  	return c.Render(201, render.JSON(u))
    64  }
    65  
    66  func UsersUpdate(c buffalo.Context) error {
    67  	tx := c.Get("tx").(*pop.Connection)
    68  	u := c.Get("user").(*models.User)
    69  
    70  	err := c.Bind(u)
    71  	if err != nil {
    72  		return errors.WithStack(err)
    73  	}
    74  
    75  	verrs, err := u.ValidateUpdate(tx)
    76  	if err != nil {
    77  		return errors.WithStack(err)
    78  	}
    79  	if verrs.HasAny() {
    80  		c.Set("verrs", verrs.Errors)
    81  		return c.Render(422, render.JSON(verrs))
    82  	}
    83  	err = tx.Update(u)
    84  	if err != nil {
    85  		return errors.WithStack(err)
    86  	}
    87  
    88  	err = tx.Reload(u)
    89  	if err != nil {
    90  		return errors.WithStack(err)
    91  	}
    92  	return c.Render(200, render.JSON(u))
    93  }
    94  
    95  func UsersDelete(c buffalo.Context) error {
    96  	tx := c.Get("tx").(*pop.Connection)
    97  	u := c.Get("user").(*models.User)
    98  
    99  	err := tx.Destroy(u)
   100  	if err != nil {
   101  		return errors.WithStack(err)
   102  	}
   103  
   104  	return c.Render(200, render.JSON(u))
   105  }