github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/resource/_fixtures/default/actions/widgets.go.tmpl (about)

     1  package actions
     2  
     3  import (
     4  
     5    "fmt"
     6    "net/http"
     7    "github.com/gobuffalo/buffalo"
     8    "github.com/gobuffalo/pop/v5"
     9    "github.com/gobuffalo/x/responder"
    10    "github.com/markbates/coke/models"
    11  )
    12  
    13  // This file is generated by Buffalo. It offers a basic structure for
    14  // adding, editing and deleting a page. If your model is more
    15  // complex or you need more than the basic implementation you need to
    16  // edit this file.
    17  
    18  // Following naming logic is implemented in Buffalo:
    19  // Model: Singular (Widget)
    20  // DB Table: Plural (widgets)
    21  // Resource: Plural (Widgets)
    22  // Path: Plural (/widgets)
    23  // View Template Folder: Plural (/templates/widgets/)
    24  
    25  // WidgetsResource is the resource for the Widget model
    26  type WidgetsResource struct{
    27    buffalo.Resource
    28  }
    29  
    30  // List gets all Widgets. This function is mapped to the path
    31  // GET /widgets
    32  func (v WidgetsResource) List(c buffalo.Context) error {
    33    // Get the DB connection from the context
    34    tx, ok := c.Value("tx").(*pop.Connection)
    35    if !ok {
    36      return fmt.Errorf("no transaction found")
    37    }
    38  
    39    widgets := &models.Widgets{}
    40  
    41    // Paginate results. Params "page" and "per_page" control pagination.
    42    // Default values are "page=1" and "per_page=20".
    43    q := tx.PaginateFromParams(c.Params())
    44  
    45    // Retrieve all Widgets from the DB
    46    if err := q.All(widgets); err != nil {
    47      return err
    48    }
    49  
    50    return responder.Wants("html", func (c buffalo.Context) error {
    51      // Add the paginator to the context so it can be used in the template.
    52      c.Set("pagination", q.Paginator)
    53  
    54      c.Set("widgets", widgets)
    55      return c.Render(http.StatusOK, r.HTML("/widgets/index.plush.html"))
    56    }).Wants("json", func (c buffalo.Context) error {
    57      return c.Render(200, r.JSON(widgets))
    58    }).Wants("xml", func (c buffalo.Context) error {
    59      return c.Render(200, r.XML(widgets))
    60    }).Respond(c)
    61  }
    62  
    63  // Show gets the data for one Widget. This function is mapped to
    64  // the path GET /widgets/{widget_id}
    65  func (v WidgetsResource) Show(c buffalo.Context) error {
    66    // Get the DB connection from the context
    67    tx, ok := c.Value("tx").(*pop.Connection)
    68    if !ok {
    69      return fmt.Errorf("no transaction found")
    70    }
    71  
    72    // Allocate an empty Widget
    73    widget := &models.Widget{}
    74  
    75    // To find the Widget the parameter widget_id is used.
    76    if err := tx.Find(widget, c.Param("widget_id")); err != nil {
    77      return c.Error(http.StatusNotFound, err)
    78    }
    79  
    80    return responder.Wants("html", func (c buffalo.Context) error {
    81      c.Set("widget", widget)
    82  
    83      return c.Render(http.StatusOK, r.HTML("/widgets/show.plush.html"))
    84    }).Wants("json", func (c buffalo.Context) error {
    85      return c.Render(200, r.JSON(widget))
    86    }).Wants("xml", func (c buffalo.Context) error {
    87      return c.Render(200, r.XML(widget))
    88    }).Respond(c)
    89  }
    90  
    91  // New renders the form for creating a new Widget.
    92  // This function is mapped to the path GET /widgets/new
    93  func (v WidgetsResource) New(c buffalo.Context) error {
    94    c.Set("widget", &models.Widget{})
    95  
    96    return c.Render(http.StatusOK, r.HTML("/widgets/new.plush.html"))
    97  }
    98  
    99  // Create adds a Widget to the DB. This function is mapped to the
   100  // path POST /widgets
   101  func (v WidgetsResource) Create(c buffalo.Context) error {
   102    // Allocate an empty Widget
   103    widget := &models.Widget{}
   104  
   105    // Bind widget to the html form elements
   106    if err := c.Bind(widget); err != nil {
   107      return err
   108    }
   109  
   110    // Get the DB connection from the context
   111    tx, ok := c.Value("tx").(*pop.Connection)
   112    if !ok {
   113      return fmt.Errorf("no transaction found")
   114    }
   115  
   116    // Validate the data from the html form
   117    verrs, err := tx.ValidateAndCreate(widget)
   118    if err != nil {
   119      return err
   120    }
   121  
   122    if verrs.HasAny() {
   123      return responder.Wants("html", func (c buffalo.Context) error {
   124        // Make the errors available inside the html template
   125        c.Set("errors", verrs)
   126  
   127        // Render again the new.html template that the user can
   128        // correct the input.
   129        c.Set("widget", widget)
   130  
   131        return c.Render(http.StatusUnprocessableEntity, r.HTML("/widgets/new.plush.html"))
   132      }).Wants("json", func (c buffalo.Context) error {
   133        return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
   134      }).Wants("xml", func (c buffalo.Context) error {
   135        return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
   136      }).Respond(c)
   137    }
   138  
   139    return responder.Wants("html", func (c buffalo.Context) error {
   140      // If there are no errors set a success message
   141      c.Flash().Add("success", T.Translate(c, "widget.created.success"))
   142  
   143      // and redirect to the show page
   144      return c.Redirect(http.StatusSeeOther, "/widgets/%v", widget.ID)
   145    }).Wants("json", func (c buffalo.Context) error {
   146      return c.Render(http.StatusCreated, r.JSON(widget))
   147    }).Wants("xml", func (c buffalo.Context) error {
   148      return c.Render(http.StatusCreated, r.XML(widget))
   149    }).Respond(c)
   150  }
   151  
   152  // Edit renders a edit form for a Widget. This function is
   153  // mapped to the path GET /widgets/{widget_id}/edit
   154  func (v WidgetsResource) Edit(c buffalo.Context) error {
   155    // Get the DB connection from the context
   156    tx, ok := c.Value("tx").(*pop.Connection)
   157    if !ok {
   158      return fmt.Errorf("no transaction found")
   159    }
   160  
   161    // Allocate an empty Widget
   162    widget := &models.Widget{}
   163  
   164    if err := tx.Find(widget, c.Param("widget_id")); err != nil {
   165      return c.Error(http.StatusNotFound, err)
   166    }
   167  
   168    c.Set("widget", widget)
   169    return c.Render(http.StatusOK, r.HTML("/widgets/edit.plush.html"))
   170  }
   171  
   172  // Update changes a Widget in the DB. This function is mapped to
   173  // the path PUT /widgets/{widget_id}
   174  func (v WidgetsResource) Update(c buffalo.Context) error {
   175    // Get the DB connection from the context
   176    tx, ok := c.Value("tx").(*pop.Connection)
   177    if !ok {
   178      return fmt.Errorf("no transaction found")
   179    }
   180  
   181    // Allocate an empty Widget
   182    widget := &models.Widget{}
   183  
   184    if err := tx.Find(widget, c.Param("widget_id")); err != nil {
   185      return c.Error(http.StatusNotFound, err)
   186    }
   187  
   188    // Bind Widget to the html form elements
   189    if err := c.Bind(widget); err != nil {
   190      return err
   191    }
   192  
   193    verrs, err := tx.ValidateAndUpdate(widget)
   194    if err != nil {
   195      return err
   196    }
   197  
   198    if verrs.HasAny() {
   199      return responder.Wants("html", func (c buffalo.Context) error {
   200        // Make the errors available inside the html template
   201        c.Set("errors", verrs)
   202  
   203        // Render again the edit.html template that the user can
   204        // correct the input.
   205        c.Set("widget", widget)
   206  
   207        return c.Render(http.StatusUnprocessableEntity, r.HTML("/widgets/edit.plush.html"))
   208      }).Wants("json", func (c buffalo.Context) error {
   209        return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
   210      }).Wants("xml", func (c buffalo.Context) error {
   211        return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
   212      }).Respond(c)
   213    }
   214  
   215    return responder.Wants("html", func (c buffalo.Context) error {
   216      // If there are no errors set a success message
   217      c.Flash().Add("success", T.Translate(c, "widget.updated.success"))
   218  
   219      // and redirect to the show page
   220      return c.Redirect(http.StatusSeeOther, "/widgets/%v", widget.ID)
   221    }).Wants("json", func (c buffalo.Context) error {
   222      return c.Render(http.StatusOK, r.JSON(widget))
   223    }).Wants("xml", func (c buffalo.Context) error {
   224      return c.Render(http.StatusOK, r.XML(widget))
   225    }).Respond(c)
   226  }
   227  
   228  // Destroy deletes a Widget from the DB. This function is mapped
   229  // to the path DELETE /widgets/{widget_id}
   230  func (v WidgetsResource) Destroy(c buffalo.Context) error {
   231    // Get the DB connection from the context
   232    tx, ok := c.Value("tx").(*pop.Connection)
   233    if !ok {
   234      return fmt.Errorf("no transaction found")
   235    }
   236  
   237    // Allocate an empty Widget
   238    widget := &models.Widget{}
   239  
   240    // To find the Widget the parameter widget_id is used.
   241    if err := tx.Find(widget, c.Param("widget_id")); err != nil {
   242      return c.Error(http.StatusNotFound, err)
   243    }
   244  
   245    if err := tx.Destroy(widget); err != nil {
   246      return err
   247    }
   248  
   249    return responder.Wants("html", func (c buffalo.Context) error {
   250      // If there are no errors set a flash message
   251      c.Flash().Add("success", T.Translate(c, "widget.destroyed.success"))
   252  
   253      // Redirect to the index page
   254      return c.Redirect(http.StatusSeeOther, "/widgets")
   255    }).Wants("json", func (c buffalo.Context) error {
   256      return c.Render(http.StatusOK, r.JSON(widget))
   257    }).Wants("xml", func (c buffalo.Context) error {
   258      return c.Render(http.StatusOK, r.XML(widget))
   259    }).Respond(c)
   260  }