5abc705eae
- web/: 产品官网(Hero 事件流终端、功能矩阵、架构、快速开始、下载、博客 + Markdown 详情页),青瓷绿双主题 - admin/: 内容管理(JWT 登录、文章分页/搜索/CRUD、草稿与发布),embed 挂 /admin - server/: Gin + GORM,MySQL(DSN 走 .env,SQLite 兜底);规范落地:sundynix_ 表前缀、字符串雪花主键、snake_case 列名、统一响应信封、公共分页参数 - 双 SPA embed 单二进制部署,make build 一键出包 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
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)
|
|
}
|