github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/generators/resource/templates/actions/resource-use_model.go.tmpl (about) 1 package actions 2 3 import ( 4 5 "github.com/gobuffalo/buffalo" 6 "github.com/markbates/pop" 7 "{{.modelsPath}}" 8 ) 9 10 // This file is generated by Buffalo. It offers a basic structure for 11 // adding, editing and deleting a page. If your model is more 12 // complex or you need more than the basic implementation you need to 13 // edit this file. 14 15 // Following naming logic is implemented in Buffalo: 16 // Model: Singular ({{.model}}) 17 // DB Table: Plural ({{.modelPlural}}) 18 // Resource: Plural ({{.modelPlural}}) 19 // Path: Plural (/{{.under}}) 20 // View Template Folder: Plural (/templates/{{.varPlural}}/) 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 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 {{.varPlural}} := &models.{{.modelPlural}}{} 33 // You can order your list here. Just change 34 err := tx.All({{.varPlural}}) 35 // to: 36 // err := tx.Order("(case when completed then 1 else 2 end) desc, lower([sort_parameter]) asc").All({{.downFirstCap}}) 37 // Don't forget to change [sort_parameter] to the parameter of 38 // your model, which should be used for sorting. 39 if err != nil { 40 return err 41 } 42 // Make {{.plural}} available inside the html template 43 c.Set("{{.varPlural}}", {{.varPlural}}) 44 return c.Render(200, r.HTML("{{.modelPluralUnder}}/index.html")) 45 } 46 47 // Show gets the data for one {{.model}}. This function is mapped to 48 // the path GET /{{.under}}/{{"{"}}{{.underSingular}}_id} 49 func (v {{.camel}}Resource) Show(c buffalo.Context) error { 50 // Get the DB connection from the context 51 tx := c.Value("tx").(*pop.Connection) 52 // Allocate an empty {{.model}} 53 {{.varSingular}} := &models.{{.model}}{} 54 // To find the {{.model}} the parameter {{.underSingular}}_id is used. 55 err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) 56 if err != nil { 57 return err 58 } 59 // Make {{.varSingular}} available inside the html template 60 c.Set("{{.varSingular}}", {{.varSingular}}) 61 return c.Render(200, r.HTML("{{.modelPluralUnder}}/show.html")) 62 } 63 64 // New renders the formular for creating a new {{.singular}}. 65 // This function is mapped to the path GET /{{.under}}/new 66 func (v {{.camel}}Resource) New(c buffalo.Context) error { 67 // Make {{.varSingular}} available inside the html template 68 c.Set("{{.varSingular}}", &models.{{.model}}{}) 69 return c.Render(200, r.HTML("{{.modelPluralUnder}}/new.html")) 70 } 71 72 // Create adds a {{.singular}} to the DB. This function is mapped to the 73 // path POST /{{.under}} 74 func (v {{.camel}}Resource) Create(c buffalo.Context) error { 75 // Allocate an empty {{.model}} 76 {{.varSingular}} := &models.{{.model}}{} 77 // Bind {{.varSingular}} to the html form elements 78 err := c.Bind({{.varSingular}}) 79 if err != nil { 80 return err 81 } 82 // Get the DB connection from the context 83 tx := c.Value("tx").(*pop.Connection) 84 // Validate the data from the html form 85 verrs, err := tx.ValidateAndCreate({{.varSingular}}) 86 if err != nil { 87 return err 88 } 89 if verrs.HasAny() { 90 // Make {{.varSingular}} available inside the html template 91 c.Set("{{.varSingular}}", {{.varSingular}}) 92 // Make the errors available inside the html template 93 c.Set("errors", verrs) 94 // Render again the new.html template that the user can 95 // correct the input. 96 return c.Render(422, r.HTML("{{.modelPluralUnder}}/new.html")) 97 } 98 // If there are no errors set a success message 99 c.Flash().Add("success", "{{.model}} was created successfully") 100 // and redirect to the {{.under}} index page 101 return c.Redirect(302, "/{{.under}}/%s",{{.varSingular}}.ID) 102 } 103 104 // Edit renders a edit formular for a {{.singular}}. This function is 105 // mapped to the path GET /{{.under}}/{{"{"}}{{.underSingular}}_id}/edit 106 func (v {{.camel}}Resource) Edit(c buffalo.Context) error { 107 // Get the DB connection from the context 108 tx := c.Value("tx").(*pop.Connection) 109 // Allocate an empty {{.model}} 110 {{.varSingular}} := &models.{{.model}}{} 111 err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) 112 if err != nil { 113 return err 114 } 115 // Make {{.varSingular}} available inside the html template 116 c.Set("{{.varSingular}}", {{.varSingular}}) 117 return c.Render(200, r.HTML("{{.modelPluralUnder}}/edit.html")) 118 } 119 120 // Update changes a {{.singular}} in the DB. This function is mapped to 121 // the path PUT /{{.under}}/{{"{"}}{{.underSingular}}_id} 122 func (v {{.camel}}Resource) Update(c buffalo.Context) error { 123 // Get the DB connection from the context 124 tx := c.Value("tx").(*pop.Connection) 125 // Allocate an empty {{.model}} 126 {{.varSingular}} := &models.{{.model}}{} 127 err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) 128 if err != nil { 129 return err 130 } 131 // Bind {{.singular}} to the html form elements 132 err = c.Bind({{.varSingular}}) 133 if err != nil { 134 return err 135 } 136 verrs, err := tx.ValidateAndUpdate({{.varSingular}}) 137 if err != nil { 138 return err 139 } 140 if verrs.HasAny() { 141 // Make {{.varSingular}} available inside the html template 142 c.Set("{{.varSingular}}", {{.varSingular}}) 143 // Make the errors available inside the html template 144 c.Set("errors", verrs) 145 // Render again the edit.html template that the user can 146 // correct the input. 147 return c.Render(422, r.HTML("{{.modelPluralUnder}}/edit.html")) 148 } 149 // If there are no errors set a success message 150 c.Flash().Add("success", "{{.model}} was updated successfully") 151 // and redirect to the {{.under}} index page 152 return c.Redirect(302, "/{{.under}}/%s",{{.varSingular}}.ID) 153 } 154 155 // Destroy deletes a {{.singular}} from the DB. This function is mapped 156 // to the path DELETE /{{.under}}/{{"{"}}{{.underSingular}}_id} 157 func (v {{.camel}}Resource) Destroy(c buffalo.Context) error { 158 // Get the DB connection from the context 159 tx := c.Value("tx").(*pop.Connection) 160 // Allocate an empty {{.model}} 161 {{.varSingular}} := &models.{{.model}}{} 162 // To find the {{.model}} the parameter {{.underSingular}}_id is used. 163 err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) 164 if err != nil { 165 return err 166 } 167 err = tx.Destroy({{.varSingular}}) 168 if err != nil { 169 return err 170 } 171 // If there are no errors set a flash message 172 c.Flash().Add("success", "{{.model}} was destroyed successfully") 173 // Redirect to the {{.under}} index page 174 return c.Redirect(302, "/{{.under}}") 175 }