github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/resource/templates/use_model/actions/resource-name.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  	"{{.opts.App.ModelsPkg}}"
    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 ({{.opts.Model.Proper}})
    21  // DB Table: Plural ({{.opts.Model.Tableize}})
    22  // Resource: Plural ({{.opts.Name.Resource}})
    23  // Path: Plural (/{{.opts.Name.URL}})
    24  // View Template Folder: Plural (/templates/{{.opts.Name.File.Pluralize}}/)
    25  
    26  // {{.opts.Name.Resource}}Resource is the resource for the {{.opts.Model.Proper}} model
    27  type {{.opts.Name.Resource}}Resource struct {
    28  	buffalo.Resource
    29  }
    30  
    31  // List gets all {{.opts.Model.Group}}. This function is mapped to the path
    32  // GET /{{.opts.Name.URL}}
    33  func (v {{.opts.Name.Resource}}Resource) 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  	{{.opts.Model.VarCasePlural}} := &models.{{.opts.Model.Group}}{}
    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 {{.opts.Model.Group}} from the DB
    47  	if err := q.All({{.opts.Model.VarCasePlural}}); 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("{{.opts.Model.VarCasePlural}}", {{.opts.Model.VarCasePlural}})
    56  		return c.Render(http.StatusOK, r.HTML("{{.opts.Name.File.Pluralize}}/index.plush.html"))
    57  	}).Wants("json", func(c buffalo.Context) error {
    58  		return c.Render(200, r.JSON({{.opts.Model.VarCasePlural}}))
    59  	}).Wants("xml", func(c buffalo.Context) error {
    60  		return c.Render(200, r.XML({{.opts.Model.VarCasePlural}}))
    61  	}).Respond(c)
    62  }
    63  
    64  // Show gets the data for one {{.opts.Model.Proper}}. This function is mapped to
    65  // the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
    66  func (v {{.opts.Name.Resource}}Resource) 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 {{.opts.Model.Proper}}
    74  	{{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{}
    75  
    76  	// To find the {{.opts.Model.Proper}} the parameter {{.opts.Name.ParamID}} is used.
    77  	if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
    78  		return c.Error(http.StatusNotFound, err)
    79  	}
    80  
    81  	return responder.Wants("html", func(c buffalo.Context) error {
    82  		c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}})
    83  
    84  		return c.Render(http.StatusOK, r.HTML("{{.opts.Name.File.Pluralize}}/show.plush.html"))
    85  	}).Wants("json", func(c buffalo.Context) error {
    86  		return c.Render(200, r.JSON({{.opts.Model.VarCaseSingle}}))
    87  	}).Wants("xml", func(c buffalo.Context) error {
    88  		return c.Render(200, r.XML({{.opts.Model.VarCaseSingle}}))
    89  	}).Respond(c)
    90  }
    91  
    92  {{ if .opts.App.AsWeb -}}
    93  // New renders the form for creating a new {{.opts.Model.Proper}}.
    94  // This function is mapped to the path GET /{{.opts.Name.URL}}/new
    95  func (v {{.opts.Name.Resource}}Resource) New(c buffalo.Context) error {
    96  	c.Set("{{.opts.Model.VarCaseSingle}}", &models.{{.opts.Model.Proper}}{})
    97  
    98  	return c.Render(http.StatusOK, r.HTML("{{.opts.Name.File.Pluralize}}/new.plush.html"))
    99  }
   100  {{ end -}}
   101  
   102  // Create adds a {{.opts.Model.Proper}} to the DB. This function is mapped to the
   103  // path POST /{{.opts.Name.URL}}
   104  func (v {{.opts.Name.Resource}}Resource) Create(c buffalo.Context) error {
   105  	// Allocate an empty {{.opts.Model.Proper}}
   106  	{{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{}
   107  
   108  	// Bind {{.opts.Model.VarCaseSingle}} to the html form elements
   109  	if err := c.Bind({{.opts.Model.VarCaseSingle}}); err != nil {
   110  		return err
   111  	}
   112  
   113  	// Get the DB connection from the context
   114  	tx, ok := c.Value("tx").(*pop.Connection)
   115  	if !ok {
   116  		return fmt.Errorf("no transaction found")
   117  	}
   118  
   119  	// Validate the data from the html form
   120  	verrs, err := tx.ValidateAndCreate({{.opts.Model.VarCaseSingle}})
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	if verrs.HasAny() {
   126  		return responder.Wants("html", func(c buffalo.Context) error {
   127  			// Make the errors available inside the html template
   128  			c.Set("errors", verrs)
   129  
   130  			// Render again the new.html template that the user can
   131  			// correct the input.
   132  			c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}})
   133  
   134  			return c.Render(http.StatusUnprocessableEntity, r.HTML("{{.opts.Name.File.Pluralize}}/new.plush.html"))
   135  		}).Wants("json", func(c buffalo.Context) error {
   136  			return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
   137  		}).Wants("xml", func(c buffalo.Context) error {
   138  			return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
   139  		}).Respond(c)
   140  	}
   141  
   142  	return responder.Wants("html", func(c buffalo.Context) error {
   143  		// If there are no errors set a success message
   144  		c.Flash().Add("success", T.Translate(c, "{{.opts.Model.VarCaseSingle}}.created.success"))
   145  
   146  		// and redirect to the show page
   147  		return c.Redirect(http.StatusSeeOther, "/{{.opts.Name.URL}}/%v", {{.opts.Model.VarCaseSingle}}.ID)
   148  	}).Wants("json", func(c buffalo.Context) error {
   149  		return c.Render(http.StatusCreated, r.JSON({{.opts.Model.VarCaseSingle}}))
   150  	}).Wants("xml", func(c buffalo.Context) error {
   151  		return c.Render(http.StatusCreated, r.XML({{.opts.Model.VarCaseSingle}}))
   152  	}).Respond(c)
   153  }
   154  
   155  {{ if .opts.App.AsWeb -}}
   156  // Edit renders a edit form for a {{.opts.Model.Proper}}. This function is
   157  // mapped to the path GET /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}/edit
   158  func (v {{.opts.Name.Resource}}Resource) Edit(c buffalo.Context) error {
   159  	// Get the DB connection from the context
   160  	tx, ok := c.Value("tx").(*pop.Connection)
   161  	if !ok {
   162  		return fmt.Errorf("no transaction found")
   163  	}
   164  
   165  	// Allocate an empty {{.opts.Model.Proper}}
   166  	{{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{}
   167  
   168  	if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
   169  		return c.Error(http.StatusNotFound, err)
   170  	}
   171  
   172  	c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}})
   173  	return c.Render(http.StatusOK, r.HTML("{{.opts.Name.File.Pluralize}}/edit.plush.html"))
   174  }
   175  {{ end -}}
   176  
   177  // Update changes a {{.opts.Model.Proper}} in the DB. This function is mapped to
   178  // the path PUT /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
   179  func (v {{.opts.Name.Resource}}Resource) Update(c buffalo.Context) error {
   180  	// Get the DB connection from the context
   181  	tx, ok := c.Value("tx").(*pop.Connection)
   182  	if !ok {
   183  		return fmt.Errorf("no transaction found")
   184  	}
   185  
   186  	// Allocate an empty {{.opts.Model.Proper}}
   187  	{{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{}
   188  
   189  	if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
   190  		return c.Error(http.StatusNotFound, err)
   191  	}
   192  
   193  	// Bind {{.opts.Model.Proper}} to the html form elements
   194  	if err := c.Bind({{.opts.Model.VarCaseSingle}}); err != nil {
   195  		return err
   196  	}
   197  
   198  	verrs, err := tx.ValidateAndUpdate({{.opts.Model.VarCaseSingle}})
   199  	if err != nil {
   200  		return err
   201  	}
   202  
   203  	if verrs.HasAny() {
   204  		return responder.Wants("html", func(c buffalo.Context) error {
   205  			// Make the errors available inside the html template
   206  			c.Set("errors", verrs)
   207  
   208  			// Render again the edit.html template that the user can
   209  			// correct the input.
   210  			c.Set("{{.opts.Model.VarCaseSingle}}", {{.opts.Model.VarCaseSingle}})
   211  
   212  			return c.Render(http.StatusUnprocessableEntity, r.HTML("{{.opts.Name.File.Pluralize}}/edit.plush.html"))
   213  		}).Wants("json", func(c buffalo.Context) error {
   214  			return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
   215  		}).Wants("xml", func(c buffalo.Context) error {
   216  			return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
   217  		}).Respond(c)
   218  	}
   219  
   220  	return responder.Wants("html", func(c buffalo.Context) error {
   221  		// If there are no errors set a success message
   222  		c.Flash().Add("success", T.Translate(c, "{{.opts.Model.VarCaseSingle}}.updated.success"))
   223  
   224  		// and redirect to the show page
   225  		return c.Redirect(http.StatusSeeOther, "/{{.opts.Name.URL}}/%v", {{.opts.Model.VarCaseSingle}}.ID)
   226  	}).Wants("json", func(c buffalo.Context) error {
   227  		return c.Render(http.StatusOK, r.JSON({{.opts.Model.VarCaseSingle}}))
   228  	}).Wants("xml", func(c buffalo.Context) error {
   229  		return c.Render(http.StatusOK, r.XML({{.opts.Model.VarCaseSingle}}))
   230  	}).Respond(c)
   231  }
   232  
   233  // Destroy deletes a {{.opts.Model.Proper}} from the DB. This function is mapped
   234  // to the path DELETE /{{.opts.Name.URL}}/{{"{"}}{{.opts.Name.ParamID}}}
   235  func (v {{.opts.Name.Resource}}Resource) Destroy(c buffalo.Context) error {
   236  	// Get the DB connection from the context
   237  	tx, ok := c.Value("tx").(*pop.Connection)
   238  	if !ok {
   239  		return fmt.Errorf("no transaction found")
   240  	}
   241  
   242  	// Allocate an empty {{.opts.Model.Proper}}
   243  	{{.opts.Model.VarCaseSingle}} := &models.{{.opts.Model.Proper}}{}
   244  
   245  	// To find the {{.opts.Model.Proper}} the parameter {{.opts.Name.ParamID}} is used.
   246  	if err := tx.Find({{.opts.Model.VarCaseSingle}}, c.Param("{{.opts.Name.ParamID}}")); err != nil {
   247  		return c.Error(http.StatusNotFound, err)
   248  	}
   249  
   250  	if err := tx.Destroy({{.opts.Model.VarCaseSingle}}); err != nil {
   251  		return err
   252  	}
   253  
   254  	return responder.Wants("html", func(c buffalo.Context) error {
   255  		// If there are no errors set a flash message
   256  		c.Flash().Add("success", T.Translate(c, "{{.opts.Model.VarCaseSingle}}.destroyed.success"))
   257  
   258  		// Redirect to the index page
   259  		return c.Redirect(http.StatusSeeOther, "/{{.opts.Name.URL}}")
   260  	}).Wants("json", func(c buffalo.Context) error {
   261  		return c.Render(http.StatusOK, r.JSON({{.opts.Model.VarCaseSingle}}))
   262  	}).Wants("xml", func(c buffalo.Context) error {
   263  		return c.Render(http.StatusOK, r.XML({{.opts.Model.VarCaseSingle}}))
   264  	}).Respond(c)
   265  }