Move controller to the template renderer (#68)
This commit is contained in:
parent
6a7070a729
commit
5531e0bec2
26 changed files with 654 additions and 679 deletions
|
|
@ -5,8 +5,8 @@ import (
|
|||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/pkg/form"
|
||||
"github.com/mikestefanello/pagoda/pkg/page"
|
||||
"github.com/mikestefanello/pagoda/pkg/services"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
)
|
||||
|
|
@ -19,7 +19,7 @@ const (
|
|||
type (
|
||||
Contact struct {
|
||||
mail *services.MailClient
|
||||
controller.Controller
|
||||
*services.TemplateRenderer
|
||||
}
|
||||
|
||||
contactForm struct {
|
||||
|
|
@ -34,28 +34,28 @@ func init() {
|
|||
Register(new(Contact))
|
||||
}
|
||||
|
||||
func (c *Contact) Init(ct *services.Container) error {
|
||||
c.Controller = controller.NewController(ct)
|
||||
c.mail = ct.Mail
|
||||
func (h *Contact) Init(c *services.Container) error {
|
||||
h.TemplateRenderer = c.TemplateRenderer
|
||||
h.mail = c.Mail
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Contact) Routes(g *echo.Group) {
|
||||
g.GET("/contact", c.Page).Name = routeNameContact
|
||||
g.POST("/contact", c.Submit).Name = routeNameContactSubmit
|
||||
func (h *Contact) Routes(g *echo.Group) {
|
||||
g.GET("/contact", h.Page).Name = routeNameContact
|
||||
g.POST("/contact", h.Submit).Name = routeNameContactSubmit
|
||||
}
|
||||
|
||||
func (c *Contact) Page(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = templates.LayoutMain
|
||||
page.Name = templates.PageContact
|
||||
page.Title = "Contact us"
|
||||
page.Form = form.Get[contactForm](ctx)
|
||||
func (h *Contact) Page(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Layout = templates.LayoutMain
|
||||
p.Name = templates.PageContact
|
||||
p.Title = "Contact us"
|
||||
p.Form = form.Get[contactForm](ctx)
|
||||
|
||||
return c.RenderPage(ctx, page)
|
||||
return h.RenderPage(ctx, p)
|
||||
}
|
||||
|
||||
func (c *Contact) Submit(ctx echo.Context) error {
|
||||
func (h *Contact) Submit(ctx echo.Context) error {
|
||||
var input contactForm
|
||||
|
||||
err := form.Submit(ctx, &input)
|
||||
|
|
@ -63,12 +63,12 @@ func (c *Contact) Submit(ctx echo.Context) error {
|
|||
switch err.(type) {
|
||||
case nil:
|
||||
case validator.ValidationErrors:
|
||||
return c.Page(ctx)
|
||||
return h.Page(ctx)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.mail.
|
||||
err = h.mail.
|
||||
Compose().
|
||||
To(input.Email).
|
||||
Subject("Contact form submitted").
|
||||
|
|
@ -76,8 +76,8 @@ func (c *Contact) Submit(ctx echo.Context) error {
|
|||
Send(ctx)
|
||||
|
||||
if err != nil {
|
||||
return c.Fail(err, "unable to send email")
|
||||
return fail(err, "unable to send email")
|
||||
}
|
||||
|
||||
return c.Page(ctx)
|
||||
return h.Page(ctx)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue