saasitone/routes/contact.go

57 lines
1.2 KiB
Go
Raw Normal View History

2021-12-14 08:13:53 -08:00
package routes
2021-12-03 03:11:01 -08:00
import (
2021-12-22 18:51:18 -08:00
"goweb/context"
"goweb/controller"
2021-12-03 03:11:01 -08:00
"github.com/labstack/echo/v4"
)
2021-12-22 18:51:18 -08:00
type (
Contact struct {
controller.Controller
}
2021-12-03 03:11:01 -08:00
2021-12-22 18:51:18 -08:00
ContactForm struct {
Email string `form:"email" validate:"required,email"`
Message string `form:"message" validate:"required"`
Submission controller.FormSubmission
2021-12-22 18:51:18 -08:00
}
)
func (c *Contact) Get(ctx echo.Context) error {
2021-12-28 19:05:20 -08:00
page := controller.NewPage(ctx)
page.Layout = "main"
page.Name = "contact"
page.Title = "Contact us"
page.Form = ContactForm{}
2021-12-22 18:51:18 -08:00
if form := ctx.Get(context.FormKey); form != nil {
2021-12-28 19:05:20 -08:00
page.Form = form.(*ContactForm)
2021-12-22 18:51:18 -08:00
}
2021-12-28 19:05:20 -08:00
return c.RenderPage(ctx, page)
2021-12-03 03:11:01 -08:00
}
2021-12-22 18:51:18 -08:00
func (c *Contact) Post(ctx echo.Context) error {
var form ContactForm
2021-12-23 20:04:00 -08:00
ctx.Set(context.FormKey, &form)
// Parse the form values
2021-12-22 18:51:18 -08:00
if err := ctx.Bind(&form); err != nil {
2021-12-23 17:58:49 -08:00
return c.Fail(ctx, err, "unable to bind form")
2021-12-22 18:51:18 -08:00
}
if err := form.Submission.Process(ctx, form); err != nil {
2021-12-23 17:58:49 -08:00
return c.Fail(ctx, err, "unable to process form submission")
}
if !form.Submission.HasErrors() {
if err := c.Container.Mail.Send(ctx, form.Email, "Hello!"); err != nil {
2021-12-23 17:58:49 -08:00
return c.Fail(ctx, err, "unable to send email")
}
2021-12-22 18:51:18 -08:00
}
return c.Get(ctx)
2021-12-03 03:11:01 -08:00
}