github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/resource/templates/action.go.tmpl (about) 1 package actions 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gobuffalo/buffalo" 8 "github.com/gobuffalo/buffalo/render" 9 "github.com/gobuffalo/pop/v6" 10 11 "{{ .ModelPkg }}" 12 ) 13 14 // {{ .Name.Resource }}Resource is the resource for the {{.Name.Proper}} model 15 type {{ .Name.Resource }}Resource struct{ 16 buffalo.Resource 17 } 18 19 // List gets all {{.Name.Group }}. This function is mapped to the path 20 // GET /{{ .Name.URL }} 21 func (v {{ .Name.Resource }}Resource) List(c buffalo.Context) error { 22 // Get the DB connection from the context 23 tx, ok := c.Value("tx").(*pop.Connection) 24 if !ok { 25 return fmt.Errorf("no transaction found") 26 } 27 28 {{.Name.VarCasePlural}} := &models.{{ .Name.Group }}{} 29 30 // Paginate results. Params "page" and "per_page" control pagination. 31 // Default values are "page=1" and "per_page=20". 32 q := tx.PaginateFromParams(c.Params()) 33 34 // Retrieve all {{ .Name.Group }} from the DB 35 if err := q.All({{ .Name.VarCasePlural }}); err != nil { 36 return err 37 } 38 39 // Add the paginator to the context so it can be used in the template. 40 c.Set("pagination", q.Paginator) 41 c.Set("{{ .Name.VarCasePlural }}", {{ .Name.VarCasePlural }}) 42 43 return c.Render(http.StatusOK, r.HTML("/{{ .Name.File.Pluralize }}/index.plush.html")) 44 } 45 46 // Show gets the data for one {{.Name.Proper}}. This function is mapped to 47 // the path GET /{{ .Name.URL }}/{{"{"}}{{ .Name.ParamID }}} 48 func (v {{ .Name.Resource }}Resource) Show(c buffalo.Context) error { 49 // Get the DB connection from the context 50 tx, ok := c.Value("tx").(*pop.Connection) 51 if !ok { 52 return fmt.Errorf("no transaction found") 53 } 54 55 // Allocate an empty {{ .Name.Proper }} 56 {{ .Name.VarCaseSingle }} := &models.{{ .Name.Proper }}{} 57 58 // To find the {{ .Name.Proper }} the parameter {{ .Name.ParamID }} is used. 59 if err := tx.Find({{ .Name.VarCaseSingle }}, c.Param("{{ .Name.ParamID }}")); err != nil { 60 return c.Error(http.StatusNotFound, err) 61 } 62 63 c.Set("{{ .Name.VarCaseSingle }}", {{ .Name.VarCaseSingle }}) 64 65 return c.Render(http.StatusOK, r.HTML("/{{ .Name.File.Pluralize }}/show.plush.html")) 66 } 67 68 // New renders the form for creating a new {{ .Name.Proper }}. 69 // This function is mapped to the path GET /{{ .Name.URL }}/new 70 func (v {{ .Name.Resource }}Resource) New(c buffalo.Context) error { 71 c.Set("{{.Name.VarCaseSingle}}", &models.{{ .Name.Proper }}{}) 72 73 return c.Render(http.StatusOK, r.HTML("/{{ .Name.File.Pluralize }}/new.plush.html")) 74 } 75 76 // Create adds a {{ .Name.Proper }} to the DB. This function is mapped to the 77 // path POST /{{ .Name.URL }} 78 func (v {{ .Name.Resource }}Resource) Create(c buffalo.Context) error { 79 // Allocate an empty {{.Name.Proper}} 80 {{ .Name.VarCaseSingle }} := &models.{{ .Name.Proper }}{} 81 82 // Bind {{ .Name.VarCaseSingle }} to the html form elements 83 if err := c.Bind({{ .Name.VarCaseSingle }}); err != nil { 84 return err 85 } 86 87 // Get the DB connection from the context 88 tx, ok := c.Value("tx").(*pop.Connection) 89 if !ok { 90 return fmt.Errorf("no transaction found") 91 } 92 93 // Validate the data from the html form 94 verrs, err := tx.ValidateAndCreate({{ .Name.VarCaseSingle }}) 95 if err != nil { 96 return err 97 } 98 99 if verrs.HasAny() { 100 // Make the errors available inside the html template 101 c.Set("errors", verrs) 102 103 // Render again the new.html template that the user can 104 // correct the input. 105 c.Set("{{ .Name.VarCaseSingle }}", {{ .Name.VarCaseSingle }}) 106 107 return c.Render(http.StatusUnprocessableEntity, r.HTML("/{{ .Name.File.Pluralize }}/new.plush.html")) 108 } 109 110 // If there are no errors set a success message 111 c.Flash().Add("success","{{ .Name.VarCaseSingle }}.created.success") 112 113 // and redirect to the show page 114 return c.Redirect(http.StatusSeeOther, "{{ .Name.VarCaseSingle }}Path()", render.Data{"{{ .Name.ParamID }}": {{ .Name.VarCaseSingle }}.ID}) 115 } 116 117 // Edit renders a edit form for a {{ .Name.Proper }}. This function is 118 // mapped to the path GET /{{ .Name.URL }}/{{"{"}}{{ .Name.ParamID }}}/edit 119 func (v {{ .Name.Resource }}Resource) Edit(c buffalo.Context) error { 120 // Get the DB connection from the context 121 tx, ok := c.Value("tx").(*pop.Connection) 122 if !ok { 123 return fmt.Errorf("no transaction found") 124 } 125 126 // Allocate an empty {{ .Name.Proper }} 127 {{ .Name.VarCaseSingle }} := &models.{{ .Name.Proper }}{} 128 129 if err := tx.Find({{ .Name.VarCaseSingle }}, c.Param("{{ .Name.ParamID }}")); err != nil { 130 return c.Error(http.StatusNotFound, err) 131 } 132 133 c.Set("{{.Name.VarCaseSingle}}", {{.Name.VarCaseSingle}}) 134 135 return c.Render(http.StatusOK, r.HTML("/{{.Name.File.Pluralize}}/edit.plush.html")) 136 } 137 138 // Update changes a {{ .Name.Proper }} in the DB. This function is mapped to 139 // the path PUT /{{ .Name.URL}}/{{"{"}}{{ .Name.ParamID }}} 140 func (v {{ .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 fmt.Errorf("no transaction found") 145 } 146 147 // Allocate an empty {{ .Name.Proper }} 148 {{ .Name.VarCaseSingle }} := &models.{{ .Name.Proper }}{} 149 150 if err := tx.Find({{ .Name.VarCaseSingle }}, c.Param("{{ .Name.ParamID }}")); err != nil { 151 return c.Error(http.StatusNotFound, err) 152 } 153 154 // Bind {{ .Name.Proper }} to the html form elements 155 if err := c.Bind({{ .Name.VarCaseSingle }}); err != nil { 156 return err 157 } 158 159 verrs, err := tx.ValidateAndUpdate({{ .Name.VarCaseSingle }}) 160 if err != nil { 161 return 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 c.Set("{{ .Name.VarCaseSingle }}", {{ .Name.VarCaseSingle }}) 171 172 return c.Render(http.StatusUnprocessableEntity, r.HTML("/{{ .Name.File.Pluralize }}/edit.plush.html")) 173 } 174 175 // If there are no errors set a success message 176 c.Flash().Add("success", "{{ .Name.VarCaseSingle }}.updated.success") 177 178 // and redirect to the show page 179 return c.Redirect(http.StatusSeeOther, "{{ .Name.VarCaseSingle }}Path()", render.Data{"{{ .Name.ParamID }}": {{ .Name.VarCaseSingle }}.ID}) 180 } 181 182 // Destroy deletes a {{ .Name.Proper }} from the DB. This function is mapped 183 // to the path DELETE /{{ .Name.URL }}/{{"{"}}{{ .Name.ParamID }}} 184 func (v {{ .Name.Resource }}Resource) Destroy(c buffalo.Context) error { 185 // Get the DB connection from the context 186 tx, ok := c.Value("tx").(*pop.Connection) 187 if !ok { 188 return fmt.Errorf("no transaction found") 189 } 190 191 // Allocate an empty {{ .Name.Proper }} 192 {{ .Name.VarCaseSingle }} := &models.{{ .Name.Proper }}{} 193 194 // To find the {{ .Name.Proper }} the parameter {{ .Name.ParamID }} is used. 195 if err := tx.Find({{ .Name.VarCaseSingle }}, c.Param("{{ .Name.ParamID }}")); err != nil { 196 return c.Error(http.StatusNotFound, err) 197 } 198 199 if err := tx.Destroy({{ .Name.VarCaseSingle }}); err != nil { 200 return err 201 } 202 203 // If there are no errors set a flash message 204 c.Flash().Add("success", "{{ .Name.VarCaseSingle }}.destroyed.success") 205 206 // Redirect to the index page 207 return c.Redirect(http.StatusSeeOther, "{{ .Name.VarCasePlural }}Path()") 208 }