diff --git a/gin-bin b/gin-bin deleted file mode 100755 index f7b811b..0000000 Binary files a/gin-bin and /dev/null differ diff --git a/main.go b/main.go index b8a8ad7..ac06593 100644 --- a/main.go +++ b/main.go @@ -50,8 +50,16 @@ func main() { r.Use(cors.New(corsConfig)) r.GET("/forms/emoji", func(c *gin.Context) { + var emojiCounts = map[string]int{ + "👍": 0, + "👎": 0, + "😀": 0, + "😑": 0, + "🤢": 0, + "👀": 0, + } 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}) @@ -65,12 +73,12 @@ func main() { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting emoji rows", "postId": postId}) return } - results = append(results, ec) + emojiCounts[ec.Emoji] = ec.TotalCount } c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{ "postId": c.Query("postId"), - "results": results, + "results": emojiCounts, }) }) @@ -78,6 +86,15 @@ func main() { postId := c.PostForm("postId") emoji := c.PostForm("emojiInput") + var emojiCounts = map[string]int{ + "👍": 0, + "👎": 0, + "😀": 0, + "😑": 0, + "🤢": 0, + "👀": 0, + } + if postId == "" || emoji == "" { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postId}) return @@ -89,7 +106,6 @@ func main() { return } - 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}) @@ -103,10 +119,10 @@ func main() { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting emoji rows", "postId": postId}) return } - results = append(results, ec) + emojiCounts[ec.Emoji] = ec.TotalCount } - c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": results}) + c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounts}) }) r.Run(":3001") diff --git a/templates/emoji_form.tmpl b/templates/emoji_form.tmpl index 8a15a35..c31efe0 100644 --- a/templates/emoji_form.tmpl +++ b/templates/emoji_form.tmpl @@ -1,8 +1,13 @@
-{{ range .results }} - -{{ end }} +
+ + + + + + +

{{.error}}