github.com/mayra-cabrera/buffalo@v0.9.4-0.20170814145312-66d2e7772f11/generators/resource/templates/actions/resource-json-xml.go.tmpl (about) 1 package actions 2 3 import ( 4 "github.com/pkg/errors" 5 6 "github.com/gobuffalo/buffalo" 7 "github.com/markbates/pop" 8 "{{.modelsPath}}" 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 ({{.model}}) 18 // DB Table: Plural ({{.modelPlural}}) 19 // Resource: Plural ({{.modelPlural}}) 20 // Path: Plural (/{{.under}}) 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 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 // Paginate results. Params "page" and "per_page" control pagination. 34 // Default values are "page=1" and "per_page=20". 35 q := tx.PaginateFromParams(c.Params()) 36 // You can order your list here. Just change 37 err := q.All({{.varPlural}}) 38 // to: 39 // err := q.Order("created_at desc").All({{.varPlural}}) 40 if err != nil { 41 return errors.WithStack(err) 42 } 43 c.Response().Header().Set("X-Pagination", q.Paginator.String()) 44 return c.Render(200, r.{{.renderFunction}}({{.varPlural}})) 45 } 46 47 // Show gets the data for one {{.model}}. This function is mapped to 48 // the path GET /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_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 {{.modelSingularUnder}}_id is used. 55 err := tx.Find({{.varSingular}}, c.Param("{{.modelSingularUnder}}_id")) 56 if err != nil { 57 return errors.WithStack(err) 58 } 59 return c.Render(200, r.{{.renderFunction}}({{.varSingular}})) 60 } 61 62 // New default implementation. Returns a 404 63 func (v {{.camel}}Resource) New(c buffalo.Context) error { 64 return c.Error(404, errors.New("not available")) 65 } 66 67 // Create adds a {{.singular}} to the DB. This function is mapped to the 68 // path POST /{{.resourceURL}} 69 func (v {{.camel}}Resource) Create(c buffalo.Context) error { 70 // Allocate an empty {{.model}} 71 {{.varSingular}} := &models.{{.model}}{} 72 // Bind {{.varSingular}} to the html form elements 73 err := c.Bind({{.varSingular}}) 74 if err != nil { 75 return errors.WithStack(err) 76 } 77 // Get the DB connection from the context 78 tx := c.Value("tx").(*pop.Connection) 79 // Validate the data from the html form 80 verrs, err := tx.ValidateAndCreate({{.varSingular}}) 81 if err != nil { 82 return errors.WithStack(err) 83 } 84 if verrs.HasAny() { 85 // Render errors as {{.renderFunction}} 86 return c.Render(400, r.{{.renderFunction}}(verrs)) 87 } 88 // Success! 89 return c.Render(201, r.{{.renderFunction}}({{.varSingular}})) 90 } 91 92 // Edit default implementation. Returns a 404 93 func (v {{.camel}}Resource) Edit(c buffalo.Context) error { 94 return c.Error(404, errors.New("not available")) 95 } 96 97 // Update changes a {{.singular}} in the DB. This function is mapped to 98 // the path PUT /{{.resourceURL}}/{{"{"}}{{.modelSingularUnder}}_id} 99 func (v {{.camel}}Resource) Update(c buffalo.Context) error { 100 // Get the DB connection from the context 101 tx := c.Value("tx").(*pop.Connection) 102 // Allocate an empty {{.model}} 103 {{.varSingular}} := &models.{{.model}}{} 104 err := tx.Find({{.varSingular}}, c.Param("{{.modelSingularUnder}}_id")) 105 if err != nil { 106 return errors.WithStack(err) 107 } 108 // Bind {{.singular}} to the html form elements 109 err = c.Bind({{.varSingular}}) 110 if err != nil { 111 return errors.WithStack(err) 112 } 113 verrs, err := tx.ValidateAndUpdate({{.varSingular}}) 114 if err != nil { 115 return errors.WithStack(err) 116 } 117 if verrs.HasAny() { 118 // Render errors as {{.renderFunction}} 119 return c.Render(400, r.{{.renderFunction}}(verrs)) 120 } 121 // Success! 122 return c.Render(200, r.{{.renderFunction}}({{.varSingular}})) 123 } 124 125 // Destroy deletes a {{.singular}} from the DB. This function is mapped 126 // to the path DELETE /{{.under}}/{{"{"}}{{.modelSingularUnder}}_id} 127 func (v {{.camel}}Resource) Destroy(c buffalo.Context) error { 128 // Get the DB connection from the context 129 tx := c.Value("tx").(*pop.Connection) 130 // Allocate an empty {{.model}} 131 {{.varSingular}} := &models.{{.model}}{} 132 // To find the {{.model}} the parameter {{.modelSingularUnder}}_id is used. 133 err := tx.Find({{.varSingular}}, c.Param("{{.modelSingularUnder}}_id")) 134 if err != nil { 135 return errors.WithStack(err) 136 } 137 err = tx.Destroy({{.varSingular}}) 138 if err != nil { 139 return errors.WithStack(err) 140 } 141 // Success! 142 return c.Render(200, r.{{.renderFunction}}({{.varSingular}})) 143 }