feat: group routes

This commit is contained in:
log101 2024-05-30 13:30:32 +03:00
parent 4868e4d7cf
commit a376800eeb

83
main.go
View File

@ -97,54 +97,57 @@ func main() {
corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request"} corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request"}
r.Use(cors.New(corsConfig)) r.Use(cors.New(corsConfig))
// Get the emoji form, you must provide postId query parameter blogForm := r.Group("/blog/api/")
r.GET("/forms/emoji", func(c *gin.Context) { {
postId := c.Query("postId") // Get the emoji form, you must provide postId query parameter
blogForm.GET("/forms/emoji", func(c *gin.Context) {
postId := c.Query("postId")
// get emoji counts for each emoji // get emoji counts for each emoji
emojiCounter, err := countEmojis(postId) emojiCounter, err := countEmojis(postId)
if err != nil { if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId}) c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId})
return return
} }
// Return the html template // Return the html template
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": emojiCounter, "results": emojiCounter,
})
}) })
})
// Update the emoji counter, this handler will // Update the emoji counter, this handler will
// add a new entry to the database with anonymized ip // add a new entry to the database with anonymized ip
// then sums the results to get the emoji counter // then sums the results to get the emoji counter
r.POST("/forms/emoji/post", func(c *gin.Context) { blogForm.POST("/forms/emoji/post", func(c *gin.Context) {
postId := c.PostForm("postId") postId := c.PostForm("postId")
emoji := c.PostForm("emojiInput") emoji := c.PostForm("emojiInput")
// Check if parameters are missing // Check if parameters are missing
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
} }
// Add the new emoji entry to the database // Add the new emoji entry to the database
_, 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
} }
// get emoji counts for each emoji // get emoji counts for each emoji
emojiCounter, err := countEmojis(postId) emojiCounter, err := countEmojis(postId)
if err != nil { if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId}) c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId})
return return
} }
// Return the html with the updated emoji counter // Return the html with the updated emoji counter
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounter}) c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounter})
}) })
}
r.Run(":8000") r.Run(":8000")
} }