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,60 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// UserRepository 用户仓储
|
||||
type UserRepository struct {
|
||||
// UserRepository 用户仓储接口
|
||||
type UserRepository interface {
|
||||
Create(user *model.User) error
|
||||
GetByID(id string) (*model.User, error)
|
||||
GetByUsername(username string) (*model.User, error)
|
||||
GetByEmail(email string) (*model.User, error)
|
||||
GetByPhone(phone string) (*model.User, error)
|
||||
Update(user *model.User) error
|
||||
Delete(id string) error
|
||||
List(page, pageSize int) ([]*model.User, int64, error)
|
||||
GetFollowers(userID string, page, pageSize int) ([]*model.User, int64, error)
|
||||
GetFollowing(userID string, page, pageSize int) ([]*model.User, int64, error)
|
||||
CreateFollow(follow *model.Follow) error
|
||||
DeleteFollow(followerID, followingID string) error
|
||||
IsFollowing(followerID, followingID string) (bool, error)
|
||||
IsFollowingBatch(followerID string, targetUserIDs []string) (map[string]bool, error)
|
||||
IncrementFollowersCount(userID string) error
|
||||
DecrementFollowersCount(userID string) error
|
||||
IncrementFollowingCount(userID string) error
|
||||
DecrementFollowingCount(userID string) error
|
||||
RefreshFollowersCount(userID string) error
|
||||
GetPostsCount(userID string) (int64, error)
|
||||
GetPostsCountBatch(userIDs []string) (map[string]int64, error)
|
||||
RefreshFollowingCount(userID string) error
|
||||
IsBlocked(blockerID, blockedID string) (bool, error)
|
||||
IsBlockedBatch(blockerID string, blockedIDs []string) (map[string]bool, error)
|
||||
IsBlockedEitherDirection(userA, userB string) (bool, error)
|
||||
BlockUserAndCleanupRelations(blockerID, blockedID string) error
|
||||
UnblockUser(blockerID, blockedID string) error
|
||||
GetBlockedUsers(blockerID string, page, pageSize int) ([]*model.User, int64, error)
|
||||
Search(keyword string, page, pageSize int) ([]*model.User, int64, error)
|
||||
GetMutualFollowStatus(currentUserID string, targetUserIDs []string) (map[string][2]bool, error)
|
||||
AdminList(page, pageSize int, keyword, status string) ([]*model.User, int64, error)
|
||||
GetCommentsCount(userID string) (int64, error)
|
||||
UpdateStatus(userID string, status model.UserStatus) error
|
||||
}
|
||||
|
||||
// userRepository 用户仓储实现
|
||||
type userRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewUserRepository 创建用户仓储
|
||||
func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
func NewUserRepository(db *gorm.DB) UserRepository {
|
||||
return &userRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 创建用户
|
||||
func (r *UserRepository) Create(user *model.User) error {
|
||||
func (r *userRepository) Create(user *model.User) error {
|
||||
return r.db.Create(user).Error
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取用户
|
||||
func (r *UserRepository) GetByID(id string) (*model.User, error) {
|
||||
func (r *userRepository) GetByID(id string) (*model.User, error) {
|
||||
var user model.User
|
||||
err := r.db.First(&user, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -34,7 +71,7 @@ func (r *UserRepository) GetByID(id string) (*model.User, error) {
|
||||
}
|
||||
|
||||
// GetByUsername 根据用户名获取用户
|
||||
func (r *UserRepository) GetByUsername(username string) (*model.User, error) {
|
||||
func (r *userRepository) GetByUsername(username string) (*model.User, error) {
|
||||
var user model.User
|
||||
err := r.db.First(&user, "username = ?", username).Error
|
||||
if err != nil {
|
||||
@@ -44,7 +81,7 @@ func (r *UserRepository) GetByUsername(username string) (*model.User, error) {
|
||||
}
|
||||
|
||||
// GetByEmail 根据邮箱获取用户
|
||||
func (r *UserRepository) GetByEmail(email string) (*model.User, error) {
|
||||
func (r *userRepository) GetByEmail(email string) (*model.User, error) {
|
||||
var user model.User
|
||||
err := r.db.First(&user, "email = ?", email).Error
|
||||
if err != nil {
|
||||
@@ -54,7 +91,7 @@ func (r *UserRepository) GetByEmail(email string) (*model.User, error) {
|
||||
}
|
||||
|
||||
// GetByPhone 根据手机号获取用户
|
||||
func (r *UserRepository) GetByPhone(phone string) (*model.User, error) {
|
||||
func (r *userRepository) GetByPhone(phone string) (*model.User, error) {
|
||||
var user model.User
|
||||
err := r.db.First(&user, "phone = ?", phone).Error
|
||||
if err != nil {
|
||||
@@ -64,17 +101,17 @@ func (r *UserRepository) GetByPhone(phone string) (*model.User, error) {
|
||||
}
|
||||
|
||||
// Update 更新用户
|
||||
func (r *UserRepository) Update(user *model.User) error {
|
||||
func (r *userRepository) Update(user *model.User) error {
|
||||
return r.db.Save(user).Error
|
||||
}
|
||||
|
||||
// Delete 删除用户
|
||||
func (r *UserRepository) Delete(id string) error {
|
||||
func (r *userRepository) Delete(id string) error {
|
||||
return r.db.Delete(&model.User{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// List 分页获取用户列表
|
||||
func (r *UserRepository) List(page, pageSize int) ([]*model.User, int64, error) {
|
||||
func (r *userRepository) List(page, pageSize int) ([]*model.User, int64, error) {
|
||||
var users []*model.User
|
||||
var total int64
|
||||
|
||||
@@ -87,7 +124,7 @@ func (r *UserRepository) List(page, pageSize int) ([]*model.User, int64, error)
|
||||
}
|
||||
|
||||
// GetFollowers 获取用户粉丝
|
||||
func (r *UserRepository) GetFollowers(userID string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
func (r *userRepository) GetFollowers(userID string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
var users []*model.User
|
||||
var total int64
|
||||
|
||||
@@ -104,7 +141,7 @@ func (r *UserRepository) GetFollowers(userID string, page, pageSize int) ([]*mod
|
||||
}
|
||||
|
||||
// GetFollowing 获取用户关注
|
||||
func (r *UserRepository) GetFollowing(userID string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
func (r *userRepository) GetFollowing(userID string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
var users []*model.User
|
||||
var total int64
|
||||
|
||||
@@ -121,17 +158,17 @@ func (r *UserRepository) GetFollowing(userID string, page, pageSize int) ([]*mod
|
||||
}
|
||||
|
||||
// CreateFollow 创建关注关系
|
||||
func (r *UserRepository) CreateFollow(follow *model.Follow) error {
|
||||
func (r *userRepository) CreateFollow(follow *model.Follow) error {
|
||||
return r.db.Create(follow).Error
|
||||
}
|
||||
|
||||
// DeleteFollow 删除关注关系
|
||||
func (r *UserRepository) DeleteFollow(followerID, followingID string) error {
|
||||
func (r *userRepository) DeleteFollow(followerID, followingID string) error {
|
||||
return r.db.Where("follower_id = ? AND following_id = ?", followerID, followingID).Delete(&model.Follow{}).Error
|
||||
}
|
||||
|
||||
// IsFollowing 检查是否关注了某用户
|
||||
func (r *UserRepository) IsFollowing(followerID, followingID string) (bool, error) {
|
||||
func (r *userRepository) IsFollowing(followerID, followingID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.Follow{}).Where("follower_id = ? AND following_id = ?", followerID, followingID).Count(&count).Error
|
||||
if err != nil {
|
||||
@@ -142,7 +179,7 @@ func (r *UserRepository) IsFollowing(followerID, followingID string) (bool, erro
|
||||
|
||||
// IsFollowingBatch 批量检查当前用户是否关注了目标用户(解决 N+1 问题)
|
||||
// 返回 map[targetUserID]bool
|
||||
func (r *UserRepository) IsFollowingBatch(followerID string, targetUserIDs []string) (map[string]bool, error) {
|
||||
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
|
||||
@@ -171,31 +208,31 @@ func (r *UserRepository) IsFollowingBatch(followerID string, targetUserIDs []str
|
||||
}
|
||||
|
||||
// IncrementFollowersCount 增加用户粉丝数
|
||||
func (r *UserRepository) IncrementFollowersCount(userID string) error {
|
||||
func (r *userRepository) IncrementFollowersCount(userID string) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ?", userID).
|
||||
UpdateColumn("followers_count", gorm.Expr("followers_count + 1")).Error
|
||||
}
|
||||
|
||||
// DecrementFollowersCount 减少用户粉丝数
|
||||
func (r *UserRepository) DecrementFollowersCount(userID string) error {
|
||||
func (r *userRepository) DecrementFollowersCount(userID string) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ? AND followers_count > 0", userID).
|
||||
UpdateColumn("followers_count", gorm.Expr("followers_count - 1")).Error
|
||||
}
|
||||
|
||||
// IncrementFollowingCount 增加用户关注数
|
||||
func (r *UserRepository) IncrementFollowingCount(userID string) error {
|
||||
func (r *userRepository) IncrementFollowingCount(userID string) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ?", userID).
|
||||
UpdateColumn("following_count", gorm.Expr("following_count + 1")).Error
|
||||
}
|
||||
|
||||
// DecrementFollowingCount 减少用户关注数
|
||||
func (r *UserRepository) DecrementFollowingCount(userID string) error {
|
||||
func (r *userRepository) DecrementFollowingCount(userID string) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ? AND following_count > 0", userID).
|
||||
UpdateColumn("following_count", gorm.Expr("following_count - 1")).Error
|
||||
}
|
||||
|
||||
// RefreshFollowersCount 刷新用户粉丝数(通过实际计数)
|
||||
func (r *UserRepository) RefreshFollowersCount(userID string) error {
|
||||
func (r *userRepository) RefreshFollowersCount(userID string) error {
|
||||
var count int64
|
||||
err := r.db.Model(&model.Follow{}).Where("following_id = ?", userID).Count(&count).Error
|
||||
if err != nil {
|
||||
@@ -206,7 +243,7 @@ func (r *UserRepository) RefreshFollowersCount(userID string) error {
|
||||
}
|
||||
|
||||
// GetPostsCount 获取用户帖子数(实时计算)
|
||||
func (r *UserRepository) GetPostsCount(userID string) (int64, error) {
|
||||
func (r *userRepository) GetPostsCount(userID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.Post{}).
|
||||
Where("user_id = ? AND status = ?", userID, model.PostStatusPublished).
|
||||
@@ -216,7 +253,7 @@ func (r *UserRepository) GetPostsCount(userID string) (int64, error) {
|
||||
|
||||
// GetPostsCountBatch 批量获取用户帖子数(实时计算)
|
||||
// 返回 map[userID]postsCount
|
||||
func (r *UserRepository) GetPostsCountBatch(userIDs []string) (map[string]int64, error) {
|
||||
func (r *userRepository) GetPostsCountBatch(userIDs []string) (map[string]int64, error) {
|
||||
result := make(map[string]int64)
|
||||
if len(userIDs) == 0 {
|
||||
return result, nil
|
||||
@@ -251,7 +288,7 @@ func (r *UserRepository) GetPostsCountBatch(userIDs []string) (map[string]int64,
|
||||
}
|
||||
|
||||
// RefreshFollowingCount 刷新用户关注数(通过实际计数)
|
||||
func (r *UserRepository) RefreshFollowingCount(userID string) error {
|
||||
func (r *userRepository) RefreshFollowingCount(userID string) error {
|
||||
var count int64
|
||||
err := r.db.Model(&model.Follow{}).Where("follower_id = ?", userID).Count(&count).Error
|
||||
if err != nil {
|
||||
@@ -262,7 +299,7 @@ func (r *UserRepository) RefreshFollowingCount(userID string) error {
|
||||
}
|
||||
|
||||
// IsBlocked 检查拉黑关系是否存在(blocker -> blocked)
|
||||
func (r *UserRepository) IsBlocked(blockerID, blockedID string) (bool, error) {
|
||||
func (r *userRepository) IsBlocked(blockerID, blockedID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.UserBlock{}).
|
||||
Where("blocker_id = ? AND blocked_id = ?", blockerID, blockedID).
|
||||
@@ -275,7 +312,7 @@ func (r *UserRepository) IsBlocked(blockerID, blockedID string) (bool, error) {
|
||||
|
||||
// IsBlockedBatch 批量检查拉黑关系(解决 N+1 问题)
|
||||
// 返回 map[blockedUserID]bool
|
||||
func (r *UserRepository) IsBlockedBatch(blockerID string, blockedIDs []string) (map[string]bool, error) {
|
||||
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
|
||||
@@ -304,7 +341,7 @@ func (r *UserRepository) IsBlockedBatch(blockerID string, blockedIDs []string) (
|
||||
}
|
||||
|
||||
// IsBlockedEitherDirection 检查是否任一方向存在拉黑
|
||||
func (r *UserRepository) IsBlockedEitherDirection(userA, userB string) (bool, error) {
|
||||
func (r *userRepository) IsBlockedEitherDirection(userA, userB string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.UserBlock{}).
|
||||
Where("(blocker_id = ? AND blocked_id = ?) OR (blocker_id = ? AND blocked_id = ?)",
|
||||
@@ -317,7 +354,7 @@ func (r *UserRepository) IsBlockedEitherDirection(userA, userB string) (bool, er
|
||||
}
|
||||
|
||||
// BlockUserAndCleanupRelations 拉黑用户并清理双向关注关系(事务)
|
||||
func (r *UserRepository) BlockUserAndCleanupRelations(blockerID, blockedID string) error {
|
||||
func (r *userRepository) BlockUserAndCleanupRelations(blockerID, blockedID string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
block := &model.UserBlock{
|
||||
BlockerID: blockerID,
|
||||
@@ -364,13 +401,13 @@ func (r *UserRepository) BlockUserAndCleanupRelations(blockerID, blockedID strin
|
||||
}
|
||||
|
||||
// UnblockUser 取消拉黑
|
||||
func (r *UserRepository) UnblockUser(blockerID, blockedID string) error {
|
||||
func (r *userRepository) UnblockUser(blockerID, blockedID string) error {
|
||||
return r.db.Where("blocker_id = ? AND blocked_id = ?", blockerID, blockedID).
|
||||
Delete(&model.UserBlock{}).Error
|
||||
}
|
||||
|
||||
// GetBlockedUsers 获取用户黑名单列表
|
||||
func (r *UserRepository) GetBlockedUsers(blockerID string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
func (r *userRepository) GetBlockedUsers(blockerID string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
var users []*model.User
|
||||
var total int64
|
||||
|
||||
@@ -388,7 +425,7 @@ func (r *UserRepository) GetBlockedUsers(blockerID string, page, pageSize int) (
|
||||
}
|
||||
|
||||
// Search 搜索用户
|
||||
func (r *UserRepository) Search(keyword string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
func (r *userRepository) Search(keyword string, page, pageSize int) ([]*model.User, int64, error) {
|
||||
var users []*model.User
|
||||
var total int64
|
||||
|
||||
@@ -421,7 +458,7 @@ func (r *UserRepository) Search(keyword string, page, pageSize int) ([]*model.Us
|
||||
|
||||
// GetMutualFollowStatus 批量获取双向关注状态
|
||||
// 返回 map[userID][isFollowing, isFollowingMe]
|
||||
func (r *UserRepository) GetMutualFollowStatus(currentUserID string, targetUserIDs []string) (map[string][2]bool, error) {
|
||||
func (r *userRepository) GetMutualFollowStatus(currentUserID string, targetUserIDs []string) (map[string][2]bool, error) {
|
||||
result := make(map[string][2]bool)
|
||||
|
||||
if len(targetUserIDs) == 0 {
|
||||
@@ -465,7 +502,7 @@ func (r *UserRepository) GetMutualFollowStatus(currentUserID string, targetUserI
|
||||
}
|
||||
|
||||
// AdminList 管理端分页获取用户列表(支持关键词和状态筛选)
|
||||
func (r *UserRepository) AdminList(page, pageSize int, keyword, status string) ([]*model.User, int64, error) {
|
||||
func (r *userRepository) AdminList(page, pageSize int, keyword, status string) ([]*model.User, int64, error) {
|
||||
var users []*model.User
|
||||
var total int64
|
||||
|
||||
@@ -501,7 +538,7 @@ func (r *UserRepository) AdminList(page, pageSize int, keyword, status string) (
|
||||
}
|
||||
|
||||
// GetCommentsCount 获取用户评论数
|
||||
func (r *UserRepository) GetCommentsCount(userID string) (int64, error) {
|
||||
func (r *userRepository) GetCommentsCount(userID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.Comment{}).
|
||||
Where("user_id = ?", userID).
|
||||
@@ -510,6 +547,6 @@ func (r *UserRepository) GetCommentsCount(userID string) (int64, error) {
|
||||
}
|
||||
|
||||
// UpdateStatus 更新用户状态
|
||||
func (r *UserRepository) UpdateStatus(userID string, status model.UserStatus) error {
|
||||
func (r *userRepository) UpdateStatus(userID string, status model.UserStatus) error {
|
||||
return r.db.Model(&model.User{}).Where("id = ?", userID).Update("status", status).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user