added comments and added a comment end point
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
/*
|
/* Copyright (C) 2022 Rawley Fowler
|
||||||
Copyright (C) 2022 Rawley Fowler
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
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("/", RenderBlogPage)
|
||||||
r.GET("/post/:url", RenderIndividualBlogPost)
|
r.GET("/post/:url", RenderIndividualBlogPost)
|
||||||
r.POST("/post", CreateBlogPost)
|
r.POST("/post", CreateBlogPost)
|
||||||
|
r.POST("/post/comment", CreateComment)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderBlogPage(c *gin.Context) {
|
func RenderBlogPage(c *gin.Context) {
|
||||||
@@ -84,12 +83,31 @@ func CreateBlogPost(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Create the blog post in the database
|
// 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 {
|
func GetAllBlogPosts() *[]models.BlogPost {
|
||||||
var posts []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
|
return &posts
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,5 +117,11 @@ func GetBlogPostById(id string) *models.BlogPost {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 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
|
return &post
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ import (
|
|||||||
|
|
||||||
type BlogPost struct {
|
type BlogPost struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Url string `json:"url" gorm:"primaryKey" gorm:"unique"`
|
Url string `json:"url" gorm:"primaryKey"`
|
||||||
Title string `json:"title" gorm:"unique"`
|
Title string `json:"title" gorm:"unique"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date" gorm:"default:NOW()"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Comments []Comment `gorm:"foreignKey:AssociatedPost"`
|
Comments []Comment `gorm:"foreignKey:AssociatedPost;references:Url;type:varchar(191)"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Id uint `json:"id" gorm: "primaryKey" gorm: "unique"`
|
Id uint `json:"id" gorm:"primaryKey, AUTO_INCREMENT"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date" gorm:"default:NOW()"`
|
||||||
Author string `json:"author" form:"author"`
|
Author string `json:"author" form:"author"`
|
||||||
Content string `json:"content" form:"content"`
|
Content string `json:"content" form:"content"`
|
||||||
AssociatedPost string
|
AssociatedPost string
|
||||||
|
|||||||
@@ -16,11 +16,11 @@
|
|||||||
<p>No comments...</p>
|
<p>No comments...</p>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
<h4>Leave a comment!</h4>
|
<h4>Leave a comment!</h4>
|
||||||
<form method="POST" action="/post/comment">
|
<form method="POST" action="/blog/post/comment">
|
||||||
<input type="text" placeholder="Name" name="author"/>
|
<input type="text" placeholder="Name" name="author"/>
|
||||||
<input type="field" placeholder="Comment" name="content" />
|
<input type="field" placeholder="Comment" name="content" />
|
||||||
<input type="text" style="display: none;" value="{{ .Url }}" readonly />
|
<input type="text" style="display: none;" name="url" value="{{ .Url }}" readonly />
|
||||||
<button type="submit">Post</button>
|
<button type="submit">Post</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{{ template "footer.tmpl" . }}
|
{{ template "footer_no_banners.tmpl" . }}
|
||||||
|
|||||||
Reference in New Issue
Block a user