feat: add multiple emojis

This commit is contained in:
log101 2024-05-29 10:25:59 +03:00
parent 78957bd23e
commit 237fcb1c31
3 changed files with 48 additions and 13 deletions

BIN
gin-bin Executable file

Binary file not shown.

57
main.go
View File

@ -15,6 +15,11 @@ import (
var db *sql.DB var db *sql.DB
type EmojiCount struct {
Emoji string
TotalCount int
}
func main() { func main() {
cfg := mysql.Config{ cfg := mysql.Config{
User: os.Getenv("DBUSER"), User: os.Getenv("DBUSER"),
@ -45,36 +50,64 @@ func main() {
r.Use(cors.New(corsConfig)) r.Use(cors.New(corsConfig))
r.GET("/forms/emoji", func(c *gin.Context) { r.GET("/forms/emoji", func(c *gin.Context) {
postId := c.Query("postId")
var results []EmojiCount
rows, err := db.Query("SELECT emoji, COUNT(*) FROM emoji_clicks WHERE post_id = ? GROUP BY emoji;", postId)
if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": err.Error(), "postId": postId})
return
}
defer rows.Close()
for rows.Next() {
var ec EmojiCount
if err := rows.Scan(&ec.Emoji, &ec.TotalCount); err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting emoji rows", "postId": postId})
return
}
results = append(results, ec)
}
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{ c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{
"postId": c.Query("postId"), "postId": c.Query("postId"),
"results": results,
}) })
}) })
r.POST("/forms/emoji/post", func(c *gin.Context) { r.POST("/forms/emoji/post", func(c *gin.Context) {
postID := c.PostForm("postId") postId := c.PostForm("postId")
emoji := c.PostForm("emojiInput") emoji := c.PostForm("emojiInput")
if postID == "" || emoji == "" { if postId == "" || emoji == "" {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postID}) c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postId})
return return
} }
_, err := db.Exec("INSERT INTO emoji_clicks (post_id, emoji) VALUES (?, ?)", postID, emoji) _, err := db.Exec("INSERT INTO emoji_clicks (post_id, emoji) VALUES (?, ?)", postId, emoji)
if err != nil { if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postID}) c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postId})
return return
} }
var totalCount int var results []EmojiCount
rows, err := db.Query("SELECT emoji, COUNT(*) FROM emoji_clicks WHERE post_id = ? GROUP BY emoji;", postId)
err = db.QueryRow("SELECT COUNT(*) AS total_count FROM emoji_clicks WHERE post_id = ?;", postID).Scan(&totalCount)
if err != nil { if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postID}) c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": err.Error(), "postId": postId})
return return
} }
defer rows.Close()
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postID, "thumbsUpCount": totalCount}) for rows.Next() {
var ec EmojiCount
if err := rows.Scan(&ec.Emoji, &ec.TotalCount); err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting emoji rows", "postId": postId})
return
}
results = append(results, ec)
}
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": results})
}) })
r.Run(":8000") r.Run(":3001")
} }

View File

@ -1,6 +1,8 @@
<form hx-post="http://localhost:8000/forms/emoji/post" hx-swap="outerHTML"> <form hx-post="http://localhost:8000/forms/emoji/post" hx-swap="outerHTML">
<input type="hidden" name="postId" value="{{.postId}}"> <input type="hidden" name="postId" value="{{.postId}}">
<button name="emojiInput" value="👍" type="submit">👍 {{.thumbsUpCount}}</button> {{ range .results }}
<button name="emojiInput" value="{{ .Emoji }}" type="submit" class="text-lg">{{ .Emoji }} {{ .TotalCount }}</button>
{{ end }}
</form> </form>
<p>{{.error}}</p> <p>{{.error}}</p>