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