github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/resource/_fixtures/default/actions/widgets.go.tmpl (about)

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