package handler import ( "errors" "github.com/gin-gonic/gin" "gorm.io/gorm" "git.sundynix.cn/Blizzard/sundynix-site/server/internal/model" "git.sundynix.cn/Blizzard/sundynix-site/server/internal/pkg/resp" ) // PostHandler 面向用户端的公开文章接口。 type PostHandler struct { db *gorm.DB } func NewPostHandler(db *gorm.DB) *PostHandler { return &PostHandler{db: db} } // List GET /api/posts — 已发布文章列表(不含正文) func (h *PostHandler) List(c *gin.Context) { var items []model.PostListItem err := h.db.Model(&model.Post{}). Where("published_at IS NOT NULL"). Order("published_at DESC"). Find(&items).Error if err != nil { resp.ServerError(c, "查询失败") return } resp.OK(c, items) } // Get GET /api/posts/:slug — 文章详情(含 Markdown 正文) func (h *PostHandler) Get(c *gin.Context) { var post model.Post err := h.db. Where("slug = ? AND published_at IS NOT NULL", c.Param("slug")). First(&post).Error if errors.Is(err, gorm.ErrRecordNotFound) { resp.NotFound(c, "文章不存在") return } if err != nil { resp.ServerError(c, "查询失败") return } resp.OK(c, post) }