github.com/bketelsen/buffalo@v0.9.5/generators/resource/templates/actions/resource-json-xml.go.tmpl (about)

     1  package actions
     2  
     3  import (
     4    "github.com/pkg/errors"
     5  
     6    "github.com/gobuffalo/buffalo"
     7    "github.com/markbates/pop"
     8    "{{.modelsPath}}"
     9  )
    10  
    11  // This file is generated by Buffalo. It offers a basic structure for
    12  // adding, editing and deleting a page. If your model is more
    13  // complex or you need more than the basic implementation you need to
    14  // edit this file.
    15  
    16  // Following naming logic is implemented in Buffalo:
    17  // Model: Singular ({{.model}})
    18  // DB Table: Plural ({{.modelPlural}})
    19  // Resource: Plural ({{.modelPlural}})
    20  // Path: Plural (/{{.under}})
    21  
    22  // {{.modelPlural}}Resource is the resource for the {{.singular}} model
    23  type {{.camel}}Resource struct{
    24    buffalo.Resource
    25  }
    26  
    27  // List gets all {{.modelPlural}}. This function is mapped to the path
    28  // GET /{{.under}}
    29  func (v {{.camel}}Resource) List(c buffalo.Context) error {
    30    // Get the DB connection from the context
    31    tx := c.Value("tx").(*pop.Connection)
    32  
    33    {{.varPlural}} := &models.{{.modelPlural}}{}
    34  
    35    // Paginate results. Params "page" and "per_page" control pagination.
    36    // Default values are "page=1" and "per_page=20".
    37    q := tx.PaginateFromParams(c.Params())
    38  
    39    // Retrieve all {{.modelPlural}} from the DB
    40    if err := q.All({{.varPlural}}); err != nil {
    41      return c.Error(404, err)
    42    }
    43  
    44    // Add the paginator to the headers so clients know how to paginate.
    45    c.Response().Header().Set("X-Pagination", q.Paginator.String())
    46  
    47    return c.Render(200, r.{{.renderFunction}}({{.varPlural}}))
    48  }
    49  
    50  // Show gets the data for one {{.model}}. This function is mapped to
    51  // the path GET /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id}
    52  func (v {{.camel}}Resource) Show(c buffalo.Context) error {
    53    // Get the DB connection from the context
    54    tx := c.Value("tx").(*pop.Connection)
    55  
    56    // Allocate an empty {{.model}}
    57    {{.varSingular}} := &models.{{.model}}{}
    58  
    59    // To find the {{.model}} the parameter {{.modelSingularUnder}}_id is used.
    60    if err := tx.Find({{.varSingular}}, c.Param("{{.modelSingularUnder}}_id")); err != nil {
    61        return c.Error(404, err)
    62    }
    63    return c.Render(200, r.{{.renderFunction}}({{.varSingular}}))
    64  }
    65  
    66  // New default implementation. Returns a 404
    67  func (v {{.camel}}Resource) New(c buffalo.Context) error {
    68    return c.Error(404, errors.New("not available"))
    69  }
    70  
    71  // Create adds a {{.singular}} to the DB. This function is mapped to the
    72  // path POST /{{.resourceURL}}
    73  func (v {{.camel}}Resource) Create(c buffalo.Context) error {
    74    // Allocate an empty {{.model}}
    75    {{.varSingular}} := &models.{{.model}}{}
    76  
    77    // Bind {{.varSingular}} to the html form elements
    78    if err := c.Bind({{.varSingular}}); err != nil {
    79      return errors.WithStack(err)
    80    }
    81  
    82    // Get the DB connection from the context
    83    tx := c.Value("tx").(*pop.Connection)
    84  
    85    // Validate the data from the html form
    86    verrs, err := tx.ValidateAndCreate({{.varSingular}})
    87    if err != nil {
    88      return errors.WithStack(err)
    89    }
    90  
    91    if verrs.HasAny() {
    92      // Render errors as {{.renderFunction}}
    93      return c.Render(400, r.{{.renderFunction}}(verrs))
    94    }
    95  
    96    // Success!
    97    return c.Render(201, r.{{.renderFunction}}({{.varSingular}}))
    98  }
    99  
   100  // Edit default implementation. Returns a 404
   101  func (v {{.camel}}Resource) Edit(c buffalo.Context) error {
   102    return c.Error(404, errors.New("not available"))
   103  }
   104  
   105  // Update changes a {{.singular}} in the DB. This function is mapped to
   106  // the path PUT /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id}
   107  func (v {{.camel}}Resource) Update(c buffalo.Context) error {
   108    // Get the DB connection from the context
   109    tx := c.Value("tx").(*pop.Connection)
   110  
   111    // Allocate an empty {{.model}}
   112    {{.varSingular}} := &models.{{.model}}{}
   113  
   114    if err := tx.Find({{.varSingular}}, c.Param("{{.modelSingularUnder}}_id")); err != nil {
   115      return c.Error(404, err)
   116    }
   117  
   118    // Bind {{.singular}} to the html form elements
   119    if err := c.Bind({{.varSingular}}); err != nil {
   120      return errors.WithStack(err)
   121    }
   122  
   123    verrs, err := tx.ValidateAndUpdate({{.varSingular}})
   124    if err != nil {
   125      return errors.WithStack(err)
   126    }
   127  
   128    if verrs.HasAny() {
   129      // Render errors as {{.renderFunction}}
   130      return c.Render(400, r.{{.renderFunction}}(verrs))
   131    }
   132  
   133    // Success!
   134    return c.Render(200, r.{{.renderFunction}}({{.varSingular}}))
   135  }
   136  
   137  // Destroy deletes a {{.singular}} from the DB. This function is mapped
   138  // to the path DELETE /{{.under}}/{{"{"}}{{.modelSingularUnder}}_id}
   139  func (v {{.camel}}Resource) Destroy(c buffalo.Context) error {
   140    // Get the DB connection from the context
   141    tx := c.Value("tx").(*pop.Connection)
   142  
   143    // Allocate an empty {{.model}}
   144    {{.varSingular}} := &models.{{.model}}{}
   145  
   146    // To find the {{.model}} the parameter {{.modelSingularUnder}}_id is used.
   147    if err := tx.Find({{.varSingular}}, c.Param("{{.modelSingularUnder}}_id")); err != nil {
   148      return c.Error(404, err)
   149    }
   150  
   151    if err := tx.Destroy({{.varSingular}}); err != nil {
   152      return errors.WithStack(err)
   153    }
   154  
   155    // Success!
   156    return c.Render(200, r.{{.renderFunction}}({{.varSingular}}))
   157  }