wp-go/actions/detail.go

73 lines
1.6 KiB
Go
Raw Normal View History

2022-09-18 04:34:48 +00:00
package actions
2022-09-18 14:06:27 +00:00
import (
2022-09-20 04:00:09 +00:00
"context"
2022-09-18 14:06:27 +00:00
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/models"
"net/http"
"strconv"
)
2022-09-18 04:34:48 +00:00
func Detail(c *gin.Context) {
2022-09-18 14:06:27 +00:00
var err error
2022-09-20 04:00:09 +00:00
ctx := context.TODO()
recent := common.RecentPosts(ctx)
2022-09-19 11:11:36 +00:00
archive := common.Archives()
2022-09-20 04:00:09 +00:00
categoryItems := common.Categories(ctx)
2022-09-20 13:16:51 +00:00
comments := common.RecentComments(ctx)
2022-09-19 11:11:36 +00:00
var h = gin.H{
"title": models.Options["blogname"],
"options": models.Options,
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
2022-09-20 13:16:51 +00:00
"comments": comments,
2022-09-19 11:11:36 +00:00
}
2022-09-18 14:06:27 +00:00
defer func() {
2022-09-19 04:36:51 +00:00
c.HTML(http.StatusOK, "posts/detail.gohtml", h)
2022-09-18 14:06:27 +00:00
if err != nil {
c.Error(err)
}
}()
2022-09-19 11:11:36 +00:00
id := c.Param("id")
2022-09-18 14:06:27 +00:00
Id := 0
if id != "" {
Id, err = strconv.Atoi(id)
if err != nil {
return
}
}
ID := uint64(Id)
post := common.GetPostFromCache(ID)
if post.Id == 0 {
er := common.QueryAndSetPostCache([]models.WpPosts{{Id: ID}})
if er != nil {
err = er
return
}
post = common.GetPostFromCache(ID)
if post.Id == 0 {
return
}
}
pw := sessions.Default(c).Get("post_password")
2022-09-19 13:31:46 +00:00
showComment := false
if post.CommentCount > 0 || post.CommentStatus == "open" {
showComment = true
}
2022-09-18 14:06:27 +00:00
common.PasswordProjectTitle(&post)
if post.PostPassword != "" && pw != post.PostPassword {
common.PasswdProjectContent(&post)
showComment = false
}
2022-09-19 11:11:36 +00:00
prev, next, err := common.GetContextPost(post.Id, post.PostDate)
h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"])
h["post"] = post
2022-09-19 13:31:46 +00:00
h["showComment"] = showComment
2022-09-19 11:11:36 +00:00
h["prev"] = prev
h["next"] = next
2022-09-18 04:34:48 +00:00
}