2025-04-09 16:56:05 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-15 16:47:00 +08:00
|
|
|
"go_blog/models"
|
2025-04-09 16:56:05 +08:00
|
|
|
"net/http"
|
2025-04-15 16:47:00 +08:00
|
|
|
"strconv"
|
2025-04-09 16:56:05 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-02-12 13:48:35 +08:00
|
|
|
"gorm.io/gorm"
|
2025-04-09 16:56:05 +08:00
|
|
|
)
|
|
|
|
|
|
2026-02-12 13:48:35 +08:00
|
|
|
// ShowPost 显示文章详情
|
2025-04-09 16:56:05 +08:00
|
|
|
func ShowPost(c *gin.Context) {
|
2026-02-12 13:48:35 +08:00
|
|
|
themeManager, ok := getThemeManager(c)
|
2025-05-27 19:01:05 +08:00
|
|
|
if !ok {
|
2026-02-12 13:48:35 +08:00
|
|
|
c.String(http.StatusInternalServerError, "Theme manager not found")
|
2025-05-27 19:01:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-04-15 16:47:00 +08:00
|
|
|
|
2025-05-27 19:01:05 +08:00
|
|
|
idParam := c.Param("id")
|
|
|
|
|
id, err := strconv.ParseInt(idParam, 10, 32)
|
2025-04-15 16:47:00 +08:00
|
|
|
if err != nil {
|
2025-05-27 19:01:05 +08:00
|
|
|
c.String(http.StatusBadRequest, "Invalid post ID format")
|
2025-04-15 16:47:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-05-27 19:01:05 +08:00
|
|
|
|
2026-02-12 13:48:35 +08:00
|
|
|
db, ok := getDB(c)
|
2025-05-27 19:01:05 +08:00
|
|
|
if !ok {
|
2026-02-12 13:48:35 +08:00
|
|
|
c.String(http.StatusInternalServerError, "DB not found")
|
2025-05-27 19:01:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 13:48:35 +08:00
|
|
|
var content = models.Content{Cid: int32(id)}
|
2025-05-27 19:01:05 +08:00
|
|
|
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 {
|
2026-02-12 13:48:35 +08:00
|
|
|
c.String(http.StatusInternalServerError, "Template 'post' not found")
|
2025-05-27 19:01:05 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Header("Content-Type", "text/html; charset=utf-8")
|
2026-02-12 13:48:35 +08:00
|
|
|
tpl.Execute(c.Writer, content)
|
2025-04-09 16:56:05 +08:00
|
|
|
}
|