From b6428b033ca521e4cb46e4ccc8458261c2e77158 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Wed, 6 Oct 2021 11:50:19 +0300 Subject: [PATCH] feat: add message pass to 400 html template --- controllers/threads_controller.go | 23 ++++++++++++++++------- repositories/structs.go | 11 +++++++++++ templates/400.html | 2 +- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 6141706..d2a7938 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -105,8 +105,10 @@ func CreateThread(c *gin.Context) { captchaString := form.Value["captcha"][0] isCaptchaValid := captcha.VerifyString(captchaID, captchaString) if !isCaptchaValid { - log.Println("error:", err) - c.HTML(http.StatusInternalServerError, "400.html", nil) + errorHtmlData := Repositories.BadRequestHtmlData{ + Message: Repositories.InvalidCaptchaErrorMessage, + } + c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) return } @@ -115,7 +117,10 @@ func CreateThread(c *gin.Context) { title := form.Value["title"][0] isPostValid := Utils.ValidatePost(title, text) if !isPostValid { - c.HTML(http.StatusBadRequest, "400.html", nil) + errorHtmlData := Repositories.BadRequestHtmlData{ + Message: Repositories.InvalidTitleOrTextErrorMessage, + } + c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) return } @@ -225,17 +230,21 @@ func UpdateThread(c *gin.Context) { captchaString := form.Value["captcha"][0] isCaptchaValid := captcha.VerifyString(captchaID, captchaString) if !isCaptchaValid { - log.Println("error:", err) - c.HTML(http.StatusInternalServerError, "400.html", nil) + errorHtmlData := Repositories.BadRequestHtmlData{ + Message: Repositories.InvalidCaptchaErrorMessage, + } + c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) return } // TODO: dat shit crashes if no fields in request text := form.Value["text"][0] - // title := form.Value["title"][0] isPostValid := Utils.ValidatePost("", text) if !isPostValid { - c.HTML(http.StatusBadRequest, "400.html", nil) + errorHtmlData := Repositories.BadRequestHtmlData{ + Message: Repositories.InvalidTitleOrTextErrorMessage, + } + c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) return } diff --git a/repositories/structs.go b/repositories/structs.go index cbfa837..b3c5e4e 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -32,19 +32,30 @@ type File struct { // HTML Templates Structs // HTML Templates Structs +// post-form.html type HtmlFormData struct { FirstPostID int CaptchaID string } +// thread.html type GetThreadHtmlData struct { Thread []Post FormData HtmlFormData } +// index.html type GetThreadsHtmlData struct { Threads []Post PagesCount int Page int FormData HtmlFormData } + +// 400.html +type BadRequestHtmlData struct { + Message string +} + +var InvalidTitleOrTextErrorMessage = "TITLE OR TEXT SHOULD NOT BE EMPTY" +var InvalidCaptchaErrorMessage = "INVALID CAPTCHA" diff --git a/templates/400.html b/templates/400.html index 505dacf..9f0749d 100644 --- a/templates/400.html +++ b/templates/400.html @@ -12,7 +12,7 @@

400

-

TITLE OR TEXT SHOULD NOT BE EMPTY

+

{{ .Message }}