wip: added contact form handling
This commit is contained in:
44
internal/controllers/contactController.go
Normal file
44
internal/controllers/contactController.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"GoMembership/internal/services"
|
||||
"GoMembership/pkg/logger"
|
||||
)
|
||||
|
||||
type ContactController struct {
|
||||
EmailService *services.EmailService
|
||||
}
|
||||
type contactData struct {
|
||||
email string `validate:"required,email"`
|
||||
name string
|
||||
message string `validate:"required"`
|
||||
}
|
||||
|
||||
func (cc *ContactController) RelayContactRequest(c *gin.Context) {
|
||||
var msgData contactData
|
||||
if c.Query("username") != "" {
|
||||
// A bot is talking to us
|
||||
return
|
||||
}
|
||||
msgData.name = c.Query("name")
|
||||
msgData.email = c.Query("email")
|
||||
msgData.message = c.Query("message")
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(msgData); err != nil {
|
||||
logger.Error.Printf("Couldn't validate contact form data: %v", err)
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{"error": "Couldn't validate contact form data"})
|
||||
}
|
||||
|
||||
if err := cc.EmailService.RelayContactFormMessage(&msgData.email, &msgData.name, &msgData.message); err != nil {
|
||||
logger.Error.Printf("Couldn't send contact message mail: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't send mail"})
|
||||
}
|
||||
c.JSON(http.StatusOK, "Your message has been sent")
|
||||
}
|
||||
Reference in New Issue
Block a user