Added a basic homepage
This commit is contained in:
parent
d40640a648
commit
12fd3c04ca
113 changed files with 414 additions and 506 deletions
62
internal/handlers/contact.go
Normal file
62
internal/handlers/contact.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/camzawacki/personal-site/internal/form"
|
||||
"github.com/camzawacki/personal-site/internal/routenames"
|
||||
"github.com/camzawacki/personal-site/internal/services"
|
||||
"github.com/camzawacki/personal-site/internal/ui/forms"
|
||||
"github.com/camzawacki/personal-site/internal/ui/pages"
|
||||
)
|
||||
|
||||
type Contact struct {
|
||||
mail *services.MailClient
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(new(Contact))
|
||||
}
|
||||
|
||||
func (h *Contact) Init(c *services.Container) error {
|
||||
h.mail = c.Mail
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Contact) Routes(g *echo.Group) {
|
||||
g.GET("/contact", h.Page).Name = routenames.Contact
|
||||
g.POST("/contact", h.Submit).Name = routenames.ContactSubmit
|
||||
}
|
||||
|
||||
func (h *Contact) Page(ctx echo.Context) error {
|
||||
return pages.ContactUs(ctx, form.Get[forms.Contact](ctx))
|
||||
}
|
||||
|
||||
func (h *Contact) Submit(ctx echo.Context) error {
|
||||
var input forms.Contact
|
||||
|
||||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue