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