actions/dutonians.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 (Dutonian)
// DB Table: Plural (dutonians)
// Resource: Plural (Dutonians)
// Path: Plural (/dutonians)
// View Template Folder: Plural (/templates/dutonians/)
// DutoniansResource is the resource for the Dutonian model
type DutoniansResource struct {
buffalo.Resource
}
// List gets all Dutonians. This function is mapped to the path
// GET /dutonians
func (v DutoniansResource) 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"))
}
dutonians := &models.Dutonians{}
// 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 Dutonians from the DB
if err := q.All(dutonians); 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, dutonians))
}
// Show gets the data for one Dutonian. This function is mapped to
// the path GET /dutonians/{dutonian_id}
func (v DutoniansResource) 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 Dutonian
dutonian := &models.Dutonian{}
// To find the Dutonian the parameter dutonian_id is used.
if err := tx.Find(dutonian, c.Param("dutonian_id")); err != nil {
return c.Error(404, err)
}
return c.Render(200, r.Auto(c, dutonian))
}
// New renders the form for creating a new Dutonian.
// This function is mapped to the path GET /dutonians/new
func (v DutoniansResource) New(c buffalo.Context) error {
return c.Render(200, r.Auto(c, &models.Dutonian{}))
}
// Create adds a Dutonian to the DB. This function is mapped to the
// path POST /dutonians
func (v DutoniansResource) 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 Dutonian
dutonian := &models.Dutonian{
UserID: user.ID,
UpdatedBy: user.ID,
}
// Bind dutonian to the html form elements
if err := c.Bind(dutonian); err != nil {
return errors.WithStack(err)
}
if dutonian.Photo.Valid() {
dutonian.PhotoName = dutonian.Photo.Filename
}
// Validate the data from the html form
verrs, err := tx.ValidateAndCreate(dutonian)
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, dutonian))
}
// If there are no errors set a success message
c.Flash().Add("success", "Dutonian was created successfully")
// and redirect to the dutonians index page
return c.Render(201, r.Auto(c, dutonian))
}
// Edit renders a edit form for a Dutonian. This function is
// mapped to the path GET /dutonians/{dutonian_id}/edit
func (v DutoniansResource) 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 Dutonian
dutonian := &models.Dutonian{}
if err := tx.Find(dutonian, c.Param("dutonian_id")); err != nil {
return c.Error(404, err)
}
return c.Render(200, r.Auto(c, dutonian))
}
// Update changes a Dutonian in the DB. This function is mapped to
// the path PUT /dutonians/{dutonian_id}
func (v DutoniansResource) 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 Dutonian
dutonian := &models.Dutonian{
UpdatedBy: user.ID,
}
if err := tx.Find(dutonian, c.Param("dutonian_id")); err != nil {
return c.Error(404, err)
}
// Bind Dutonian to the html form elements
if err := c.Bind(dutonian); err != nil {
return errors.WithStack(err)
}
// to solve for this, don't allow filename to be null, but if it's not set then maybe set it to ""
dutonian.UpdatedBy = user.ID
if dutonian.Photo.Valid() {
dutonian.PhotoName = dutonian.Photo.Filename
}
verrs, err := tx.ValidateAndUpdate(dutonian)
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, dutonian))
}
// If there are no errors set a success message
c.Flash().Add("success", "Dutonian was updated successfully")
// and redirect to the dutonians index page
return c.Render(200, r.Auto(c, dutonian))
}
// Destroy deletes a Dutonian from the DB. This function is mapped
// to the path DELETE /dutonians/{dutonian_id}
func (v DutoniansResource) 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 Dutonian
dutonian := &models.Dutonian{}
// To find the Dutonian the parameter dutonian_id is used.
if err := tx.Find(dutonian, c.Param("dutonian_id")); err != nil {
return c.Error(404, err)
}
if err := tx.Destroy(dutonian); err != nil {
return errors.WithStack(err)
}
// If there are no errors set a flash message
c.Flash().Add("success", "Dutonian was destroyed successfully")
// Redirect to the dutonians index page
return c.Render(200, r.Auto(c, dutonian))
}