зеркало из https://github.com/Azure/spec-sla-bot.git
206 строки
5.5 KiB
Go
206 строки
5.5 KiB
Go
|
package actions
|
||
|
|
||
|
import (
|
||
|
"github.com/Azure/spec-sla-bot/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 (Email)
|
||
|
// DB Table: Plural (emails)
|
||
|
// Resource: Plural (Emails)
|
||
|
// Path: Plural (/emails)
|
||
|
// View Template Folder: Plural (/templates/emails/)
|
||
|
|
||
|
// EmailsResource is the resource for the Email model
|
||
|
type EmailsResource struct {
|
||
|
buffalo.Resource
|
||
|
}
|
||
|
|
||
|
// List gets all Emails. This function is mapped to the path
|
||
|
// GET /emails
|
||
|
func (v EmailsResource) 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"))
|
||
|
}
|
||
|
|
||
|
emails := &models.Emails{}
|
||
|
|
||
|
// 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 Emails from the DB
|
||
|
if err := q.All(emails); 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, emails))
|
||
|
}
|
||
|
|
||
|
// Show gets the data for one Email. This function is mapped to
|
||
|
// the path GET /emails/{email_id}
|
||
|
func (v EmailsResource) 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 Email
|
||
|
email := &models.Email{}
|
||
|
|
||
|
// To find the Email the parameter email_id is used.
|
||
|
if err := tx.Find(email, c.Param("email_id")); err != nil {
|
||
|
return c.Error(404, err)
|
||
|
}
|
||
|
|
||
|
return c.Render(200, r.Auto(c, email))
|
||
|
}
|
||
|
|
||
|
// New renders the form for creating a new Email.
|
||
|
// This function is mapped to the path GET /emails/new
|
||
|
func (v EmailsResource) New(c buffalo.Context) error {
|
||
|
return c.Render(200, r.Auto(c, &models.Email{}))
|
||
|
}
|
||
|
|
||
|
// Create adds a Email to the DB. This function is mapped to the
|
||
|
// path POST /emails
|
||
|
func (v EmailsResource) Create(c buffalo.Context) error {
|
||
|
// Allocate an empty Email
|
||
|
email := &models.Email{}
|
||
|
|
||
|
// Bind email to the html form elements
|
||
|
if err := c.Bind(email); err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
// Get the DB connection from the context
|
||
|
tx, ok := c.Value("tx").(*pop.Connection)
|
||
|
if !ok {
|
||
|
return errors.WithStack(errors.New("no transaction found"))
|
||
|
}
|
||
|
|
||
|
// Validate the data from the html form
|
||
|
verrs, err := tx.ValidateAndCreate(email)
|
||
|
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, email))
|
||
|
}
|
||
|
|
||
|
// If there are no errors set a success message
|
||
|
c.Flash().Add("success", "Email was created successfully")
|
||
|
|
||
|
// and redirect to the emails index page
|
||
|
return c.Render(201, r.Auto(c, email))
|
||
|
}
|
||
|
|
||
|
// Edit renders a edit form for a Email. This function is
|
||
|
// mapped to the path GET /emails/{email_id}/edit
|
||
|
func (v EmailsResource) 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 Email
|
||
|
email := &models.Email{}
|
||
|
|
||
|
if err := tx.Find(email, c.Param("email_id")); err != nil {
|
||
|
return c.Error(404, err)
|
||
|
}
|
||
|
|
||
|
return c.Render(200, r.Auto(c, email))
|
||
|
}
|
||
|
|
||
|
// Update changes a Email in the DB. This function is mapped to
|
||
|
// the path PUT /emails/{email_id}
|
||
|
func (v EmailsResource) 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"))
|
||
|
}
|
||
|
|
||
|
// Allocate an empty Email
|
||
|
email := &models.Email{}
|
||
|
|
||
|
if err := tx.Find(email, c.Param("email_id")); err != nil {
|
||
|
return c.Error(404, err)
|
||
|
}
|
||
|
|
||
|
// Bind Email to the html form elements
|
||
|
if err := c.Bind(email); err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
verrs, err := tx.ValidateAndUpdate(email)
|
||
|
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, email))
|
||
|
}
|
||
|
|
||
|
// If there are no errors set a success message
|
||
|
c.Flash().Add("success", "Email was updated successfully")
|
||
|
|
||
|
// and redirect to the emails index page
|
||
|
return c.Render(200, r.Auto(c, email))
|
||
|
}
|
||
|
|
||
|
// Destroy deletes a Email from the DB. This function is mapped
|
||
|
// to the path DELETE /emails/{email_id}
|
||
|
func (v EmailsResource) 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 Email
|
||
|
email := &models.Email{}
|
||
|
|
||
|
// To find the Email the parameter email_id is used.
|
||
|
if err := tx.Find(email, c.Param("email_id")); err != nil {
|
||
|
return c.Error(404, err)
|
||
|
}
|
||
|
|
||
|
if err := tx.Destroy(email); err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
// If there are no errors set a flash message
|
||
|
c.Flash().Add("success", "Email was destroyed successfully")
|
||
|
|
||
|
// Redirect to the emails index page
|
||
|
return c.Render(200, r.Auto(c, email))
|
||
|
}
|