diff --git a/controllers/blog.go b/controllers/blog.go index cb64edf..a2ebfbf 100644 --- a/controllers/blog.go +++ b/controllers/blog.go @@ -1,8 +1,6 @@ package controllers -/* -Copyright (C) 2022 Rawley Fowler - +/* Copyright (C) 2022 Rawley Fowler This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or @@ -47,6 +45,7 @@ func RegisterBlogGroup(r *gin.RouterGroup) { r.GET("/", RenderBlogPage) r.GET("/post/:url", RenderIndividualBlogPost) r.POST("/post", CreateBlogPost) + r.POST("/post/comment", CreateComment) } func RenderBlogPage(c *gin.Context) { @@ -84,12 +83,31 @@ func CreateBlogPost(c *gin.Context) { return } // Create the blog post in the database - db.Create(&data) + if db.Create(&data).Error != nil { + c.Status(http.StatusNotAcceptable) + } else { + c.Status(http.StatusAccepted) + } +} + +func CreateComment(c *gin.Context) { + if c.Request.ParseForm() != nil { + c.Status(http.StatusNotAcceptable) + return + } + comment := models.Comment{ + Author: c.Request.Form.Get("author"), + Content: c.Request.Form.Get("content"), + AssociatedPost: c.Request.Form.Get("url"), + } + db.Create(&comment) } func GetAllBlogPosts() *[]models.BlogPost { var posts []models.BlogPost - db.Find(&posts) + // Select title, date, and url fields from the blog post records an store them in posts. + // This is so we don't grab the entire blog post when we render them on the overview page. Saves a couple ms. + db.Model(&models.BlogPost{}).Select("title, date, url").Take(&posts) return &posts } @@ -99,5 +117,11 @@ func GetBlogPostById(id string) *models.BlogPost { if err != nil { return nil } + // TODO: Figure out gorm joins! + // gorm joins are not working at all, just going to do another query to make it work for now. + err = db.Where(&models.Comment{AssociatedPost: id}).Find(&post.Comments).Error + if err != nil { + return nil + } return &post } diff --git a/models/blog_post.go b/models/blog_post.go index ad29438..cf0a295 100644 --- a/models/blog_post.go +++ b/models/blog_post.go @@ -22,9 +22,9 @@ import ( type BlogPost struct { gorm.Model - Url string `json:"url" gorm:"primaryKey" gorm:"unique"` + Url string `json:"url" gorm:"primaryKey"` Title string `json:"title" gorm:"unique"` - Date string `json:"date"` + Date string `json:"date" gorm:"default:NOW()"` Content string `json:"content"` - Comments []Comment `gorm:"foreignKey:AssociatedPost"` + Comments []Comment `gorm:"foreignKey:AssociatedPost;references:Url;type:varchar(191)"` } diff --git a/models/comment.go b/models/comment.go index f973347..8234c87 100644 --- a/models/comment.go +++ b/models/comment.go @@ -22,8 +22,8 @@ import ( type Comment struct { gorm.Model - Id uint `json:"id" gorm: "primaryKey" gorm: "unique"` - Date string `json:"date"` + Id uint `json:"id" gorm:"primaryKey, AUTO_INCREMENT"` + Date string `json:"date" gorm:"default:NOW()"` 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 1edc3d6..14a2feb 100644 --- a/templates/blog_post.tmpl +++ b/templates/blog_post.tmpl @@ -16,11 +16,11 @@

No comments...

{{ end }}

Leave a comment!

-
+ - +
-{{ template "footer.tmpl" . }} +{{ template "footer_no_banners.tmpl" . }}