actions/abstracts.go
package actions
import (
"github.com/devrelcollective/xela/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop"
"github.com/pkg/errors"
)
// 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 (Abstract)
// DB Table: Plural (abstracts)
// Resource: Plural (Abstracts)
// Path: Plural (/abstracts)
// View Template Folder: Plural (/templates/abstracts/)
// AbstractsResource is the resource for the Abstract model
type AbstractsResource struct {
buffalo.Resource
}
// List gets all Abstracts. This function is mapped to the path
// GET /abstracts
func (v AbstractsResource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
abstracts := &models.Abstracts{}
// Paginate results. Params "page" and "per_page" control pagination.
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())
// Retrieve all Abstracts from the DB
if err := q.All(abstracts); err != nil {
return errors.WithStack(err)
}
// Add the paginator to the context so it can be used in the template.
c.Set("pagination", q.Paginator)
return c.Render(200, r.Auto(c, abstracts))
}
// Show gets the data for one Abstract. This function is mapped to
// the path GET /abstracts/{abstract_id}
func (v AbstractsResource) Show(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
// Allocate an empty Abstract
abstract := &models.Abstract{}
// To find the Abstract the parameter abstract_id is used.
if err := tx.Find(abstract, c.Param("abstract_id")); err != nil {
return c.Error(404, err)
}
return c.Render(200, r.Auto(c, abstract))
}
// New renders the form for creating a new Abstract.
// This function is mapped to the path GET /abstracts/new
func (v AbstractsResource) New(c buffalo.Context) error {
return c.Render(200, r.Auto(c, &models.Abstract{}))
}
// Create adds a Abstract to the DB. This function is mapped to the
// path POST /abstracts
func (v AbstractsResource) Create(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
user := c.Value("current_user").(*models.User)
// Allocate an empty Abstract
abstract := &models.Abstract{
UserID: user.ID,
UpdatedBy: user.ID,
}
// Bind abstract to the html form elements
if err := c.Bind(abstract); err != nil {
return errors.WithStack(err)
}
// Validate the data from the html form
verrs, err := tx.ValidateAndCreate(abstract)
if err != nil {
return errors.WithStack(err)
}
if verrs.HasAny() {
// Make the errors available inside the html template
c.Set("errors", verrs)
// Render again the new.html template that the user can
// correct the input.
return c.Render(422, r.Auto(c, abstract))
}
// If there are no errors set a success message
c.Flash().Add("success", "Abstract was created successfully")
// and redirect to the abstracts index page
return c.Render(201, r.Auto(c, abstract))
}
// Edit renders a edit form for a Abstract. This function is
// mapped to the path GET /abstracts/{abstract_id}/edit
func (v AbstractsResource) Edit(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
// Allocate an empty Abstract
abstract := &models.Abstract{}
if err := tx.Find(abstract, c.Param("abstract_id")); err != nil {
return c.Error(404, err)
}
return c.Render(200, r.Auto(c, abstract))
}
// Update changes a Abstract in the DB. This function is mapped to
// the path PUT /abstracts/{abstract_id}
func (v AbstractsResource) Update(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
user := c.Value("current_user").(*models.User)
// Allocate an empty Abstract
abstract := &models.Abstract{
UpdatedBy: user.ID,
}
if err := tx.Find(abstract, c.Param("abstract_id")); err != nil {
return c.Error(404, err)
}
// Bind Abstract to the html form elements
if err := c.Bind(abstract); err != nil {
return errors.WithStack(err)
}
verrs, err := tx.ValidateAndUpdate(abstract)
if err != nil {
return errors.WithStack(err)
}
if verrs.HasAny() {
// Make the errors available inside the html template
c.Set("errors", verrs)
// Render again the edit.html template that the user can
// correct the input.
return c.Render(422, r.Auto(c, abstract))
}
// If there are no errors set a success message
c.Flash().Add("success", "Abstract was updated successfully")
// and redirect to the abstracts index page
return c.Render(200, r.Auto(c, abstract))
}
// Destroy deletes a Abstract from the DB. This function is mapped
// to the path DELETE /abstracts/{abstract_id}
func (v AbstractsResource) Destroy(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
// Allocate an empty Abstract
abstract := &models.Abstract{}
// To find the Abstract the parameter abstract_id is used.
if err := tx.Find(abstract, c.Param("abstract_id")); err != nil {
return c.Error(404, err)
}
if err := tx.Destroy(abstract); err != nil {
return errors.WithStack(err)
}
// If there are no errors set a flash message
c.Flash().Add("success", "Abstract was destroyed successfully")
// Redirect to the abstracts index page
return c.Render(200, r.Auto(c, abstract))
}