From 581e7e258af38c7811ce95c5b7e9f8c411c04fe0 Mon Sep 17 00:00:00 2001 From: rawleyIfowler Date: Wed, 23 Feb 2022 11:41:29 -0600 Subject: [PATCH] added blog posting --- controllers/blog.go | 13 ++++++++++--- models/comment.go | 6 +++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/controllers/blog.go b/controllers/blog.go index 6e8f56e..b4f35f7 100644 --- a/controllers/blog.go +++ b/controllers/blog.go @@ -18,8 +18,8 @@ along with this program. If not, see .Rawley Fow */ import ( + "encoding/json" "io/ioutil" - "log" "net/http" "github.com/gin-gonic/gin" @@ -67,12 +67,19 @@ func CreateBlogPost(c *gin.Context) { c.Status(403) return } - data, err := ioutil.ReadAll(c.Request.Body) + raw, err := ioutil.ReadAll(c.Request.Body) if err != nil { c.Status(406) return } - log.Println(data) + var data models.BlogPost + // Write the data from the request to the data variable + if json.Unmarshal([]byte(raw), &data) != nil { + c.Status(406) + return + } + // Create the blog post in the database + db.Create(&data) } func GetAllBlogPosts() *[]models.BlogPost { diff --git a/models/comment.go b/models/comment.go index ca67103..f8e9a5a 100644 --- a/models/comment.go +++ b/models/comment.go @@ -22,8 +22,8 @@ import ( type Comment struct { gorm.Model - Id uint `json:"id"` - Date string `json:"date"` - Author string `json:"author"` + Id uint `json:"id"` + Date string `json:"date"` + Author string `json:"author"` Content string `json:"content"` }