From 89487cda3b56b631cb56ad9750ab9fa43830883a Mon Sep 17 00:00:00 2001
From: Yanislav Igonin <yanislavdabest@gmail.com>
Date: Thu, 7 Oct 2021 10:51:18 +0300
Subject: [PATCH] feat: make sage possible

---
 controllers/threads_controller.go | 2 +-
 repositories/posts_repository.go  | 4 +++-
 templates/post-form.html          | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go
index d2a7938..784f3a4 100644
--- a/controllers/threads_controller.go
+++ b/controllers/threads_controller.go
@@ -254,7 +254,7 @@ func UpdateThread(c *gin.Context) {
 	if len(isSageField) != 0 {
 		isSageString = isSageField[0]
 	}
-	isSage := isSageString == "true"
+	isSage := isSageString == "on"
 
 	conn, err := Db.Pool.Acquire(context.TODO())
 	if err != nil {
diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go
index 804da5b..a33b3ee 100644
--- a/repositories/posts_repository.go
+++ b/repositories/posts_repository.go
@@ -188,7 +188,7 @@ func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) {
 	}
 
 	// updating parent post `updated_at`
-	if !p.IsParent {
+	if !p.IsParent && !p.IsSage {
 		sql = `
 			UPDATE posts
 			SET updated_at = now()
@@ -197,6 +197,8 @@ func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) {
 		row := tx.QueryRow(context.TODO(), sql, p.ParentID)
 		var msg string
 		err = row.Scan(&msg)
+		// UPDATE always return `no rows`
+		// so we need to check this condition
 		if err != nil && err != pgx.ErrNoRows {
 			return 0, err
 		}
diff --git a/templates/post-form.html b/templates/post-form.html
index 6990d21..c4ae8de 100644
--- a/templates/post-form.html
+++ b/templates/post-form.html
@@ -24,7 +24,7 @@
       <div class="row">
         {{ if ne .FirstPostID 0 }}
         <div class="col">
-          <input class="form-check-input" type="checkbox" value="" id="postSage" name="sage">
+          <input class="form-check-input" type="checkbox" value="on" id="postSage" name="sage">
           <label class="form-check-label" for="postSage">
             Sage
           </label>