github.com/mayra-cabrera/buffalo@v0.9.4-0.20170814145312-66d2e7772f11/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 {{.varPlural}} := &models.{{.modelPlural}}{} 34 // Paginate results. Params "page" and "per_page" control pagination. 35 // Default values are "page=1" and "per_page=20". 36 q := tx.PaginateFromParams(c.Params()) 37 // You can order your list here. Just change 38 err := q.All({{.varPlural}}) 39 // to: 40 // err := q.Order("created_at desc").All({{.varPlural}}) 41 if err != nil { 42 return errors.WithStack(err) 43 } 44 // Make {{.modelPlural}} available inside the html template 45 c.Set("{{.varPlural}}", {{.varPlural}}) 46 // Add the paginator to the context so it can be used in the template. 47 c.Set("pagination", q.Paginator) 48 return c.Render(200, r.HTML("{{.filesPath}}/index.html")) 49 } 50 51 // Show gets the data for one {{.model}}. This function is mapped to 52 // the path GET /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id} 53 func (v {{.resourceName}}Resource) Show(c buffalo.Context) error { 54 // Get the DB connection from the context 55 tx := c.Value("tx").(*pop.Connection) 56 // Allocate an empty {{.model}} 57 {{.varSingular}} := &models.{{.model}}{} 58 // To find the {{.model}} the parameter {{.resourceSingularUnder}}_id is used. 59 err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")) 60 if err != nil { 61 return errors.WithStack(err) 62 } 63 // Make {{.varSingular}} available inside the html template 64 c.Set("{{.varSingular}}", {{.varSingular}}) 65 return c.Render(200, r.HTML("{{.filesPath}}/show.html")) 66 } 67 68 // New renders the formular for creating a new {{.model}}. 69 // This function is mapped to the path GET /{{.resourceURL}}/new 70 func (v {{.resourceName}}Resource) New(c buffalo.Context) error { 71 // Make {{.varSingular}} available inside the html template 72 c.Set("{{.varSingular}}", &models.{{.model}}{}) 73 return c.Render(200, r.HTML("{{.filesPath}}/new.html")) 74 } 75 76 // Create adds a {{.model}} to the DB. This function is mapped to the 77 // path POST /{{.resourceURL}} 78 func (v {{.resourceName}}Resource) Create(c buffalo.Context) error { 79 // Allocate an empty {{.model}} 80 {{.varSingular}} := &models.{{.model}}{} 81 // Bind {{.varSingular}} to the html form elements 82 err := c.Bind({{.varSingular}}) 83 if err != nil { 84 return errors.WithStack(err) 85 } 86 // Get the DB connection from the context 87 tx := c.Value("tx").(*pop.Connection) 88 // Validate the data from the html form 89 verrs, err := tx.ValidateAndCreate({{.varSingular}}) 90 if err != nil { 91 return errors.WithStack(err) 92 } 93 if verrs.HasAny() { 94 // Make {{.varSingular}} available inside the html template 95 c.Set("{{.varSingular}}", {{.varSingular}}) 96 // Make the errors available inside the html template 97 c.Set("errors", verrs) 98 // Render again the new.html template that the user can 99 // correct the input. 100 return c.Render(422, r.HTML("{{.filesPath}}/new.html")) 101 } 102 // If there are no errors set a success message 103 c.Flash().Add("success", "{{.model}} was created successfully") 104 // and redirect to the {{.under}} index page 105 return c.Redirect(302, "/{{.resourceURL}}/%s",{{.varSingular}}.ID) 106 } 107 108 // Edit renders a edit formular for a {{.singular}}. This function is 109 // mapped to the path GET /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id}/edit 110 func (v {{.resourceName}}Resource) Edit(c buffalo.Context) error { 111 // Get the DB connection from the context 112 tx := c.Value("tx").(*pop.Connection) 113 // Allocate an empty {{.model}} 114 {{.varSingular}} := &models.{{.model}}{} 115 err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")) 116 if err != nil { 117 return errors.WithStack(err) 118 } 119 // Make {{.varSingular}} available inside the html template 120 c.Set("{{.varSingular}}", {{.varSingular}}) 121 return c.Render(200, r.HTML("{{.filesPath}}/edit.html")) 122 } 123 124 // Update changes a {{.singular}} in the DB. This function is mapped to 125 // the path PUT /{{.under}}/{{"{"}}{{.modelSingularUnder}}_id} 126 func (v {{.resourceName}}Resource) Update(c buffalo.Context) error { 127 // Get the DB connection from the context 128 tx := c.Value("tx").(*pop.Connection) 129 // Allocate an empty {{.model}} 130 {{.varSingular}} := &models.{{.model}}{} 131 err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")) 132 if err != nil { 133 return errors.WithStack(err) 134 } 135 // Bind {{.model}} to the html form elements 136 err = c.Bind({{.varSingular}}) 137 if err != nil { 138 return errors.WithStack(err) 139 } 140 verrs, err := tx.ValidateAndUpdate({{.varSingular}}) 141 if err != nil { 142 return errors.WithStack(err) 143 } 144 if verrs.HasAny() { 145 // Make {{.varSingular}} available inside the html template 146 c.Set("{{.varSingular}}", {{.varSingular}}) 147 // Make the errors available inside the html template 148 c.Set("errors", verrs) 149 // Render again the edit.html template that the user can 150 // correct the input. 151 return c.Render(422, r.HTML("{{.filesPath}}/edit.html")) 152 } 153 // If there are no errors set a success message 154 c.Flash().Add("success", "{{.model}} was updated successfully") 155 // and redirect to the {{.under}} index page 156 return c.Redirect(302, "/{{.resourceURL}}/%s",{{.varSingular}}.ID) 157 } 158 159 // Destroy deletes a {{.singular}} from the DB. This function is mapped 160 // to the path DELETE /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id} 161 func (v {{.resourceName}}Resource) Destroy(c buffalo.Context) error { 162 // Get the DB connection from the context 163 tx := c.Value("tx").(*pop.Connection) 164 // Allocate an empty {{.model}} 165 {{.varSingular}} := &models.{{.model}}{} 166 // To find the {{.model}} the parameter {{.modelSingularUnder}}_id is used. 167 err := tx.Find({{.varSingular}}, c.Param("{{.resourceSingularUnder}}_id")) 168 if err != nil { 169 return errors.WithStack(err) 170 } 171 err = tx.Destroy({{.varSingular}}) 172 if err != nil { 173 return errors.WithStack(err) 174 } 175 // If there are no errors set a flash message 176 c.Flash().Add("success", "{{.model}} was destroyed successfully") 177 // Redirect to the {{.under}} index page 178 return c.Redirect(302, "/{{.resourceURL}}") 179 }