github.com/jacobsoderblom/buffalo@v0.11.0/generators/resource/templates/actions/resource-json-xml.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 headers so clients know how to paginate. 49 c.Response().Header().Set("X-Pagination", q.Paginator.String()) 50 51 return c.Render(200, r.{{.opts.MimeType}}({{.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.{{.opts.MimeType}}({{.opts.Model.VarCaseSingular}})) 72 } 73 74 // New default implementation. Returns a 404 75 func (v {{.opts.Name.Resource}}Resource) New(c buffalo.Context) error { 76 return c.Error(404, errors.New("not available")) 77 } 78 79 // Create adds a {{.opts.Model.Model}} to the DB. This function is mapped to the 80 // path POST /{{.opts.Name.URL}} 81 func (v {{.opts.Name.Resource}}Resource) Create(c buffalo.Context) error { 82 // Allocate an empty {{.opts.Model.Model}} 83 {{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{} 84 85 // Bind {{.opts.Model.VarCaseSingular}} to the html form elements 86 if err := c.Bind({{.opts.Model.VarCaseSingular}}); err != nil { 87 return errors.WithStack(err) 88 } 89 90 // Get the DB connection from the context 91 tx, ok := c.Value("tx").(*pop.Connection) 92 if !ok { 93 return errors.WithStack(errors.New("no transaction found")) 94 } 95 96 // Validate the data from the html form 97 verrs, err := tx.ValidateAndCreate({{.opts.Model.VarCaseSingular}}) 98 if err != nil { 99 return errors.WithStack(err) 100 } 101 102 if verrs.HasAny() { 103 // Render errors as {{.opts.MimeType}} 104 return c.Render(400, r.{{.opts.MimeType}}(verrs)) 105 } 106 107 return c.Render(201, r.{{.opts.MimeType}}({{.opts.Model.VarCaseSingular}})) 108 } 109 110 // Edit default implementation. Returns a 404 111 func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error { 112 return c.Error(404, errors.New("not available")) 113 } 114 115 // Update changes a {{.opts.Model.Model}} in the DB. This function is mapped to 116 // the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}} 117 func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error { 118 // Get the DB connection from the context 119 tx, ok := c.Value("tx").(*pop.Connection) 120 if !ok { 121 return errors.WithStack(errors.New("no transaction found")) 122 } 123 124 // Allocate an empty {{.opts.Model.Model}} 125 {{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{} 126 127 if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil { 128 return c.Error(404, err) 129 } 130 131 // Bind {{.opts.Model.Model}} to the html form elements 132 if err := c.Bind({{.opts.Model.VarCaseSingular}}); err != nil { 133 return errors.WithStack(err) 134 } 135 136 verrs, err := tx.ValidateAndUpdate({{.opts.Model.VarCaseSingular}}) 137 if err != nil { 138 return errors.WithStack(err) 139 } 140 141 if verrs.HasAny() { 142 // Render errors as {{.opts.MimeType}} 143 return c.Render(400, r.{{.opts.MimeType}}(verrs)) 144 } 145 146 return c.Render(200, r.{{.opts.MimeType}}({{.opts.Model.VarCaseSingular}})) 147 } 148 149 // Destroy deletes a {{.opts.Model.Model}} from the DB. This function is mapped 150 // to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}} 151 func (v {{.opts.Name.Resource}}Resource) Destroy(c buffalo.Context) error { 152 // Get the DB connection from the context 153 tx, ok := c.Value("tx").(*pop.Connection) 154 if !ok { 155 return errors.WithStack(errors.New("no transaction found")) 156 } 157 158 // Allocate an empty {{.opts.Model.Model}} 159 {{.opts.Model.VarCaseSingular}} := &models.{{.opts.Model.Model}}{} 160 161 // To find the {{.opts.Model.Model}} the parameter {{.opts.Name.ParamID}} is used. 162 if err := tx.Find({{.opts.Model.VarCaseSingular}}, c.Param("{{.opts.Name.ParamID}}")); err != nil { 163 return c.Error(404, err) 164 } 165 166 if err := tx.Destroy({{.opts.Model.VarCaseSingular}}); err != nil { 167 return errors.WithStack(err) 168 } 169 170 return c.Render(200, r.{{.opts.MimeType}}({{.opts.Model.VarCaseSingular}})) 171 }