github.com/cheikhshift/buffalo@v0.9.5/generators/resource/templates/actions/resource-use_model.go.tmpl (about)

     1  package actions
     2  
     3  import (
     4  
     5    "github.com/pkg/errors"
     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 ({{.modelTable}})
    19  // Resource: Plural ({{.resourceName}})
    20  // Path: Plural (/{{.resourceURL}})
    21  // View Template Folder: Plural (/templates/{{.filesPath}}/)
    22  
    23  // {{.resourceName}}Resource is the resource for the {{.singular}} model
    24  type {{.resourceName}}Resource struct{
    25    buffalo.Resource
    26  }
    27  
    28  // List gets all {{.modelPlural}}. This function is mapped to the path
    29  // GET /{{.under}}
    30  func (v {{.resourceName}}Resource) List(c buffalo.Context) error {
    31    // Get the DB connection from the context
    32    tx := c.Value("tx").(*pop.Connection)
    33  
    34    {{.varPlural}} := &models.{{.modelPlural}}{}
    35  
    36    // Paginate results. Params "page" and "per_page" control pagination.
    37    // Default values are "page=1" and "per_page=20".
    38    q := tx.PaginateFromParams(c.Params())
    39  
    40    // Retrieve all {{.modelPlural}} from the DB
    41    if err := q.All({{.varPlural}}); err != nil {
    42      return errors.WithStack(err)
    43    }
    44  
    45    // Make {{.modelPlural}} available inside the html template
    46    c.Set("{{.varPlural}}", {{.varPlural}})
    47  
    48    // Add the paginator to the context so it can be used in the template.
    49    c.Set("pagination", q.Paginator)
    50  
    51    return c.Render(200, r.HTML("{{.filesPath}}/index.html"))
    52  }
    53  
    54  // Show gets the data for one {{.model}}. This function is mapped to
    55  // the path GET /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id}
    56  func (v {{.resourceName}}Resource) Show(c buffalo.Context) error {
    57    // Get the DB connection from the context
    58    tx := c.Value("tx").(*pop.Connection)
    59  
    60    // Allocate an empty {{.model}}
    61    {{.varSingular}} := &models.{{.model}}{}
    62  
    63    // To find the {{.model}} the parameter {{.resourceSingularUnder}}_id is used.
    64    if err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")); err != nil {
    65      return c.Error(404, err)
    66    }
    67  
    68    // Make {{.varSingular}} available inside the html template
    69    c.Set("{{.varSingular}}", {{.varSingular}})
    70  
    71    return c.Render(200, r.HTML("{{.filesPath}}/show.html"))
    72  }
    73  
    74  // New renders the form for creating a new {{.model}}.
    75  // This function is mapped to the path GET /{{.resourceURL}}/new
    76  func (v {{.resourceName}}Resource) New(c buffalo.Context) error {
    77    // Make {{.varSingular}} available inside the html template
    78    c.Set("{{.varSingular}}", &models.{{.model}}{})
    79  
    80    return c.Render(200, r.HTML("{{.filesPath}}/new.html"))
    81  }
    82  
    83  // Create adds a {{.model}} to the DB. This function is mapped to the
    84  // path POST /{{.resourceURL}}
    85  func (v {{.resourceName}}Resource) Create(c buffalo.Context) error {
    86    // Allocate an empty {{.model}}
    87    {{.varSingular}} := &models.{{.model}}{}
    88  
    89    // Bind {{.varSingular}} to the html form elements
    90    if err := c.Bind({{.varSingular}}); err != nil {
    91      return errors.WithStack(err)
    92    }
    93  
    94    // Get the DB connection from the context
    95    tx := c.Value("tx").(*pop.Connection)
    96  
    97    // Validate the data from the html form
    98    verrs, err := tx.ValidateAndCreate({{.varSingular}})
    99    if err != nil {
   100      return errors.WithStack(err)
   101    }
   102  
   103    if verrs.HasAny() {
   104      // Make {{.varSingular}} available inside the html template
   105      c.Set("{{.varSingular}}", {{.varSingular}})
   106  
   107      // Make the errors available inside the html template
   108      c.Set("errors", verrs)
   109  
   110      // Render again the new.html template that the user can
   111      // correct the input.
   112      return c.Render(422, r.HTML("{{.filesPath}}/new.html"))
   113    }
   114  
   115    // If there are no errors set a success message
   116    c.Flash().Add("success", "{{.model}} was created successfully")
   117  
   118    // and redirect to the {{.under}} index page
   119    return c.Redirect(302, "/{{.resourceURL}}/%s",{{.varSingular}}.ID)
   120  }
   121  
   122  // Edit renders a edit form for a {{.singular}}. This function is
   123  // mapped to the path GET /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id}/edit
   124  func (v {{.resourceName}}Resource) Edit(c buffalo.Context) error {
   125    // Get the DB connection from the context
   126    tx := c.Value("tx").(*pop.Connection)
   127  
   128    // Allocate an empty {{.model}}
   129    {{.varSingular}} := &models.{{.model}}{}
   130  
   131    if err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")); err != nil {
   132      return c.Error(404, err)
   133    }
   134  
   135    // Make {{.varSingular}} available inside the html template
   136    c.Set("{{.varSingular}}", {{.varSingular}})
   137    return c.Render(200, r.HTML("{{.filesPath}}/edit.html"))
   138  }
   139  
   140  // Update changes a {{.singular}} in the DB. This function is mapped to
   141  // the path PUT /{{.under}}/{{"{"}}{{.modelSingularUnder}}_id}
   142  func (v {{.resourceName}}Resource) Update(c buffalo.Context) error {
   143    // Get the DB connection from the context
   144    tx := c.Value("tx").(*pop.Connection)
   145  
   146    // Allocate an empty {{.model}}
   147    {{.varSingular}} := &models.{{.model}}{}
   148  
   149    if err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")); err != nil {
   150      return c.Error(404, err)
   151    }
   152  
   153    // Bind {{.model}} to the html form elements
   154    if err := c.Bind({{.varSingular}}); err != nil {
   155      return errors.WithStack(err)
   156    }
   157  
   158    verrs, err := tx.ValidateAndUpdate({{.varSingular}})
   159    if err != nil {
   160      return errors.WithStack(err)
   161    }
   162  
   163    if verrs.HasAny() {
   164      // Make {{.varSingular}} available inside the html template
   165      c.Set("{{.varSingular}}", {{.varSingular}})
   166  
   167      // Make the errors available inside the html template
   168      c.Set("errors", verrs)
   169  
   170      // Render again the edit.html template that the user can
   171      // correct the input.
   172      return c.Render(422, r.HTML("{{.filesPath}}/edit.html"))
   173    }
   174  
   175    // If there are no errors set a success message
   176    c.Flash().Add("success", "{{.model}} was updated successfully")
   177  
   178    // and redirect to the {{.under}} index page
   179    return c.Redirect(302, "/{{.resourceURL}}/%s",{{.varSingular}}.ID)
   180  }
   181  
   182  // Destroy deletes a {{.singular}} from the DB. This function is mapped
   183  // to the path DELETE /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id}
   184  func (v {{.resourceName}}Resource) Destroy(c buffalo.Context) error {
   185    // Get the DB connection from the context
   186    tx := c.Value("tx").(*pop.Connection)
   187  
   188    // Allocate an empty {{.model}}
   189    {{.varSingular}} := &models.{{.model}}{}
   190  
   191    // To find the {{.model}} the parameter {{.modelSingularUnder}}_id is used.
   192    if err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")); err != nil {
   193      return c.Error(404, err)
   194    }
   195  
   196    if err := tx.Destroy({{.varSingular}}); err != nil {
   197      return errors.WithStack(err)
   198    }
   199  
   200    // If there are no errors set a flash message
   201    c.Flash().Add("success", "{{.model}} was destroyed successfully")
   202  
   203    // Redirect to the {{.under}} index page
   204    return c.Redirect(302, "/{{.resourceURL}}")
   205  }