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 == "") +}