diff --git a/controllers/blog.go b/controllers/blog.go index b4f35f7..cb64edf 100644 --- a/controllers/blog.go +++ b/controllers/blog.go @@ -54,28 +54,33 @@ func RenderBlogPage(c *gin.Context) { } func RenderIndividualBlogPost(c *gin.Context) { - c.HTML(http.StatusOK, "blog_post.tmpl", GetBlogPostById(c.Param("url"))) + bp := GetBlogPostById(c.Param("url")) + if bp == nil { + c.HTML(http.StatusNotFound, "not_found.tmpl", gin.H{}) + } else { + c.HTML(http.StatusOK, "blog_post.tmpl", bp) + } } func CreateBlogPost(c *gin.Context) { reqKey, err := c.Request.Cookie("APK") if err != nil { - c.Status(406) + c.Status(http.StatusForbidden) return } if reqKey.Value != apiKey { - c.Status(403) + c.Status(http.StatusForbidden) return } raw, err := ioutil.ReadAll(c.Request.Body) if err != nil { - c.Status(406) + c.Status(http.StatusNotAcceptable) return } var data models.BlogPost // Write the data from the request to the data variable if json.Unmarshal([]byte(raw), &data) != nil { - c.Status(406) + c.Status(http.StatusNotAcceptable) return } // Create the blog post in the database @@ -90,6 +95,9 @@ func GetAllBlogPosts() *[]models.BlogPost { func GetBlogPostById(id string) *models.BlogPost { var post models.BlogPost - db.Where(&models.BlogPost{Url: id}).First(&post) + err := db.Where(&models.BlogPost{Url: id}).First(&post).Error + if err != nil { + return nil + } return &post } diff --git a/main.go b/main.go index bb3aedf..fee5c9f 100644 --- a/main.go +++ b/main.go @@ -27,8 +27,9 @@ var dsn string func main() { router = gin.Default() + router.Use(gin.Recovery()) // This needs to change because of RCCTL in OpenBSD, not sure if you can use a ksh variable as path in Gin but i'll test router.LoadHTMLGlob("templates/*.tmpl") bootstrap.InitializeRoutes(router) - router.Run() + router.Run(":8080") } diff --git a/models/blog_post.go b/models/blog_post.go index 4591e9f..ad29438 100644 --- a/models/blog_post.go +++ b/models/blog_post.go @@ -26,5 +26,5 @@ type BlogPost struct { Title string `json:"title" gorm:"unique"` Date string `json:"date"` Content string `json:"content"` - Comments []Comment `gorm:"foreignKey:Id"` + Comments []Comment `gorm:"foreignKey:AssociatedPost"` } diff --git a/models/comment.go b/models/comment.go index f8e9a5a..f973347 100644 --- a/models/comment.go +++ b/models/comment.go @@ -22,8 +22,9 @@ import ( type Comment struct { gorm.Model - Id uint `json:"id"` - Date string `json:"date"` - Author string `json:"author"` - Content string `json:"content"` + Id uint `json:"id" gorm: "primaryKey" gorm: "unique"` + Date string `json:"date"` + Author string `json:"author" form:"author"` + Content string `json:"content" form:"content"` + AssociatedPost string } diff --git a/templates/blog_post.tmpl b/templates/blog_post.tmpl index 1ee98f1..de289df 100644 --- a/templates/blog_post.tmpl +++ b/templates/blog_post.tmpl @@ -1,7 +1,8 @@ {{ template "header.tmpl" . }}
No comments...
{{ end }} +Leave a comment!