From 14e3bed6bb141d871a35192dc50c77ba83908656 Mon Sep 17 00:00:00 2001 From: log101 Date: Tue, 18 Jun 2024 21:36:30 +0300 Subject: [PATCH] feat: add endpoints --- handlers/emoji_form.go | 5 +--- main.go | 41 ++++++++++++++++++++++++++++++--- models/Comment.go | 8 +++++++ templates/comment_form.tmpl | 11 +++++++++ templates/emoji_form.tmpl | 12 +++++----- templates/emoji_form_error.tmpl | 12 +++++----- 6 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 models/Comment.go create mode 100644 templates/comment_form.tmpl diff --git a/handlers/emoji_form.go b/handlers/emoji_form.go index 3be3824..d5746ee 100644 --- a/handlers/emoji_form.go +++ b/handlers/emoji_form.go @@ -34,8 +34,7 @@ func PostEmojiForm(c *gin.Context) { // Check if parameters are missing if reactedPostId == "" || reaction == "" { - c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"}) - return + c.HTML(http.StatusBadRequest, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"}) } // Add the new emoji reaction to the database @@ -44,14 +43,12 @@ func PostEmojiForm(c *gin.Context) { }).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId}) if result.Error != nil { c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error writing to database"}) - return } // get emoji counts for each emoji emojiCounter, err := CountEmojis(reactedPostId) if err != nil { c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error getting the emoji counts"}) - return } // Return the html with the updated emoji counter diff --git a/main.go b/main.go index 03e5be1..ec78442 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/http" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" @@ -11,6 +12,7 @@ import ( DB "log101-blog-services/db" "log101-blog-services/handlers" "log101-blog-services/middleware" + "log101-blog-services/models" ) func main() { @@ -42,15 +44,48 @@ func main() { r.Use(cors.New(corsConfig)) r.Use(middleware.AnonymizeIPMiddleware()) - blogForm := r.Group("/blog/api/") + blog := r.Group("/api/blog/") { // Get the emoji form, you must provide postId query parameter - blogForm.GET("/forms/emoji", handlers.GetEmojiForm) + blog.GET("/forms/emoji", handlers.GetEmojiForm) // Update the user's reaction to post, this handler will // add a new entry to the database with anonymized ip // updates if user reacted before - blogForm.POST("/forms/emoji/post", handlers.PostEmojiForm) + blog.POST("/forms/emoji", handlers.PostEmojiForm) + + blog.GET("/comments", func(c *gin.Context) { + db := DB.GetDB() + postId := c.Query("postId") + comments := []models.Comment{} + + rows := db.Where("post_id = ?", postId).Find(&comments) + + if rows.Error != nil { + c.HTML(http.StatusInternalServerError, "comment_form.tmpl", gin.H{"errorMessage": "error getting comments", "comments": comments}) + } + + c.HTML(http.StatusOK, "comment_form.tmpl", gin.H{"errorMessage": "error getting comments", "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.HTML(http.StatusBadRequest, "comment_form.tmpl", gin.H{}) + } + + result := db.Create(&models.Comment{Body: commentBody, PostId: postId, Username: username}) + if result.Error != nil { + c.HTML(http.StatusInternalServerError, "comment_form.tmpl", gin.H{}) + } + + c.HTML(http.StatusOK, "comment_form.tmpl", gin.H{}) + }) + } r.Run(":8000") diff --git a/models/Comment.go b/models/Comment.go new file mode 100644 index 0000000..6f5cc50 --- /dev/null +++ b/models/Comment.go @@ -0,0 +1,8 @@ +package models + +// Gorm model +type Comment struct { + Body string + PostId string + Username string +} diff --git a/templates/comment_form.tmpl b/templates/comment_form.tmpl new file mode 100644 index 0000000..54227e8 --- /dev/null +++ b/templates/comment_form.tmpl @@ -0,0 +1,11 @@ +
+

+ Arafat Candan +

+ +

+ Daha önce hiç bu açıdan bakmamıştım, harika bir yazı olmuş. +

+
diff --git a/templates/emoji_form.tmpl b/templates/emoji_form.tmpl index 32672e2..a74ce72 100644 --- a/templates/emoji_form.tmpl +++ b/templates/emoji_form.tmpl @@ -1,9 +1,9 @@
- - - - - - + + + + + +
diff --git a/templates/emoji_form_error.tmpl b/templates/emoji_form_error.tmpl index 3d9a8c1..d8be0fd 100644 --- a/templates/emoji_form_error.tmpl +++ b/templates/emoji_form_error.tmpl @@ -1,9 +1,9 @@
- - - - - - + + + + + +

{{ .errorMessage }}