From 9406032203ab932971dc0eec7e392314472611f6 Mon Sep 17 00:00:00 2001 From: log101 Date: Tue, 11 Jun 2024 10:41:06 +0300 Subject: [PATCH] feat: update db on duplicate reaction --- main.go | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index d6c9a06..515ca15 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/joho/godotenv" "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/clause" "log101-blog-services/middleware" ) @@ -23,8 +24,8 @@ type PostReaction struct { } type EmojiReaction struct { - UserAnonIp string - PostId string + UserAnonIp string `gorm:"index:idx_anon,unqiue"` + PostId string `gorm:"index:idx_anon,unique"` Emoji string } @@ -33,7 +34,6 @@ func countEmojis(postId string) (map[string]int, error) { postReactions := []PostReaction{} // Get the emoji counts for each emoji - // rows, err := db.Query("SELECT emoji, COUNT(*) FROM emoji_clicks WHERE post_id = ? GROUP BY emoji;", postId) rows := db.Model(&EmojiReaction{}).Where("post_id = ?", postId).Select([]string{"emoji", "COUNT(*) as total_count"}).Group("emoji").Find(&postReactions) if rows.Error != nil { return nil, errors.New("couldn't get emoji counts") @@ -80,13 +80,6 @@ func main() { r.Use(cors.New(corsConfig)) r.Use(middleware.AnonymizeIPMiddleware()) - var hxPostUrl string - if ginMode == gin.DebugMode { - hxPostUrl = "http://localhost:8000/blog/api/forms/emoji/post" - } else { - hxPostUrl = "https://log101.dev/blog/api/forms/emoji/post" - } - blogForm := r.Group("/blog/api/") { // Get the emoji form, you must provide postId query parameter @@ -96,48 +89,47 @@ func main() { // get emoji counts for each emoji emojiCounter, err := countEmojis(postId) if err != nil { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId, "hxPostUrl": hxPostUrl}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts"}) return } // Return the html template c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{ - "postId": c.Query("postId"), - "results": emojiCounter, - "hxPostUrl": hxPostUrl, + "results": emojiCounter, }) }) - // Update the emoji counter, this handler will + // Update the user's reaction to post, this handler will // add a new entry to the database with anonymized ip - // then sums the results to get the emoji counter + // updates if user reacted before blogForm.POST("/forms/emoji/post", func(c *gin.Context) { reactedPostId := c.PostForm("postId") reaction := c.PostForm("emojiInput") // Check if parameters are missing if reactedPostId == "" || reaction == "" { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": reactedPostId, "hxPostUrl": hxPostUrl}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters"}) return } - // Add the new emoji entry to the database - // _, err := db.Exec("INSERT INTO emoji_clicks (user_anon_data, post_id, emoji) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE emoji = ?", c.Request.RemoteAddr, postId, emoji, emoji) - result := db.Create(&EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId}) + // Add the new emoji reaction to the database + result := db.Clauses(clause.OnConflict{ + DoUpdates: clause.AssignmentColumns([]string{"emoji"}), + }).Create(&EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId}) if result.Error != nil { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": reactedPostId, "hxPostUrl": hxPostUrl}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database"}) return } // get emoji counts for each emoji emojiCounter, err := countEmojis(reactedPostId) if err != nil { - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": reactedPostId, "hxPostUrl": hxPostUrl}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts"}) return } // Return the html with the updated emoji counter - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": reactedPostId, "results": emojiCounter, "hxPostUrl": hxPostUrl}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"results": emojiCounter}) }) }