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