使用qoder优化后更新
Some checks failed
Go build / build and run (push) Has been cancelled

This commit is contained in:
2026-02-12 13:48:35 +08:00
parent abd5962ae9
commit bc59f616fd
15 changed files with 130 additions and 278 deletions

View File

@@ -2,23 +2,18 @@ package controllers
import (
"go_blog/models"
"go_blog/utils"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm" // <-- 确保导入 gorm
"gorm.io/gorm"
)
// ShowPost 显示文章详情
func ShowPost(c *gin.Context) {
tm, exists := c.Get("ThemeManager")
if !exists {
c.String(http.StatusInternalServerError, "Theme manager not found")
return
}
themeManager, ok := tm.(*utils.ThemeManager)
themeManager, ok := getThemeManager(c)
if !ok {
c.String(http.StatusInternalServerError, "Invalid theme manager type")
c.String(http.StatusInternalServerError, "Theme manager not found")
return
}
@@ -29,19 +24,13 @@ func ShowPost(c *gin.Context) {
return
}
var content = models.Content{Cid: int32(id)}
// 从 Gin 上下文中获取 DB 实例
dbInterface, exists := c.Get("DB")
if !exists {
c.String(http.StatusInternalServerError, "DB not found in context")
return
}
db, ok := dbInterface.(*gorm.DB)
db, ok := getDB(c)
if !ok {
c.String(http.StatusInternalServerError, "Invalid DB type in context")
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 {
@@ -52,19 +41,12 @@ func ShowPost(c *gin.Context) {
return
}
// 假设你的主题中有一个名为 "post" 的模板 (post.html 或 post.tmpl)
// 注意:当前的 default 主题 (web/themes/default/templates/) 并没有 post.tmpl你需要添加它。
tpl := themeManager.GetTemplate("post")
if tpl == nil {
c.String(http.StatusInternalServerError, "Template 'post' not found in current theme: "+themeManager.CurrentTheme()+". Make sure 'post.html' or 'post.tmpl' exists in the theme's templates directory.")
c.String(http.StatusInternalServerError, "Template 'post' not found")
return
}
c.Status(http.StatusOK)
c.Header("Content-Type", "text/html; charset=utf-8")
// 将整个 content 对象传递给模板。模板内部通过 {{ .Title }} {{ .Text }} 等访问。
err = tpl.Execute(c.Writer, content)
if err != nil {
c.String(http.StatusInternalServerError, "Error rendering template: "+err.Error())
}
tpl.Execute(c.Writer, content)
}