actions/proposals.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 (Proposal)
// DB Table: Plural (proposals)
// Resource: Plural (Proposals)
// Path: Plural (/proposals)
// View Template Folder: Plural (/templates/proposals/)
// ProposalsResource is the resource for the Proposal model
type ProposalsResource struct {
buffalo.Resource
}
// List gets all Proposals. This function is mapped to the path
// GET /proposals
func (v ProposalsResource) 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"))
}
proposals := &models.Proposals{}
// 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 Proposals from the DB
if err := q.All(proposals); 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, proposals))
}
// Show gets the data for one Proposal. This function is mapped to
// the path GET /proposals/{proposal_id}
func (v ProposalsResource) 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 Proposal
proposal := &models.Proposal{}
// To find the Proposal the parameter proposal_id is used.
if err := tx.Find(proposal, c.Param("proposal_id")); err != nil {
return c.Error(404, err)
}
return c.Render(200, r.Auto(c, proposal))
}
// New renders the form for creating a new Proposal.
// This function is mapped to the path GET /proposals/new
func (v ProposalsResource) New(c buffalo.Context) error {
// return c.Render(200, r.Auto(c, &models.Proposal{}))
// }
// func NewProposal(c buffalo.Context) error {
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
speakers := models.Dutonians{}
if err := tx.Order("lastname").All(&speakers); err != nil {
return errors.WithStack(err)
}
abstracts := models.Abstracts{}
if err := tx.Order("title").All(&abstracts); err != nil {
return errors.WithStack(err)
}
events := models.Events{}
if err := tx.Order("title").All(&events); err != nil {
return errors.WithStack(err)
}
proposal := &models.Proposal{}
c.Set("proposal", proposal)
c.Set("speakers", speakers)
c.Set("abstracts", abstracts)
c.Set("events", events)
return c.Render(200, r.Auto(c, proposal))
}
// Create adds a Proposal to the DB. This function is mapped to the
// path POST /proposals
func (v ProposalsResource) 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 Proposal
proposal := &models.Proposal{
UserID: user.ID,
UpdatedBy: user.ID,
}
// Bind proposal to the html form elements
if err := c.Bind(proposal); err != nil {
return errors.WithStack(err)
}
// Validate the data from the html form
verrs, err := tx.ValidateAndCreate(proposal)
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, proposal))
}
// If there are no errors set a success message
c.Flash().Add("success", "Proposal was created successfully")
// and redirect to the proposals index page
return c.Render(201, r.Auto(c, proposal))
}
// Edit renders a edit form for a Proposal. This function is
// mapped to the path GET /proposals/{proposal_id}/edit
func (v ProposalsResource) 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 Proposal
// proposal := &models.Proposal{}
// if err := tx.Find(proposal, c.Param("proposal_id")); err != nil {
// return c.Error(404, err)
// }
// return c.Render(200, r.Auto(c, proposal))
// }
// func EditProposal(c buffalo.Context) error {
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
speakers := models.Dutonians{}
if err := tx.Order("lastname").All(&speakers); err != nil {
return errors.WithStack(err)
}
abstracts := models.Abstracts{}
if err := tx.Order("title").All(&abstracts); err != nil {
return errors.WithStack(err)
}
events := models.Events{}
if err := tx.Order("title").All(&events); err != nil {
return errors.WithStack(err)
}
proposal := &models.Proposal{}
if err := tx.Find(proposal, c.Param("proposal_id")); err != nil {
return c.Error(404, err)
}
c.Set("proposal", proposal)
c.Set("speakers", speakers)
c.Set("abstracts", abstracts)
c.Set("events", events)
return c.Render(200, r.Auto(c, proposal))
}
// Update changes a Proposal in the DB. This function is mapped to
// the path PUT /proposals/{proposal_id}
func (v ProposalsResource) 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 Proposal
proposal := &models.Proposal{
UpdatedBy: user.ID,
}
if err := tx.Find(proposal, c.Param("proposal_id")); err != nil {
return c.Error(404, err)
}
// Bind Proposal to the html form elements
if err := c.Bind(proposal); err != nil {
return errors.WithStack(err)
}
verrs, err := tx.ValidateAndUpdate(proposal)
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, proposal))
}
// If there are no errors set a success message
c.Flash().Add("success", "Proposal was updated successfully")
// and redirect to the proposals index page
return c.Render(200, r.Auto(c, proposal))
}
// Destroy deletes a Proposal from the DB. This function is mapped
// to the path DELETE /proposals/{proposal_id}
func (v ProposalsResource) 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 Proposal
proposal := &models.Proposal{}
// To find the Proposal the parameter proposal_id is used.
if err := tx.Find(proposal, c.Param("proposal_id")); err != nil {
return c.Error(404, err)
}
if err := tx.Destroy(proposal); err != nil {
return errors.WithStack(err)
}
// If there are no errors set a flash message
c.Flash().Add("success", "Proposal was destroyed successfully")
// Redirect to the proposals index page
return c.Render(200, r.Auto(c, proposal))
}