add: contactController,tests & refactored tests
This commit is contained in:
@@ -15,30 +15,33 @@ type ContactController struct {
|
||||
EmailService *services.EmailService
|
||||
}
|
||||
type contactData struct {
|
||||
email string `validate:"required,email"`
|
||||
name string
|
||||
message string `validate:"required"`
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
Name string `form:"name"`
|
||||
Message string `form:"message" validate:"required"`
|
||||
Honeypot string `form:"username" validate:"eq="`
|
||||
}
|
||||
|
||||
func (cc *ContactController) RelayContactRequest(c *gin.Context) {
|
||||
var msgData contactData
|
||||
if c.Query("username") != "" {
|
||||
|
||||
if err := c.ShouldBind(&msgData); err != nil {
|
||||
// A bot is talking to us
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{"error": "Not Acceptable"})
|
||||
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"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := cc.EmailService.RelayContactFormMessage(&msgData.email, &msgData.name, &msgData.message); err != nil {
|
||||
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"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, "Your message has been sent")
|
||||
|
||||
c.JSON(http.StatusAccepted, "Your message has been sent")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user