From 25d91e574298175d9ffa47a88ba1ba157ee8368a Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Sun, 23 Jan 2022 19:32:02 +0200 Subject: [PATCH] feat: add thread is archived check on posting --- controllers/threads_controller.go | 23 +++++++++++++++++++---- repositories/posts_repository.go | 18 ++++++++++++++++++ repositories/structs.go | 1 + 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 9604907..e0f7b5f 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -111,7 +111,7 @@ func CreateThread(c *gin.Context) { errorHtmlData := Repositories.BadRequestHtmlData{ Message: validationErrorMessage, } - c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) + c.HTML(http.StatusBadRequest, "400.html", errorHtmlData) return } @@ -122,7 +122,7 @@ func CreateThread(c *gin.Context) { errorHtmlData := Repositories.BadRequestHtmlData{ Message: Repositories.InvalidCaptchaErrorMessage, } - c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) + c.HTML(http.StatusBadRequest, "400.html", errorHtmlData) return } @@ -233,6 +233,7 @@ func CreateThread(c *gin.Context) { c.Redirect(http.StatusFound, "/"+strconv.Itoa(threadID)) } +// Add new post in thread func UpdateThread(c *gin.Context) { threadIDString := c.Param("threadID") threadID, err := strconv.Atoi(threadIDString) @@ -241,6 +242,20 @@ func UpdateThread(c *gin.Context) { return } + isArchived, err := Repositories.Posts.GetIfThreadIsArchived(threadID) + if isArchived { + errorHtmlData := Repositories.BadRequestHtmlData{ + Message: Repositories.ThreadIsArchivedErrorMessage, + } + c.HTML(http.StatusBadRequest, "400.html", errorHtmlData) + return + } + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + form, err := c.MultipartForm() if err != nil { log.Println("error:", err) @@ -256,7 +271,7 @@ func UpdateThread(c *gin.Context) { errorHtmlData := Repositories.BadRequestHtmlData{ Message: validationErrorMessage, } - c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) + c.HTML(http.StatusBadRequest, "400.html", errorHtmlData) return } @@ -267,7 +282,7 @@ func UpdateThread(c *gin.Context) { errorHtmlData := Repositories.BadRequestHtmlData{ Message: Repositories.InvalidCaptchaErrorMessage, } - c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) + c.HTML(http.StatusBadRequest, "400.html", errorHtmlData) return } diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index 2817c3c..8067740 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -166,6 +166,24 @@ func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) { return posts, nil } +// Check if thread is archived +func (r *PostsRepository) GetIfThreadIsArchived(ID int) (bool, error) { + sql := ` + SELECT is_archived + FROM posts + WHERE id = $1 + LIMIT 1 + ` + + row := Db.Pool.QueryRow(context.TODO(), sql, ID) + var isArchived bool + err := row.Scan(&isArchived) + if err != nil { + return false, err + } + return isArchived, nil +} + func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) { sql := ` INSERT INTO posts (is_parent, parent_id, title, text, is_sage, updated_at) diff --git a/repositories/structs.go b/repositories/structs.go index 6092278..51c963c 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -64,3 +64,4 @@ const InvalidTextLengthErrorMessage = "TEXT SHOULD NOT EXCEED 1000 CHARS" const InvalidFilesLengthErrorMessage = "MAXIMUM 4 FILES CAN BE UPLOADED" const InvalidFileSizeErrorMessage = "FILE SIZE EXCIDED (3MB PER FILE)" const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG" +const ThreadIsArchivedErrorMessage = "THREAD IS ARCHIVED"