Created
March 22, 2017 18:52
-
-
Save as27/c5bf31a0f3e8faef71044d44ee870028 to your computer and use it in GitHub Desktop.
Generated file
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
package actions | |
import ( | |
"tutorials/db01/models" | |
"github.com/gobuffalo/buffalo" | |
"github.com/markbates/pop" | |
) | |
// This file is generated by Buffalo. It offers a basic structure for adding, editing and deleting | |
// a page. If your model is more complex you need to edit this file. | |
// PagesResource is the resource for the page model | |
type PagesResource struct { | |
buffalo.Resource | |
} | |
// List gets all Pages. This function is mapped to the the path GET /pages | |
func (v PagesResource) List(c buffalo.Context) error { | |
tx := c.Value("tx").(*pop.Connection) | |
pages := &models.Pages{} | |
// You can order your list here, with the order method like this: | |
// err := tx.Order("(case when completed then 1 else 2 end) desc, lower(title) asc").All(pages) | |
err := tx.All(pages) | |
if err != nil { | |
return err | |
} | |
c.Set("pages", pages) | |
return c.Render(200, r.HTML("pages/index.html")) | |
} | |
// Show gets the data for one page. This function is mapped to the path GET /pages/[page_id] | |
func (v PagesResource) Show(c buffalo.Context) error { | |
tx := c.Value("tx").(*pop.Connection) | |
page := &models.Page{} | |
err := tx.Find(page, c.Param("page_id")) | |
if err != nil { | |
return err | |
} | |
c.Set("page", page) | |
return c.Render(200, r.HTML("pages/show.html")) | |
} | |
// New renders the formular for creating a new page. | |
// This function is mapped to the path GET /pages/new | |
func (v PagesResource) New(c buffalo.Context) error { | |
c.Set("page", &models.Page{}) | |
return c.Render(200, r.HTML("pages/new.html")) | |
} | |
// Create adds a page to the DB. This function is mapped to the path POST /pages | |
func (v PagesResource) Create(c buffalo.Context) error { | |
t := &models.Page{} | |
err := c.Bind(t) | |
if err != nil { | |
return err | |
} | |
tx := c.Value("tx").(*pop.Connection) | |
verrs, err := tx.ValidateAndCreate(t) | |
if err != nil { | |
return err | |
} | |
if verrs.HasAny() { | |
c.Set("page", t) | |
c.Set("errors", verrs.Errors) | |
return c.Render(422, r.HTML("pages/new.html")) | |
} | |
c.Flash().Add("success", "Page was created successfully") | |
return c.Redirect(301, "/pages") | |
} | |
func (v PagesResource) Edit(c buffalo.Context) error { | |
return c.Render(200, r.String("Pages#Edit")) | |
} | |
// Update changes a page in the DB. This function is mapped to the path PUT /pages/[page_id] | |
func (v PagesResource) Update(c buffalo.Context) error { | |
tx := c.Value("tx").(*pop.Connection) | |
t := &models.Page{} | |
err := tx.Find(t, c.Param("page_id")) | |
if err != nil { | |
return err | |
} | |
err = c.Bind(t) | |
if err != nil { | |
return err | |
} | |
err = tx.Update(t) | |
if err != nil { | |
return err | |
} | |
c.Set("page", t) | |
return c.Render(200, r.Template("text/javascript", "pages/update.js")) | |
} | |
// Destroy deletes a page from the DB. This function is mapped to the path DELETE /pages/[page_id] | |
func (v PagesResource) Destroy(c buffalo.Context) error { | |
return c.Render(200, r.String("Pages#Destroy")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment