feat(repository): enhance repository methods with batch processing and transaction support
- Updated Like and Unlike methods in CommentRepository to use transactions for data consistency. - Introduced IsLikedBatch method for batch checking if comments are liked, addressing N+1 query issues. - Enhanced BatchDelete method in PostRepository to perform batch deletions within a single transaction, improving efficiency. - Added BatchUpdateStatus method for posts to utilize a single SQL update for status changes, reducing database load. - Implemented BatchUpdateSortOrder in StickerRepository using CASE WHEN for efficient sorting updates. - Added IsFollowingBatch and IsBlockedBatch methods in UserRepository for batch checking follow and block relationships, optimizing performance.
This commit is contained in:
@@ -140,6 +140,36 @@ func (r *UserRepository) IsFollowing(followerID, followingID string) (bool, erro
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// IsFollowingBatch 批量检查当前用户是否关注了目标用户(解决 N+1 问题)
|
||||
// 返回 map[targetUserID]bool
|
||||
func (r *UserRepository) IsFollowingBatch(followerID string, targetUserIDs []string) (map[string]bool, error) {
|
||||
result := make(map[string]bool)
|
||||
if len(targetUserIDs) == 0 || followerID == "" {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 初始化所有目标用户为 false
|
||||
for _, id := range targetUserIDs {
|
||||
result[id] = false
|
||||
}
|
||||
|
||||
// 一次性查询所有关注记录
|
||||
var followingIDs []string
|
||||
err := r.db.Model(&model.Follow{}).
|
||||
Where("follower_id = ? AND following_id IN ?", followerID, targetUserIDs).
|
||||
Pluck("following_id", &followingIDs).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 更新已关注的用户
|
||||
for _, id := range followingIDs {
|
||||
result[id] = true
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// IncrementFollowersCount 增加用户粉丝数
|
||||
func (r *UserRepository) IncrementFollowersCount(userID string) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ?", userID).
|
||||
@@ -243,6 +273,36 @@ func (r *UserRepository) IsBlocked(blockerID, blockedID string) (bool, error) {
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// IsBlockedBatch 批量检查拉黑关系(解决 N+1 问题)
|
||||
// 返回 map[blockedUserID]bool
|
||||
func (r *UserRepository) IsBlockedBatch(blockerID string, blockedIDs []string) (map[string]bool, error) {
|
||||
result := make(map[string]bool)
|
||||
if len(blockedIDs) == 0 || blockerID == "" {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 初始化所有目标用户为 false
|
||||
for _, id := range blockedIDs {
|
||||
result[id] = false
|
||||
}
|
||||
|
||||
// 一次性查询所有拉黑记录
|
||||
var blockedUserIDs []string
|
||||
err := r.db.Model(&model.UserBlock{}).
|
||||
Where("blocker_id = ? AND blocked_id IN ?", blockerID, blockedIDs).
|
||||
Pluck("blocked_id", &blockedUserIDs).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 更新已拉黑的用户
|
||||
for _, id := range blockedUserIDs {
|
||||
result[id] = true
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// IsBlockedEitherDirection 检查是否任一方向存在拉黑
|
||||
func (r *UserRepository) IsBlockedEitherDirection(userA, userB string) (bool, error) {
|
||||
var count int64
|
||||
|
||||
Reference in New Issue
Block a user