github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/resource/templates/use_model/actions/resource-name.go.tmpl (about) 1 package actions 2 3 import ( 4 5 "fmt" 6 "net/http" 7 "github.com/gobuffalo/buffalo" 8 "github.com/gobuffalo/pop/v5" 9 "github.com/gobuffalo/x/responder" 10 "{{.opts.App.ModelsPkg}}" 11 ) 12 13 // This file is generated by Buffalo. It offers a basic structure for 14 // adding, editing and deleting a page. If your model is more 15 // complex or you need more than the basic implementation you need to 16 // edit this file. 17 18 // Following naming logic is implemented in Buffalo: 19 // Model: Singular ({{.opts.Model.Proper}}) 20 // DB Table: Plural ({{.opts.Model.Tableize}}) 21 // Resource: Plural ({{.opts.Name.Resource}}) 22 // Path: Plural (/{{.opts.Name.URL}}) 23 // View Template Folder: Plural (/templates/{{.opts.Name.File.Pluralize}}/) 24 25 // {{.opts.Name.Resource}}Resource is the resource for the {{.opts.Model.Proper}} model 26 type {{.opts.Name.Resource}}Resource struct{ 27 buffalo.Resource 28 } 29 30 // List gets all {{.opts.Model.Group}}. This function is mapped to the path 31 // GET /{{.opts.Name.URL}} 32 func (v {{.opts.Name.Resource}}Resource) List(c buffalo.Context) error { 33 // Get the DB connection from the context 34 tx, ok := c.Value("tx").(*pop.Connection) 35 if !ok { 36 return fmt.Errorf("no transaction found") 37 } 38 39 {{.opts.Model.VarCasePlural}} := &models.{{.opts.Model.Group}}{} 40 41 // Paginate results. Params "page" and "per_page" control pagination. 42 // Default values are "page=1" and "per_page=20". 43 q := tx.PaginateFromParams(c.Params()) 44 45 // Retrieve all {{.opts.Model.Group}} from the DB 46 if err := q.All({{.opts.Model.VarCasePlural}}); err != nil { 47 return err 48 } 49 50 return responder.Wants("html", func (c buffalo.Context) error { 51 // Add the paginator to the context so it can be used in the template. 52 c.Set("pagination", q.Paginator) 53 54 c.Set("{{.opts.Model.VarCasePlural}}", {{.opts.Model.VarCasePlural}}) 55 return c.Render(http.StatusOK, r.HTML("/{{.opts.Name.File.Pluralize}}/index.plush.html")) 56 }).Wants("json", func (c buffalo.Context) error { 57 return c.Render(200, r.JSON({{.opts.Model.VarCasePlural}})) 58 }).Wants("xml", func (c buffalo.Context) error { 59 return c.Render(200, r.XML({{.opts.Model.VarCasePlural}})) 60 }).Respond(c) 61 } 62 63 // Show gets the data for one {{.opts.Model.Proper}}. This function is mapped to 64 // the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}} 65 func (v {{.opts.Name.Resource}}Resource) Show(c buffalo.Context) error { 66 // Get the DB connection from the context 67 tx, ok := c.Value("tx").(*pop.Connection) 68 if !ok { 69 return fmt.Errorf("no transaction found") 70 } 71 72 // Allocate an empty {{.opts.Model.Proper}} 73 {{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{} 74 75 // To find the {{.opts.Model.Proper}} the parameter {{.opts.Name.ParamID}} is used. 76 if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil { 77 return c.Error(http.StatusNotFound, err) 78 } 79 80 return responder.Wants("html", func (c buffalo.Context) error { 81 c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}}) 82 83 return c.Render(http.StatusOK, r.HTML("/{{.opts.Name.File.Pluralize}}/show.plush.html")) 84 }).Wants("json", func (c buffalo.Context) error { 85 return c.Render(200, r.JSON({{.opts.Model.VarCaseSingle}})) 86 }).Wants("xml", func (c buffalo.Context) error { 87 return c.Render(200, r.XML({{.opts.Model.VarCaseSingle}})) 88 }).Respond(c) 89 } 90 91 {{ if .opts.App.AsWeb -}} 92 // New renders the form for creating a new {{.opts.Model.Proper}}. 93 // This function is mapped to the path GET /{{.opts.Name.URL}}/new 94 func (v {{.opts.Name.Resource}}Resource) New(c buffalo.Context) error { 95 c.Set("{{.opts.Model.VarCaseSingle}}", &models.{{.opts.Model.Proper}}{}) 96 97 return c.Render(http.StatusOK, r.HTML("/{{.opts.Name.File.Pluralize}}/new.plush.html")) 98 } 99 {{ end -}} 100 101 // Create adds a {{.opts.Model.Proper}} to the DB. This function is mapped to the 102 // path POST /{{.opts.Name.URL}} 103 func (v {{.opts.Name.Resource}}Resource) Create(c buffalo.Context) error { 104 // Allocate an empty {{.opts.Model.Proper}} 105 {{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{} 106 107 // Bind {{.opts.Model.VarCaseSingle}} to the html form elements 108 if err := c.Bind({{.opts.Model.VarCaseSingle}}); err != nil { 109 return err 110 } 111 112 // Get the DB connection from the context 113 tx, ok := c.Value("tx").(*pop.Connection) 114 if !ok { 115 return fmt.Errorf("no transaction found") 116 } 117 118 // Validate the data from the html form 119 verrs, err := tx.ValidateAndCreate({{.opts.Model.VarCaseSingle}}) 120 if err != nil { 121 return err 122 } 123 124 if verrs.HasAny() { 125 return responder.Wants("html", func (c buffalo.Context) error { 126 // Make the errors available inside the html template 127 c.Set("errors", verrs) 128 129 // Render again the new.html template that the user can 130 // correct the input. 131 c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}}) 132 133 return c.Render(http.StatusUnprocessableEntity, r.HTML("/{{.opts.Name.File.Pluralize}}/new.plush.html")) 134 }).Wants("json", func (c buffalo.Context) error { 135 return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs)) 136 }).Wants("xml", func (c buffalo.Context) error { 137 return c.Render(http.StatusUnprocessableEntity, r.XML(verrs)) 138 }).Respond(c) 139 } 140 141 return responder.Wants("html", func (c buffalo.Context) error { 142 // If there are no errors set a success message 143 c.Flash().Add("success", T.Translate(c, "{{.opts.Model.VarCaseSingle}}.created.success")) 144 145 // and redirect to the show page 146 return c.Redirect(http.StatusSeeOther, "/{{.opts.Name.URL}}/%v", {{.opts.Model.VarCaseSingle}}.ID) 147 }).Wants("json", func (c buffalo.Context) error { 148 return c.Render(http.StatusCreated, r.JSON({{.opts.Model.VarCaseSingle}})) 149 }).Wants("xml", func (c buffalo.Context) error { 150 return c.Render(http.StatusCreated, r.XML({{.opts.Model.VarCaseSingle}})) 151 }).Respond(c) 152 } 153 154 {{ if .opts.App.AsWeb -}} 155 // Edit renders a edit form for a {{.opts.Model.Proper}}. This function is 156 // mapped to the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}/edit 157 func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error { 158 // Get the DB connection from the context 159 tx, ok := c.Value("tx").(*pop.Connection) 160 if !ok { 161 return fmt.Errorf("no transaction found") 162 } 163 164 // Allocate an empty {{.opts.Model.Proper}} 165 {{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{} 166 167 if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil { 168 return c.Error(http.StatusNotFound, err) 169 } 170 171 c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}}) 172 return c.Render(http.StatusOK, r.HTML("/{{.opts.Name.File.Pluralize}}/edit.plush.html")) 173 } 174 {{ end -}} 175 176 // Update changes a {{.opts.Model.Proper}} in the DB. This function is mapped to 177 // the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}} 178 func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error { 179 // Get the DB connection from the context 180 tx, ok := c.Value("tx").(*pop.Connection) 181 if !ok { 182 return fmt.Errorf("no transaction found") 183 } 184 185 // Allocate an empty {{.opts.Model.Proper}} 186 {{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{} 187 188 if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil { 189 return c.Error(http.StatusNotFound, err) 190 } 191 192 // Bind {{.opts.Model.Proper}} to the html form elements 193 if err := c.Bind({{.opts.Model.VarCaseSingle}}); err != nil { 194 return err 195 } 196 197 verrs, err := tx.ValidateAndUpdate({{.opts.Model.VarCaseSingle}}) 198 if err != nil { 199 return err 200 } 201 202 if verrs.HasAny() { 203 return responder.Wants("html", func (c buffalo.Context) error { 204 // Make the errors available inside the html template 205 c.Set("errors", verrs) 206 207 // Render again the edit.html template that the user can 208 // correct the input. 209 c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}}) 210 211 return c.Render(http.StatusUnprocessableEntity, r.HTML("/{{.opts.Name.File.Pluralize}}/edit.plush.html")) 212 }).Wants("json", func (c buffalo.Context) error { 213 return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs)) 214 }).Wants("xml", func (c buffalo.Context) error { 215 return c.Render(http.StatusUnprocessableEntity, r.XML(verrs)) 216 }).Respond(c) 217 } 218 219 return responder.Wants("html", func (c buffalo.Context) error { 220 // If there are no errors set a success message 221 c.Flash().Add("success", T.Translate(c, "{{.opts.Model.VarCaseSingle}}.updated.success")) 222 223 // and redirect to the show page 224 return c.Redirect(http.StatusSeeOther, "/{{.opts.Name.URL}}/%v", {{.opts.Model.VarCaseSingle}}.ID) 225 }).Wants("json", func (c buffalo.Context) error { 226 return c.Render(http.StatusOK, r.JSON({{.opts.Model.VarCaseSingle}})) 227 }).Wants("xml", func (c buffalo.Context) error { 228 return c.Render(http.StatusOK, r.XML({{.opts.Model.VarCaseSingle}})) 229 }).Respond(c) 230 } 231 232 // Destroy deletes a {{.opts.Model.Proper}} from the DB. This function is mapped 233 // to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}} 234 func (v {{.opts.Name.Resource}}Resource) Destroy(c buffalo.Context) error { 235 // Get the DB connection from the context 236 tx, ok := c.Value("tx").(*pop.Connection) 237 if !ok { 238 return fmt.Errorf("no transaction found") 239 } 240 241 // Allocate an empty {{.opts.Model.Proper}} 242 {{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{} 243 244 // To find the {{.opts.Model.Proper}} the parameter {{.opts.Name.ParamID}} is used. 245 if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil { 246 return c.Error(http.StatusNotFound, err) 247 } 248 249 if err := tx.Destroy({{.opts.Model.VarCaseSingle}}); err != nil { 250 return err 251 } 252 253 return responder.Wants("html", func (c buffalo.Context) error { 254 // If there are no errors set a flash message 255 c.Flash().Add("success", T.Translate(c, "{{.opts.Model.VarCaseSingle}}.destroyed.success")) 256 257 // Redirect to the index page 258 return c.Redirect(http.StatusSeeOther, "/{{.opts.Name.URL}}") 259 }).Wants("json", func (c buffalo.Context) error { 260 return c.Render(http.StatusOK, r.JSON({{.opts.Model.VarCaseSingle}})) 261 }).Wants("xml", func (c buffalo.Context) error { 262 return c.Render(http.StatusOK, r.XML({{.opts.Model.VarCaseSingle}})) 263 }).Respond(c) 264 }