fixed bug where you could comment on non existant blog posts
This commit is contained in:
@@ -72,6 +72,7 @@ func RenderIndividualBlogPost(c *gin.Context) {
|
|||||||
bp := GetBlogPostById(c.Param("url"))
|
bp := GetBlogPostById(c.Param("url"))
|
||||||
if bp == nil {
|
if bp == nil {
|
||||||
c.HTML(http.StatusNotFound, "not_found.tmpl", models.Page{Title: "404"})
|
c.HTML(http.StatusNotFound, "not_found.tmpl", models.Page{Title: "404"})
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
// Blog posts handle the title themselves
|
// Blog posts handle the title themselves
|
||||||
c.HTML(http.StatusOK, "blog_post.tmpl", struct {
|
c.HTML(http.StatusOK, "blog_post.tmpl", struct {
|
||||||
@@ -147,9 +148,9 @@ func GetAllBlogPosts() *[]models.BlogPost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetBlogPostById(id string) *models.BlogPost {
|
func GetBlogPostById(id string) *models.BlogPost {
|
||||||
var post models.BlogPost
|
var post models.BlogPost = models.BlogPost{}
|
||||||
err := db.Model(&post).Where(&models.BlogPost{Url: id}).Find(&post).Error
|
err := db.Model(&post).Where(&models.BlogPost{Url: id}).Find(&post).Error
|
||||||
if err != nil {
|
if err != nil || post.Equals(&models.BlogPost{}) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// TODO: Figure out gorm joins!
|
// TODO: Figure out gorm joins!
|
||||||
|
|||||||
@@ -28,3 +28,7 @@ type BlogPost struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Comments []Comment `gorm:"foreignKey:AssociatedPost;references:Url;type:varchar(191)"`
|
Comments []Comment `gorm:"foreignKey:AssociatedPost;references:Url;type:varchar(191)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b BlogPost) Equals(m *BlogPost) bool {
|
||||||
|
return b.Url == m.Url
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user