feat: 实现内容系统 MVC(Post CRUD + 审核流转)
按 content-system.md 设计文档,实现帖子内容系统的最小可行 MVC。 新增: - Model: Post 模型,含 5 种状态(draft/pending/approved/rejected/locked) - Repository: post_repo.go,分页查询(前台 approved 列表 + 后台全状态筛选) - Service: post_service.go,核心业务逻辑(状态流转、Markdown 渲染 Goldmark) - Controller: post_controller + admin_post_controller,SSR 页面 6 个 + API 13 个 - Template: 列表/详情/编辑器/404 + 管理员列表,各配 CSS/JS - 路由: /posts, /posts/:id, /posts/new, /posts/:id/edit, REST API, Admin API - gomod: + github.com/yuin/goldmark v1.8.2 修改: - Main: AutoMigrate Post 表 - Router: deps 注册 PostService/Controller,路由注册 - Nav: 新增“帖子”导航链接 - Admin Sidebar: 新增“内容管理”入口 设计: - 审核开关复用 audit.enabled,开启时帖子为 draft,关闭后直接 approved - 仅 approved 公开可见,作者/admin 可预览非公开状态帖子 - 严格分层 ISP 接口: Controller → postUseCase, Service → postStore, Repo → *PostRepo - 状态保护: pending/locked 禁止编辑,rejected 编辑后自动重置为 draft
This commit is contained in:
76
internal/model/post.go
Normal file
76
internal/model/post.go
Normal file
@ -0,0 +1,76 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Post 帖子/文章模型
|
||||
type Post struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Title string `gorm:"type:varchar(200);not null" json:"title"`
|
||||
Body string `gorm:"type:text" json:"body"`
|
||||
BodyHTML string `gorm:"type:text" json:"body_html"`
|
||||
UserID uint `gorm:"index;not null" json:"user_id"`
|
||||
Status string `gorm:"type:varchar(20);index;default:draft;not null" json:"status"`
|
||||
RejectReason string `gorm:"type:varchar(500);default:''" json:"reject_reason,omitempty"`
|
||||
AllowComment bool `gorm:"default:true" json:"allow_comment"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// 非数据库字段(联表查询填充)
|
||||
AuthorName string `gorm:"-:all" json:"author_name,omitempty"`
|
||||
}
|
||||
|
||||
// 帖子状态常量
|
||||
const (
|
||||
PostStatusDraft = "draft"
|
||||
PostStatusPending = "pending"
|
||||
PostStatusApproved = "approved"
|
||||
PostStatusRejected = "rejected"
|
||||
PostStatusLocked = "locked"
|
||||
)
|
||||
|
||||
// PostStatusDisplayNames 状态 → 中文名
|
||||
var PostStatusDisplayNames = map[string]string{
|
||||
PostStatusDraft: "草稿",
|
||||
PostStatusPending: "待审核",
|
||||
PostStatusApproved: "已发布",
|
||||
PostStatusRejected: "已退回",
|
||||
PostStatusLocked: "已锁定",
|
||||
}
|
||||
|
||||
// PostListResult 帖子列表查询结果
|
||||
type PostListResult struct {
|
||||
Items []Post `json:"items"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
}
|
||||
|
||||
// PostCreateRequest 发帖请求
|
||||
type PostCreateRequest struct {
|
||||
Title string `json:"title" binding:"required,min=1,max=200"`
|
||||
Body string `json:"body" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// PostUpdateRequest 编辑请求
|
||||
type PostUpdateRequest struct {
|
||||
Title string `json:"title" binding:"required,min=1,max=200"`
|
||||
Body string `json:"body" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// PostRejectRequest 退回请求
|
||||
type PostRejectRequest struct {
|
||||
Reason string `json:"reason" binding:"required,min=1,max=500"`
|
||||
}
|
||||
|
||||
// PostListQuery 列表查询参数
|
||||
type PostListQuery struct {
|
||||
Keyword string `form:"keyword"`
|
||||
Status string `form:"status"`
|
||||
Page int `form:"page"`
|
||||
PageSize int `form:"page_size"`
|
||||
}
|
||||
Reference in New Issue
Block a user