Files
go_blog/controllers/showpost.go
Zhang Chao bc59f616fd
Some checks failed
Go build / build and run (push) Has been cancelled
使用qoder优化后更新
2026-02-12 13:48:35 +08:00

53 lines
1.1 KiB
Go

package controllers
import (
"go_blog/models"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// ShowPost 显示文章详情
func ShowPost(c *gin.Context) {
themeManager, ok := getThemeManager(c)
if !ok {
c.String(http.StatusInternalServerError, "Theme manager not found")
return
}
idParam := c.Param("id")
id, err := strconv.ParseInt(idParam, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Invalid post ID format")
return
}
db, ok := getDB(c)
if !ok {
c.String(http.StatusInternalServerError, "DB not found")
return
}
var content = models.Content{Cid: int32(id)}
result := db.First(&content)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
c.String(http.StatusNotFound, "Post not found")
} else {
c.String(http.StatusInternalServerError, "Error fetching post: "+result.Error.Error())
}
return
}
tpl := themeManager.GetTemplate("post")
if tpl == nil {
c.String(http.StatusInternalServerError, "Template 'post' not found")
return
}
c.Header("Content-Type", "text/html; charset=utf-8")
tpl.Execute(c.Writer, content)
}