- 新增 StudioController,依赖 studioUseCase 接口,遵循 ISP 接口隔离 - PostRepo 新增 FindByUserIDAndStatus 方法,按用户+状态分页查询 - PostService 新增 ListByUser、GetOverview 及 StudioOverview 统计 - /studio 页面路由(概览/管理/草稿箱/写文章/数据分析)+ /api/studio API - 复用 Vditor 编辑器,studio-editor.js 对接 /api/studio/posts 端点 - 新增 studio.css(B站风格三栏布局+统计卡片+状态筛选tabs) - 模板引擎注册 add/subtract 函数,分页模板中直接使用
128 lines
3.6 KiB
Go
128 lines
3.6 KiB
Go
package repository
|
||
|
||
import (
|
||
"metazone.cc/metalab/internal/model"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// PostRepo 帖子数据访问
|
||
type PostRepo struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewPostRepo 构造函数
|
||
func NewPostRepo(db *gorm.DB) *PostRepo {
|
||
return &PostRepo{db: db}
|
||
}
|
||
|
||
// Create 创建帖子
|
||
func (r *PostRepo) Create(post *model.Post) error {
|
||
return r.db.Create(post).Error
|
||
}
|
||
|
||
// FindByID 按 ID 查找帖子
|
||
func (r *PostRepo) FindByID(id uint) (*model.Post, error) {
|
||
var post model.Post
|
||
err := r.db.First(&post, id).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &post, nil
|
||
}
|
||
|
||
// FindByIDWithAuthor 按 ID 查找帖子(含作者用户名)
|
||
func (r *PostRepo) FindByIDWithAuthor(id uint) (*model.Post, error) {
|
||
var post model.Post
|
||
err := r.db.Table("posts").
|
||
Select("posts.*, users.username as author_name").
|
||
Joins("LEFT JOIN users ON users.uid = posts.user_id").
|
||
Where("posts.id = ?", id).
|
||
First(&post).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &post, nil
|
||
}
|
||
|
||
// buildQuery 构建帖子查询公共部分(联表 + 未删除 + 关键词 + 可选状态过滤)
|
||
func (r *PostRepo) buildQuery(keyword, status string) *gorm.DB {
|
||
query := r.db.Table("posts").
|
||
Select("posts.*, users.username as author_name").
|
||
Joins("LEFT JOIN users ON users.uid = posts.user_id").
|
||
Where("posts.deleted_at IS NULL")
|
||
|
||
if keyword != "" {
|
||
like := "%" + keyword + "%"
|
||
query = query.Where("posts.title LIKE ?", like)
|
||
}
|
||
if status != "" {
|
||
query = query.Where("posts.status = ?", status)
|
||
}
|
||
return query
|
||
}
|
||
|
||
// FindPageable 分页查询帖子列表(仅 approved 状态,关键词模糊搜索标题)
|
||
func (r *PostRepo) FindPageable(keyword string, offset, limit int) ([]model.Post, int64, error) {
|
||
query := r.buildQuery(keyword, model.PostStatusApproved)
|
||
return r.pageResults(query, offset, limit)
|
||
}
|
||
|
||
// FindAdminPageable 管理后台分页查询(全状态筛选)
|
||
func (r *PostRepo) FindAdminPageable(keyword, status string, offset, limit int) ([]model.Post, int64, error) {
|
||
query := r.buildQuery(keyword, status)
|
||
return r.pageResults(query, offset, limit)
|
||
}
|
||
|
||
// FindByUserID 分页查询某用户发布的帖子(仅 approved,含作者用户名)
|
||
func (r *PostRepo) FindByUserID(userID uint, offset, limit int) ([]model.Post, int64, error) {
|
||
query := r.buildQuery("", model.PostStatusApproved).
|
||
Where("posts.user_id = ?", userID)
|
||
return r.pageResults(query, offset, limit)
|
||
}
|
||
|
||
// FindByUserIDAndStatus 分页查询某用户指定状态的帖子(空 status=全状态)
|
||
func (r *PostRepo) FindByUserIDAndStatus(userID uint, status string, offset, limit int) ([]model.Post, int64, error) {
|
||
query := r.buildQuery("", status).
|
||
Where("posts.user_id = ?", userID)
|
||
return r.pageResults(query, offset, limit)
|
||
}
|
||
|
||
// pageResults 执行 Count + Offset/Limit + ORDER BY
|
||
func (r *PostRepo) pageResults(query *gorm.DB, offset, limit int) ([]model.Post, int64, error) {
|
||
var total int64
|
||
if err := query.Count(&total).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
var posts []model.Post
|
||
err := query.Order("posts.created_at DESC").
|
||
Offset(offset).Limit(limit).Find(&posts).Error
|
||
if err != nil {
|
||
return nil, 0, err
|
||
}
|
||
return posts, total, nil
|
||
}
|
||
|
||
// Update 更新帖子
|
||
func (r *PostRepo) Update(post *model.Post) error {
|
||
return r.db.Save(post).Error
|
||
}
|
||
|
||
// SoftDelete 软删除帖子
|
||
func (r *PostRepo) SoftDelete(id uint) error {
|
||
return r.db.Delete(&model.Post{}, id).Error
|
||
}
|
||
|
||
// Restore 恢复软删除
|
||
func (r *PostRepo) Restore(id uint) error {
|
||
result := r.db.Unscoped().Model(&model.Post{}).Where("id = ?", id).Update("deleted_at", nil)
|
||
if result.Error != nil {
|
||
return result.Error
|
||
}
|
||
if result.RowsAffected == 0 {
|
||
return gorm.ErrRecordNotFound
|
||
}
|
||
return nil
|
||
}
|