feat: add multiple emojis
This commit is contained in:
parent
78957bd23e
commit
237fcb1c31
57
main.go
57
main.go
|
@ -15,6 +15,11 @@ import (
|
|||
|
||||
var db *sql.DB
|
||||
|
||||
type EmojiCount struct {
|
||||
Emoji string
|
||||
TotalCount int
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg := mysql.Config{
|
||||
User: os.Getenv("DBUSER"),
|
||||
|
@ -45,36 +50,64 @@ func main() {
|
|||
r.Use(cors.New(corsConfig))
|
||||
|
||||
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{
|
||||
"postId": c.Query("postId"),
|
||||
"postId": c.Query("postId"),
|
||||
"results": results,
|
||||
})
|
||||
})
|
||||
|
||||
r.POST("/forms/emoji/post", func(c *gin.Context) {
|
||||
postID := c.PostForm("postId")
|
||||
postId := c.PostForm("postId")
|
||||
emoji := c.PostForm("emojiInput")
|
||||
|
||||
if postID == "" || emoji == "" {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postID})
|
||||
if postId == "" || emoji == "" {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postId})
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
var totalCount int
|
||||
|
||||
err = db.QueryRow("SELECT COUNT(*) AS total_count FROM emoji_clicks WHERE post_id = ?;", postID).Scan(&totalCount)
|
||||
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": "error writing to database", "postId": postID})
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": err.Error(), "postId": postId})
|
||||
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")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
<form hx-post="http://localhost:8000/forms/emoji/post" hx-swap="outerHTML">
|
||||
<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>
|
||||
<p>{{.error}}</p>
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user