feat: add thread is archived check on posting

This commit is contained in:
Yanislav Igonin 2022-01-23 19:32:02 +02:00
parent cb4eaa9fc2
commit 25d91e5742
3 changed files with 38 additions and 4 deletions

View File

@ -111,7 +111,7 @@ func CreateThread(c *gin.Context) {
errorHtmlData := Repositories.BadRequestHtmlData{ errorHtmlData := Repositories.BadRequestHtmlData{
Message: validationErrorMessage, Message: validationErrorMessage,
} }
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
return return
} }
@ -122,7 +122,7 @@ func CreateThread(c *gin.Context) {
errorHtmlData := Repositories.BadRequestHtmlData{ errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidCaptchaErrorMessage, Message: Repositories.InvalidCaptchaErrorMessage,
} }
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
return return
} }
@ -233,6 +233,7 @@ func CreateThread(c *gin.Context) {
c.Redirect(http.StatusFound, "/"+strconv.Itoa(threadID)) c.Redirect(http.StatusFound, "/"+strconv.Itoa(threadID))
} }
// Add new post in thread
func UpdateThread(c *gin.Context) { func UpdateThread(c *gin.Context) {
threadIDString := c.Param("threadID") threadIDString := c.Param("threadID")
threadID, err := strconv.Atoi(threadIDString) threadID, err := strconv.Atoi(threadIDString)
@ -241,6 +242,20 @@ func UpdateThread(c *gin.Context) {
return 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() form, err := c.MultipartForm()
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
@ -256,7 +271,7 @@ func UpdateThread(c *gin.Context) {
errorHtmlData := Repositories.BadRequestHtmlData{ errorHtmlData := Repositories.BadRequestHtmlData{
Message: validationErrorMessage, Message: validationErrorMessage,
} }
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
return return
} }
@ -267,7 +282,7 @@ func UpdateThread(c *gin.Context) {
errorHtmlData := Repositories.BadRequestHtmlData{ errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidCaptchaErrorMessage, Message: Repositories.InvalidCaptchaErrorMessage,
} }
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
return return
} }

View File

@ -166,6 +166,24 @@ func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) {
return posts, nil 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) { func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) {
sql := ` sql := `
INSERT INTO posts (is_parent, parent_id, title, text, is_sage, updated_at) INSERT INTO posts (is_parent, parent_id, title, text, is_sage, updated_at)

View File

@ -64,3 +64,4 @@ const InvalidTextLengthErrorMessage = "TEXT SHOULD NOT EXCEED 1000 CHARS"
const InvalidFilesLengthErrorMessage = "MAXIMUM 4 FILES CAN BE UPLOADED" const InvalidFilesLengthErrorMessage = "MAXIMUM 4 FILES CAN BE UPLOADED"
const InvalidFileSizeErrorMessage = "FILE SIZE EXCIDED (3MB PER FILE)" const InvalidFileSizeErrorMessage = "FILE SIZE EXCIDED (3MB PER FILE)"
const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG" const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG"
const ThreadIsArchivedErrorMessage = "THREAD IS ARCHIVED"