feat: add like button
This commit is contained in:
parent
1c911c2693
commit
78957bd23e
2
go.mod
2
go.mod
|
@ -5,9 +5,11 @@ go 1.22.3
|
|||
require (
|
||||
github.com/gin-contrib/cors v1.7.2
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/go-sql-driver/mysql v1.8.1
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/bytedance/sonic v1.11.7 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,3 +1,5 @@
|
|||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/bytedance/sonic v1.11.7 h1:k/l9p1hZpNIMJSk37wL9ltkcpqLfIho1vYthi4xT2t4=
|
||||
github.com/bytedance/sonic v1.11.7/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
|
@ -26,6 +28,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
|||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
|
|
56
main.go
56
main.go
|
@ -1,13 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func main() {
|
||||
cfg := mysql.Config{
|
||||
User: os.Getenv("DBUSER"),
|
||||
Passwd: os.Getenv("DBPASS"),
|
||||
Net: "tcp",
|
||||
Addr: "127.0.0.1:3306",
|
||||
DBName: "emojis",
|
||||
}
|
||||
// Get a database handle.
|
||||
var err error
|
||||
db, err = sql.Open("mysql", cfg.FormatDSN())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pingErr := db.Ping()
|
||||
if pingErr != nil {
|
||||
log.Fatal(pingErr)
|
||||
}
|
||||
fmt.Println("Connected!")
|
||||
|
||||
r := gin.Default()
|
||||
r.LoadHTMLGlob("templates/*")
|
||||
|
||||
|
@ -17,11 +45,35 @@ func main() {
|
|||
r.Use(cors.New(corsConfig))
|
||||
|
||||
r.GET("/forms/emoji", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{})
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{
|
||||
"postId": c.Query("postId"),
|
||||
})
|
||||
})
|
||||
|
||||
r.POST("/forms/emoji/post", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{})
|
||||
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})
|
||||
return
|
||||
}
|
||||
|
||||
_, 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})
|
||||
return
|
||||
}
|
||||
|
||||
var totalCount int
|
||||
|
||||
err = db.QueryRow("SELECT COUNT(*) AS total_count FROM emoji_clicks WHERE post_id = ?;", postID).Scan(&totalCount)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postID})
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postID, "thumbsUpCount": totalCount})
|
||||
})
|
||||
|
||||
r.Run(":8000")
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
-- schema.sql
|
||||
CREATE TABLE IF NOT EXISTS emoji_clicks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
post_id TEXT NOT NULL,
|
||||
emoji TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
<form hx-post="http://localhost:8000/forms/emoji/post" hx-swap="outerHTML">
|
||||
<button name="emojiInput" value="👍" type="submit">👍</button>
|
||||
<button name="emojiInput" value="👎" type="submit">👎</button>
|
||||
<button name="emojiInput" value="😀" type="submit">😀</button>
|
||||
<button name="emojiInput" value="😑" type="submit">😑</button>
|
||||
<button name="emojiInput" value="🤢" type="submit">🤢</button>
|
||||
<button name="emojiInput" value="👀" type="submit">👀</button>
|
||||
<input type="hidden" name="postId" value="{{.postId}}">
|
||||
<button name="emojiInput" value="👍" type="submit">👍 {{.thumbsUpCount}}</button>
|
||||
</form>
|
||||
|
||||
<p>{{.error}}</p>
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user