This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '365-resource-generator-json' of https://github.com/stan…
…islas-m/buffalo into stanislas-m-365-resource-generator-json
- Loading branch information
Showing
7 changed files
with
205 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[{ | ||
"path": "templates/users/_form.html", | ||
"absent": true | ||
}, | ||
{ | ||
"path": "templates/users/edit.html", | ||
"absent": true | ||
}, | ||
{ | ||
"path": "templates/users/index.html", | ||
"absent": true | ||
}, | ||
{ | ||
"path": "templates/users/new.html", | ||
"absent": true | ||
}, | ||
{ | ||
"path": "templates/users/show.html", | ||
"absent": true | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
generators/resource/templates/actions/resource-json.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package actions | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/gobuffalo/buffalo" | ||
"github.com/markbates/pop" | ||
"{{.modelsPath}}" | ||
) | ||
|
||
// This file is generated by Buffalo. It offers a basic structure for | ||
// adding, editing and deleting a page. If your model is more | ||
// complex or you need more than the basic implementation you need to | ||
// edit this file. | ||
|
||
// Following naming logic is implemented in Buffalo: | ||
// Model: Singular ({{.model}}) | ||
// DB Table: Plural ({{.modelPlural}}) | ||
// Resource: Plural ({{.modelPlural}}) | ||
// Path: Plural (/{{.under}}) | ||
|
||
// {{.modelPlural}}Resource is the resource for the {{.singular}} model | ||
type {{.camel}}Resource struct{ | ||
buffalo.Resource | ||
} | ||
|
||
// List gets all {{.modelPlural}}. This function is mapped to the the path | ||
// GET /{{.under}} | ||
func (v {{.camel}}Resource) List(c buffalo.Context) error { | ||
// Get the DB connection from the context | ||
tx := c.Value("tx").(*pop.Connection) | ||
{{.varPlural}} := &models.{{.modelPlural}}{} | ||
// You can order your list here. Just change | ||
err := tx.All({{.varPlural}}) | ||
// to: | ||
// err := tx.Order("(case when completed then 1 else 2 end) desc, lower([sort_parameter]) asc").All({{.downFirstCap}}) | ||
// Don't forget to change [sort_parameter] to the parameter of | ||
// your model, which should be used for sorting. | ||
if err != nil { | ||
return err | ||
} | ||
return c.Render(200, r.JSON({{.varPlural}})) | ||
} | ||
|
||
// Show gets the data for one {{.model}}. This function is mapped to | ||
// the path GET /{{.under}}/{{"{"}}{{.underSingular}}_id} | ||
func (v {{.camel}}Resource) Show(c buffalo.Context) error { | ||
// Get the DB connection from the context | ||
tx := c.Value("tx").(*pop.Connection) | ||
// Allocate an empty {{.model}} | ||
{{.varSingular}} := &models.{{.model}}{} | ||
// To find the {{.model}} the parameter {{.underSingular}}_id is used. | ||
err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) | ||
if err != nil { | ||
return err | ||
} | ||
return c.Render(200, r.JSON({{.varSingular}})) | ||
} | ||
|
||
// New default implementation. Returns a 404 | ||
func (v {{.camel}}Resource) New(c buffalo.Context) error { | ||
return c.Error(404, errors.New("not available")) | ||
} | ||
|
||
// Create adds a {{.singular}} to the DB. This function is mapped to the | ||
// path POST /{{.under}} | ||
func (v {{.camel}}Resource) Create(c buffalo.Context) error { | ||
// Allocate an empty {{.model}} | ||
{{.varSingular}} := &models.{{.model}}{} | ||
// Bind {{.varSingular}} to the html form elements | ||
err := c.Bind({{.varSingular}}) | ||
if err != nil { | ||
return err | ||
} | ||
// Get the DB connection from the context | ||
tx := c.Value("tx").(*pop.Connection) | ||
// Validate the data from the html form | ||
verrs, err := tx.ValidateAndCreate({{.varSingular}}) | ||
if err != nil { | ||
return err | ||
} | ||
if verrs.HasAny() { | ||
// Render errors as JSON | ||
return c.Render(400, r.JSON(verrs)) | ||
} | ||
// Success! | ||
return c.Render(201, nil) | ||
} | ||
|
||
// Edit default implementation. Returns a 404 | ||
func (v {{.camel}}Resource) Edit(c buffalo.Context) error { | ||
return c.Error(404, errors.New("not available")) | ||
} | ||
|
||
// Update changes a {{.singular}} in the DB. This function is mapped to | ||
// the path PUT /{{.under}}/{{"{"}}{{.underSingular}}_id} | ||
func (v {{.camel}}Resource) Update(c buffalo.Context) error { | ||
// Get the DB connection from the context | ||
tx := c.Value("tx").(*pop.Connection) | ||
// Allocate an empty {{.model}} | ||
{{.varSingular}} := &models.{{.model}}{} | ||
err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) | ||
if err != nil { | ||
return err | ||
} | ||
// Bind {{.singular}} to the html form elements | ||
err = c.Bind({{.varSingular}}) | ||
if err != nil { | ||
return err | ||
} | ||
verrs, err := tx.ValidateAndUpdate({{.varSingular}}) | ||
if err != nil { | ||
return err | ||
} | ||
if verrs.HasAny() { | ||
// Render errors as JSON | ||
return c.Render(400, r.JSON(verrs)) | ||
} | ||
// Success! | ||
return c.Render(200, nil) | ||
} | ||
|
||
// Destroy deletes a {{.singular}} from the DB. This function is mapped | ||
// to the path DELETE /{{.under}}/{{"{"}}{{.underSingular}}_id} | ||
func (v {{.camel}}Resource) Destroy(c buffalo.Context) error { | ||
// Get the DB connection from the context | ||
tx := c.Value("tx").(*pop.Connection) | ||
// Allocate an empty {{.model}} | ||
{{.varSingular}} := &models.{{.model}}{} | ||
// To find the {{.model}} the parameter {{.underSingular}}_id is used. | ||
err := tx.Find({{.varSingular}}, c.Param("{{.underSingular}}_id")) | ||
if err != nil { | ||
return err | ||
} | ||
err = tx.Destroy({{.varSingular}}) | ||
if err != nil { | ||
return err | ||
} | ||
// Success! | ||
return c.Render(200, nil) | ||
} |
12 changes: 12 additions & 0 deletions
12
generators/resource/templates/actions/resource-json_test.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package actions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
{{ range $a := .actions }} | ||
func (as *ActionSuite) Test_{{$.camel}}Resource_{{ camelize $a }}() { | ||
as.Fail("Not Implemented!") | ||
} | ||
{{ end }} |