From f24c97f5e0e12065e74f2ffd63c39a7f32e8a0de Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Sat, 11 Sep 2021 16:56:22 +0300 Subject: [PATCH] feat: add simple post validation --- controllers/threads_controller.go | 14 ++++++++++++-- templates/400.html | 26 ++++++++++++++++++++++++++ utils/utils.go | 4 ++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 templates/400.html diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index be922e2..463d1ba 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -86,9 +86,14 @@ func CreateThread(c *gin.Context) { } // TODO: dat shit crashes if no fields in request - // TODO: add validation (title or text) text := form.Value["text"][0] title := form.Value["title"][0] + isPostValid := Utils.ValidatePost(title, text) + if !isPostValid { + c.HTML(http.StatusBadRequest, "400.html", nil) + return + } + filesInRequest := form.File["files"] conn, err := Db.Pool.Acquire(context.TODO()) @@ -177,9 +182,14 @@ func UpdateThread(c *gin.Context) { } // TODO: dat shit crashes if no fields in request - // TODO: add validation (title or text) text := form.Value["text"][0] title := form.Value["title"][0] + isPostValid := Utils.ValidatePost(title, text) + if !isPostValid { + c.HTML(http.StatusBadRequest, "400.html", nil) + return + } + filesInRequest := form.File["files"] isSageField := form.Value["sage"] var isSageString string diff --git a/templates/400.html b/templates/400.html new file mode 100644 index 0000000..c762d54 --- /dev/null +++ b/templates/400.html @@ -0,0 +1,26 @@ + + + {{ template "static" }} + + + {{ template "static-meta" }} + + + +
+

400

+

TITLE OR TEXT SHOULD NOT BE EMPTY

+ + Bad request + +
+ + + \ No newline at end of file diff --git a/utils/utils.go b/utils/utils.go index a308011..96cd941 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -46,3 +46,7 @@ func CreateThreadFolder(postID int) error { return nil } + +func ValidatePost(title, text string) bool { + return (title == "" && text != "") || (title != "" && text == "") +}