added a 404 to bad blog posts
This commit is contained in:
@@ -54,28 +54,33 @@ func RenderBlogPage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RenderIndividualBlogPost(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) {
|
func CreateBlogPost(c *gin.Context) {
|
||||||
reqKey, err := c.Request.Cookie("APK")
|
reqKey, err := c.Request.Cookie("APK")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Status(406)
|
c.Status(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if reqKey.Value != apiKey {
|
if reqKey.Value != apiKey {
|
||||||
c.Status(403)
|
c.Status(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
raw, err := ioutil.ReadAll(c.Request.Body)
|
raw, err := ioutil.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Status(406)
|
c.Status(http.StatusNotAcceptable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var data models.BlogPost
|
var data models.BlogPost
|
||||||
// Write the data from the request to the data variable
|
// Write the data from the request to the data variable
|
||||||
if json.Unmarshal([]byte(raw), &data) != nil {
|
if json.Unmarshal([]byte(raw), &data) != nil {
|
||||||
c.Status(406)
|
c.Status(http.StatusNotAcceptable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Create the blog post in the database
|
// Create the blog post in the database
|
||||||
@@ -90,6 +95,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
|
||||||
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
|
return &post
|
||||||
}
|
}
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -27,8 +27,9 @@ var dsn string
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router = gin.Default()
|
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
|
// 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")
|
router.LoadHTMLGlob("templates/*.tmpl")
|
||||||
bootstrap.InitializeRoutes(router)
|
bootstrap.InitializeRoutes(router)
|
||||||
router.Run()
|
router.Run(":8080")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,5 +26,5 @@ type BlogPost struct {
|
|||||||
Title string `json:"title" gorm:"unique"`
|
Title string `json:"title" gorm:"unique"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Comments []Comment `gorm:"foreignKey:Id"`
|
Comments []Comment `gorm:"foreignKey:AssociatedPost"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,9 @@ import (
|
|||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id" gorm: "primaryKey" gorm: "unique"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Author string `json:"author"`
|
Author string `json:"author" form:"author"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content" form:"content"`
|
||||||
|
AssociatedPost string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{{ template "header.tmpl" . }}
|
{{ template "header.tmpl" . }}
|
||||||
<div>
|
<div>
|
||||||
<h3>{{ .Title }}</h3>
|
<h2>{{ .Title }}</h2>
|
||||||
<div>{{ .Content }}</div>
|
<div>{{ .Content }}</div>
|
||||||
|
<h4>Comments</h4>
|
||||||
{{ range $comment := .Comments }}
|
{{ range $comment := .Comments }}
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<div>
|
<div>
|
||||||
@@ -14,9 +15,10 @@
|
|||||||
{{ else }}
|
{{ else }}
|
||||||
<p>No comments...</p>
|
<p>No comments...</p>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
<h4>Leave a comment!</h4>
|
||||||
<form method="POST" action="">
|
<form method="POST" action="">
|
||||||
<input type="text" placeholder="Name" name="author"/>
|
<input type="text" placeholder="Name" name="author"/>
|
||||||
<input type="field" name="content" />
|
<input type="field" placeholder="Comment" name="content" />
|
||||||
<input type="text" style="display: none;" value="{{ .Url }}" readonly />
|
<input type="text" style="display: none;" value="{{ .Url }}" readonly />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user