refactor: extract comment handlers to a seperate file

This commit is contained in:
log101 2024-06-20 19:40:38 +03:00
parent 0bc4325102
commit 8adfa22c9d
4 changed files with 75 additions and 45 deletions

63
handlers/comment.go Normal file
View File

@ -0,0 +1,63 @@
// HANDLERS FOR COMMENTING ON POSTS
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
DB "log101-blog-services/db"
"log101-blog-services/models"
)
func GetComments(c *gin.Context) {
db := DB.GetDB()
postId := c.Query("postId")
comments := []models.Comment{}
// Post id is required
if postId == "" {
c.AbortWithStatus(http.StatusBadRequest)
return
}
// Retrieve comments related to post
rows := db.Where("post_id = ?", postId).Find(&comments)
if rows.Error != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if rows.RowsAffected == 0 {
c.AbortWithStatus(http.StatusNoContent)
return
} else {
c.HTML(http.StatusOK, "comments.tmpl", gin.H{"Comments": comments})
}
}
func PostComment(c *gin.Context) {
db := DB.GetDB()
postId := c.PostForm("postId")
commentBody := c.PostForm("commentBody")
username := c.PostForm("username")
if postId == "" || commentBody == "" {
c.AbortWithStatus(http.StatusBadRequest)
return
}
// username is anonymous if not present
if username == "" {
username = "anonim"
}
result := db.Create(&models.Comment{Body: commentBody, PostId: postId, Username: username})
if result.Error != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.HTML(http.StatusOK, "comment.tmpl", gin.H{"Username": username, "Body": commentBody})
}

View File

@ -1,3 +1,5 @@
// HANDLERS FOR EMOJI FORMS AND EMOJI REACTIONS
// BELOW POSTS
package handlers
import (
@ -15,7 +17,7 @@ func GetEmojiForm(c *gin.Context) {
postId := c.Query("postId")
// get emoji counts for each emoji
emojiCounter, err := CountEmojis(postId)
emojiCounter, err := GetEmojis(postId)
if err != nil {
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts"})
return
@ -46,7 +48,7 @@ func PostEmojiForm(c *gin.Context) {
}
// get emoji counts for each emoji
emojiCounter, err := CountEmojis(reactedPostId)
emojiCounter, err := GetEmojis(reactedPostId)
if err != nil {
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error getting the emoji counts"})
}
@ -56,7 +58,7 @@ func PostEmojiForm(c *gin.Context) {
}
// Get the emoji counts foe a given post id
func CountEmojis(postId string) (map[string]int, error) {
func GetEmojis(postId string) (map[string]int, error) {
postReactions := []models.PostReaction{}
db := DB.GetDB()

View File

@ -83,11 +83,11 @@ func TestPostEmojiFormMissingParams(t *testing.T) {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "missing parameters")
}
func TestCountEmojis(t *testing.T) {
func TestGetEmojis(t *testing.T) {
db := SetupTestDB()
DB.SetDB(db) // Set the mock DB
@ -95,7 +95,7 @@ func TestCountEmojis(t *testing.T) {
db.Create(&models.EmojiReaction{UserAnonIp: "127.0.0.2", Emoji: "😊", PostId: "1"})
db.Create(&models.EmojiReaction{UserAnonIp: "127.0.0.1", Emoji: "😂", PostId: "1"})
counts, err := CountEmojis("1")
counts, err := GetEmojis("1")
assert.NoError(t, err)
assert.Equal(t, 1, counts["😊"])
assert.Equal(t, 1, counts["😂"])

43
main.go
View File

@ -2,7 +2,6 @@ package main
import (
"log"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
@ -12,7 +11,6 @@ import (
DB "log101-blog-services/db"
"log101-blog-services/handlers"
"log101-blog-services/middleware"
"log101-blog-services/models"
)
func main() {
@ -54,44 +52,11 @@ func main() {
// updates if user reacted before
blog.POST("/forms/emoji", handlers.PostEmojiForm)
blog.GET("/comments", func(c *gin.Context) {
db := DB.GetDB()
postId := c.Query("postId")
comments := []models.Comment{}
// Get the comments for a given post id
blog.GET("/comments", handlers.GetComments)
if postId == "" {
c.AbortWithStatus(http.StatusBadRequest)
return
}
rows := db.Where("post_id = ?", postId).Find(&comments)
if rows.Error != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.HTML(http.StatusOK, "comments.tmpl", gin.H{"Comments": comments})
})
blog.POST("/comments", func(c *gin.Context) {
db := DB.GetDB()
postId := c.PostForm("postId")
commentBody := c.PostForm("commentBody")
username := c.PostForm("username")
if postId == "" || commentBody == "" {
c.AbortWithStatus(http.StatusBadRequest)
return
}
result := db.Create(&models.Comment{Body: commentBody, PostId: postId, Username: username})
if result.Error != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.HTML(http.StatusOK, "comment.tmpl", gin.H{"Username": username, "Body": commentBody})
})
// Drop comment on a post, postId and comment body is required
blog.POST("/comments", handlers.PostComment)
}