style: add styling

This commit is contained in:
log101 2024-05-29 11:04:10 +03:00
parent 237fcb1c31
commit 34ddac1454
3 changed files with 30 additions and 9 deletions

BIN
gin-bin

Binary file not shown.

28
main.go
View File

@ -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")

View File

@ -1,8 +1,13 @@
<form hx-post="http://localhost:8000/forms/emoji/post" hx-swap="outerHTML">
<input type="hidden" name="postId" value="{{.postId}}">
{{ range .results }}
<button name="emojiInput" value="{{ .Emoji }}" type="submit" class="text-lg">{{ .Emoji }} {{ .TotalCount }}</button>
{{ end }}
<div class="emoji-buttons-container">
<button name="emojiInput" value="👍" type="submit" class="emoji-button">👍&nbsp; {{ if gt (index .results "👍") 0 }} {{ index .results "👍"}} {{ end }}</button>
<button name="emojiInput" value="👎" type="submit" class="emoji-button">👎&nbsp; {{ if gt (index .results "👎") 0 }} {{ index .results "👎"}} {{ end }}</button>
<button name="emojiInput" value="😀" type="submit" class="emoji-button">😀&nbsp; {{ if gt (index .results "😀") 0 }} {{ index .results "😀"}} {{ end }}</button>
<button name="emojiInput" value="😑" type="submit" class="emoji-button">😑&nbsp; {{ if gt (index .results "😑") 0 }} {{ index .results "😑"}} {{ end }}</button>
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">🤢&nbsp; {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">👀&nbsp; {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
</div>
</form>
<p>{{.error}}</p>