package handlers import ( "fmt" "github.com/go-playground/validator/v10" "github.com/labstack/echo/v4" "git.grosinger.net/tgrosinger/saasitone/pkg/form" "git.grosinger.net/tgrosinger/saasitone/pkg/page" "git.grosinger.net/tgrosinger/saasitone/pkg/services" "git.grosinger.net/tgrosinger/saasitone/templ/layouts" "git.grosinger.net/tgrosinger/saasitone/templ/pages" ) const ( routeNameContact = "contact" routeNameContactSubmit = "contact.submit" ) type ( Contact struct { mail *services.MailClient *services.TemplateRenderer } ) func init() { Register(new(Contact)) } func (h *Contact) Init(c *services.Container) error { h.TemplateRenderer = c.TemplateRenderer h.mail = c.Mail return nil } func (h *Contact) Routes(g *echo.Group) { g.GET("/contact", h.Page).Name = routeNameContact g.POST("/contact", h.Submit).Name = routeNameContactSubmit } func (h *Contact) Page(ctx echo.Context) error { p := page.New(ctx) p.Title = "Contact us" p.LayoutComponent = layouts.Main f := form.Get[pages.ContactForm](ctx) component := pages.Contact(p, f) return h.RenderPageTempl(ctx, p, component) } func (h *Contact) Submit(ctx echo.Context) error { var input pages.ContactForm err := form.Submit(ctx, &input) switch err.(type) { case nil: case validator.ValidationErrors: return h.Page(ctx) default: return err } err = h.mail. Compose(). To(input.Email). Subject("Contact form submitted"). Body(fmt.Sprintf("The message is: %s", input.Message)). Send(ctx) if err != nil { return fail(err, "unable to send email") } return h.Page(ctx) }