feat: update db on duplicate reaction

This commit is contained in:
log101 2024-06-11 10:41:06 +03:00
parent 5b11552400
commit 9406032203

36
main.go
View File

@ -10,6 +10,7 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause"
"log101-blog-services/middleware" "log101-blog-services/middleware"
) )
@ -23,8 +24,8 @@ type PostReaction struct {
} }
type EmojiReaction struct { type EmojiReaction struct {
UserAnonIp string UserAnonIp string `gorm:"index:idx_anon,unqiue"`
PostId string PostId string `gorm:"index:idx_anon,unique"`
Emoji string Emoji string
} }
@ -33,7 +34,6 @@ func countEmojis(postId string) (map[string]int, error) {
postReactions := []PostReaction{} postReactions := []PostReaction{}
// Get the emoji counts for each emoji // 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) rows := db.Model(&EmojiReaction{}).Where("post_id = ?", postId).Select([]string{"emoji", "COUNT(*) as total_count"}).Group("emoji").Find(&postReactions)
if rows.Error != nil { if rows.Error != nil {
return nil, errors.New("couldn't get emoji counts") return nil, errors.New("couldn't get emoji counts")
@ -80,13 +80,6 @@ func main() {
r.Use(cors.New(corsConfig)) r.Use(cors.New(corsConfig))
r.Use(middleware.AnonymizeIPMiddleware()) 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/") blogForm := r.Group("/blog/api/")
{ {
// Get the emoji form, you must provide postId query parameter // Get the emoji form, you must provide postId query parameter
@ -96,48 +89,47 @@ func main() {
// get emoji counts for each emoji // get emoji counts for each emoji
emojiCounter, err := countEmojis(postId) emojiCounter, err := countEmojis(postId)
if err != nil { 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
} }
// Return the html template // Return the html template
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{ c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{
"postId": c.Query("postId"),
"results": emojiCounter, "results": emojiCounter,
"hxPostUrl": hxPostUrl,
}) })
}) })
// 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 // 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) { blogForm.POST("/forms/emoji/post", func(c *gin.Context) {
reactedPostId := c.PostForm("postId") reactedPostId := c.PostForm("postId")
reaction := c.PostForm("emojiInput") reaction := c.PostForm("emojiInput")
// Check if parameters are missing // Check if parameters are missing
if reactedPostId == "" || reaction == "" { 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 return
} }
// Add the new emoji entry to the database // Add the new emoji reaction 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.Clauses(clause.OnConflict{
result := db.Create(&EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId}) DoUpdates: clause.AssignmentColumns([]string{"emoji"}),
}).Create(&EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
if result.Error != nil { 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 return
} }
// get emoji counts for each emoji // get emoji counts for each emoji
emojiCounter, err := countEmojis(reactedPostId) emojiCounter, err := countEmojis(reactedPostId)
if err != nil { 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
} }
// Return the html with the updated emoji counter // 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})
}) })
} }