refactor: update repository interfaces and improve dependency injection
- Refactored repository structures to use interfaces instead of concrete types, enhancing flexibility and testability. - Updated various repository methods to accept interfaces, allowing for better dependency management. - Modified wire generation to accommodate new repository interfaces, ensuring proper service injection throughout the application. - Enhanced handler methods to utilize DTOs for filtering and data handling, improving code clarity and maintainability.
This commit is contained in:
@@ -8,23 +8,47 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CommentRepository 评论仓储
|
||||
type CommentRepository struct {
|
||||
// CommentRepository 评论仓储接口
|
||||
type CommentRepository interface {
|
||||
Create(comment *model.Comment) error
|
||||
GetByID(id string) (*model.Comment, error)
|
||||
Update(comment *model.Comment) error
|
||||
UpdateModerationStatus(commentID string, status model.CommentStatus) error
|
||||
Delete(id string) error
|
||||
ApplyPublishedStats(comment *model.Comment) error
|
||||
GetByPostID(postID string, page, pageSize int) ([]*model.Comment, int64, error)
|
||||
GetByPostIDWithReplies(postID string, page, pageSize, replyLimit int) ([]*model.Comment, int64, error)
|
||||
GetRepliesByRootID(rootID string, page, pageSize int) ([]*model.Comment, int64, error)
|
||||
GetReplies(parentID string) ([]*model.Comment, error)
|
||||
Like(commentID, userID string) error
|
||||
Unlike(commentID, userID string) error
|
||||
IsLiked(commentID, userID string) bool
|
||||
IsLikedBatch(commentIDs []string, userID string) map[string]bool
|
||||
GetAdminCommentList(filter AdminCommentListFilter) ([]*model.Comment, int64, error)
|
||||
GetAdminCommentByID(id string) (*model.Comment, error)
|
||||
BatchDelete(ids []string) ([]string, error)
|
||||
GetCommentsByPostIDForAdmin(postID string, page, pageSize int) ([]*model.Comment, int64, error)
|
||||
GetCommentsByCursor(ctx context.Context, postID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Comment], error)
|
||||
GetRepliesByCursor(ctx context.Context, rootID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Comment], error)
|
||||
}
|
||||
|
||||
// commentRepository 评论仓储实现
|
||||
type commentRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewCommentRepository 创建评论仓储
|
||||
func NewCommentRepository(db *gorm.DB) *CommentRepository {
|
||||
return &CommentRepository{db: db}
|
||||
func NewCommentRepository(db *gorm.DB) CommentRepository {
|
||||
return &commentRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 创建评论
|
||||
func (r *CommentRepository) Create(comment *model.Comment) error {
|
||||
func (r *commentRepository) Create(comment *model.Comment) error {
|
||||
return r.db.Create(comment).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取评论
|
||||
func (r *CommentRepository) GetByID(id string) (*model.Comment, error) {
|
||||
func (r *commentRepository) GetByID(id string) (*model.Comment, error) {
|
||||
var comment model.Comment
|
||||
err := r.db.Preload("User").First(&comment, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -34,12 +58,12 @@ func (r *CommentRepository) GetByID(id string) (*model.Comment, error) {
|
||||
}
|
||||
|
||||
// Update 更新评论
|
||||
func (r *CommentRepository) Update(comment *model.Comment) error {
|
||||
func (r *commentRepository) Update(comment *model.Comment) error {
|
||||
return r.db.Save(comment).Error
|
||||
}
|
||||
|
||||
// UpdateModerationStatus 更新评论审核状态
|
||||
func (r *CommentRepository) UpdateModerationStatus(commentID string, status model.CommentStatus) error {
|
||||
func (r *commentRepository) UpdateModerationStatus(commentID string, status model.CommentStatus) error {
|
||||
// 审核状态更新属于管理操作,不应影响评论内容更新时间(updated_at)
|
||||
return r.db.Model(&model.Comment{}).
|
||||
Where("id = ?", commentID).
|
||||
@@ -47,7 +71,7 @@ func (r *CommentRepository) UpdateModerationStatus(commentID string, status mode
|
||||
}
|
||||
|
||||
// Delete 删除评论(软删除,同时清理关联数据)
|
||||
func (r *CommentRepository) Delete(id string) error {
|
||||
func (r *commentRepository) Delete(id string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 先查询评论获取post_id和parent_id
|
||||
var comment model.Comment
|
||||
@@ -91,7 +115,7 @@ func (r *CommentRepository) Delete(id string) error {
|
||||
}
|
||||
|
||||
// ApplyPublishedStats 在评论审核通过后更新帖子评论数/回复数
|
||||
func (r *CommentRepository) ApplyPublishedStats(comment *model.Comment) error {
|
||||
func (r *commentRepository) ApplyPublishedStats(comment *model.Comment) error {
|
||||
if comment == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -118,7 +142,7 @@ func (r *CommentRepository) ApplyPublishedStats(comment *model.Comment) error {
|
||||
}
|
||||
|
||||
// GetByPostID 获取帖子评论
|
||||
func (r *CommentRepository) GetByPostID(postID string, page, pageSize int) ([]*model.Comment, int64, error) {
|
||||
func (r *commentRepository) GetByPostID(postID string, page, pageSize int) ([]*model.Comment, int64, error) {
|
||||
var comments []*model.Comment
|
||||
var total int64
|
||||
|
||||
@@ -136,7 +160,7 @@ func (r *CommentRepository) GetByPostID(postID string, page, pageSize int) ([]*m
|
||||
|
||||
// GetByPostIDWithReplies 获取帖子评论(包含回复,扁平化结构)
|
||||
// 所有层级的回复都扁平化展示在顶级评论的 replies 中
|
||||
func (r *CommentRepository) GetByPostIDWithReplies(postID string, page, pageSize, replyLimit int) ([]*model.Comment, int64, error) {
|
||||
func (r *commentRepository) GetByPostIDWithReplies(postID string, page, pageSize, replyLimit int) ([]*model.Comment, int64, error) {
|
||||
var comments []*model.Comment
|
||||
var total int64
|
||||
|
||||
@@ -212,7 +236,7 @@ func (r *CommentRepository) GetByPostIDWithReplies(postID string, page, pageSize
|
||||
}
|
||||
|
||||
// loadFlatReplies 加载评论的所有回复(扁平化,所有层级都在同一层)
|
||||
func (r *CommentRepository) loadFlatReplies(rootComment *model.Comment, limit int) {
|
||||
func (r *commentRepository) loadFlatReplies(rootComment *model.Comment, limit int) {
|
||||
var allReplies []*model.Comment
|
||||
|
||||
// 查询所有以该评论为根评论的回复(不包括顶级评论本身)
|
||||
@@ -226,7 +250,7 @@ func (r *CommentRepository) loadFlatReplies(rootComment *model.Comment, limit in
|
||||
}
|
||||
|
||||
// GetRepliesByRootID 根据根评论ID分页获取回复(扁平化)
|
||||
func (r *CommentRepository) GetRepliesByRootID(rootID string, page, pageSize int) ([]*model.Comment, int64, error) {
|
||||
func (r *commentRepository) GetRepliesByRootID(rootID string, page, pageSize int) ([]*model.Comment, int64, error) {
|
||||
var replies []*model.Comment
|
||||
var total int64
|
||||
|
||||
@@ -246,7 +270,7 @@ func (r *CommentRepository) GetRepliesByRootID(rootID string, page, pageSize int
|
||||
}
|
||||
|
||||
// GetReplies 获取回复
|
||||
func (r *CommentRepository) GetReplies(parentID string) ([]*model.Comment, error) {
|
||||
func (r *commentRepository) GetReplies(parentID string) ([]*model.Comment, error) {
|
||||
var comments []*model.Comment
|
||||
err := r.db.Where("parent_id = ? AND status = ?", parentID, model.CommentStatusPublished).
|
||||
Preload("User").
|
||||
@@ -256,7 +280,7 @@ func (r *CommentRepository) GetReplies(parentID string) ([]*model.Comment, error
|
||||
}
|
||||
|
||||
// Like 点赞评论(使用事务保证数据一致性)
|
||||
func (r *CommentRepository) Like(commentID, userID string) error {
|
||||
func (r *commentRepository) Like(commentID, userID string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 检查是否已经点赞
|
||||
var existing model.CommentLike
|
||||
@@ -285,7 +309,7 @@ func (r *CommentRepository) Like(commentID, userID string) error {
|
||||
}
|
||||
|
||||
// Unlike 取消点赞评论(使用事务保证数据一致性)
|
||||
func (r *CommentRepository) Unlike(commentID, userID string) error {
|
||||
func (r *commentRepository) Unlike(commentID, userID string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Where("comment_id = ? AND user_id = ?", commentID, userID).Delete(&model.CommentLike{})
|
||||
if result.Error != nil {
|
||||
@@ -301,7 +325,7 @@ func (r *CommentRepository) Unlike(commentID, userID string) error {
|
||||
}
|
||||
|
||||
// IsLiked 检查是否已点赞
|
||||
func (r *CommentRepository) IsLiked(commentID, userID string) bool {
|
||||
func (r *commentRepository) IsLiked(commentID, userID string) bool {
|
||||
var count int64
|
||||
r.db.Model(&model.CommentLike{}).Where("comment_id = ? AND user_id = ?", commentID, userID).Count(&count)
|
||||
return count > 0
|
||||
@@ -309,7 +333,7 @@ func (r *CommentRepository) IsLiked(commentID, userID string) bool {
|
||||
|
||||
// IsLikedBatch 批量检查是否点赞(解决 N+1 问题)
|
||||
// 返回 map[commentID]bool
|
||||
func (r *CommentRepository) IsLikedBatch(commentIDs []string, userID string) map[string]bool {
|
||||
func (r *commentRepository) IsLikedBatch(commentIDs []string, userID string) map[string]bool {
|
||||
result := make(map[string]bool)
|
||||
if len(commentIDs) == 0 || userID == "" {
|
||||
return result
|
||||
@@ -347,7 +371,7 @@ type AdminCommentListFilter struct {
|
||||
}
|
||||
|
||||
// GetAdminCommentList 获取管理端评论列表
|
||||
func (r *CommentRepository) GetAdminCommentList(filter AdminCommentListFilter) ([]*model.Comment, int64, error) {
|
||||
func (r *commentRepository) GetAdminCommentList(filter AdminCommentListFilter) ([]*model.Comment, int64, error) {
|
||||
var comments []*model.Comment
|
||||
var total int64
|
||||
|
||||
@@ -388,7 +412,7 @@ func (r *CommentRepository) GetAdminCommentList(filter AdminCommentListFilter) (
|
||||
}
|
||||
|
||||
// GetAdminCommentByID 获取管理端评论详情(包含关联信息)
|
||||
func (r *CommentRepository) GetAdminCommentByID(id string) (*model.Comment, error) {
|
||||
func (r *commentRepository) GetAdminCommentByID(id string) (*model.Comment, error) {
|
||||
var comment model.Comment
|
||||
err := r.db.Preload("User").First(&comment, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -398,7 +422,7 @@ func (r *CommentRepository) GetAdminCommentByID(id string) (*model.Comment, erro
|
||||
}
|
||||
|
||||
// BatchDelete 批量删除评论(使用单次事务批量删除,避免 N+1 问题)
|
||||
func (r *CommentRepository) BatchDelete(ids []string) ([]string, error) {
|
||||
func (r *commentRepository) BatchDelete(ids []string) ([]string, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -452,7 +476,7 @@ func (r *CommentRepository) BatchDelete(ids []string) ([]string, error) {
|
||||
}
|
||||
|
||||
// GetCommentsByPostIDForAdmin 管理端获取帖子的评论列表(包含所有状态)
|
||||
func (r *CommentRepository) GetCommentsByPostIDForAdmin(postID string, page, pageSize int) ([]*model.Comment, int64, error) {
|
||||
func (r *commentRepository) GetCommentsByPostIDForAdmin(postID string, page, pageSize int) ([]*model.Comment, int64, error) {
|
||||
var comments []*model.Comment
|
||||
var total int64
|
||||
|
||||
@@ -472,7 +496,7 @@ func (r *CommentRepository) GetCommentsByPostIDForAdmin(postID string, page, pag
|
||||
|
||||
// GetCommentsByCursor 游标分页获取帖子评论(包含回复)
|
||||
// 排序方式:created_at DESC
|
||||
func (r *CommentRepository) GetCommentsByCursor(ctx context.Context, postID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Comment], error) {
|
||||
func (r *commentRepository) GetCommentsByCursor(ctx context.Context, postID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Comment], error) {
|
||||
// 构建基础查询 - 只查询顶级评论
|
||||
query := r.db.WithContext(ctx).Model(&model.Comment{}).
|
||||
Where("post_id = ? AND parent_id IS NULL AND status = ?", postID, model.CommentStatusPublished)
|
||||
@@ -531,7 +555,7 @@ func (r *CommentRepository) GetCommentsByCursor(ctx context.Context, postID stri
|
||||
}
|
||||
|
||||
// loadRepliesForComments 为评论列表加载回复
|
||||
func (r *CommentRepository) loadRepliesForComments(comments []*model.Comment, replyLimit int) {
|
||||
func (r *commentRepository) loadRepliesForComments(comments []*model.Comment, replyLimit int) {
|
||||
if len(comments) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -593,7 +617,7 @@ func (r *CommentRepository) loadRepliesForComments(comments []*model.Comment, re
|
||||
|
||||
// GetRepliesByCursor 游标分页获取根评论的回复
|
||||
// 排序方式:created_at ASC(回复按时间正序)
|
||||
func (r *CommentRepository) GetRepliesByCursor(ctx context.Context, rootID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Comment], error) {
|
||||
func (r *commentRepository) GetRepliesByCursor(ctx context.Context, rootID string, req *cursor.PageRequest) (*cursor.CursorPageResult[*model.Comment], error) {
|
||||
// 构建基础查询
|
||||
query := r.db.WithContext(ctx).Model(&model.Comment{}).
|
||||
Where("root_id = ? AND status = ?", rootID, model.CommentStatusPublished)
|
||||
|
||||
Reference in New Issue
Block a user