refactor: update repository interfaces and improve dependency injection
All checks were successful
Build Backend / build (push) Successful in 12m45s
Build Backend / build-docker (push) Successful in 2m40s

- 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:
lafay
2026-03-26 18:14:16 +08:00
parent 7b41dfeb00
commit c6848aba06
50 changed files with 1034 additions and 663 deletions

View File

@@ -106,25 +106,23 @@ type GroupMembersResult struct {
// groupService 群组服务实现
type groupService struct {
db *gorm.DB
groupRepo repository.GroupRepository
userRepo *repository.UserRepository
messageRepo *repository.MessageRepository
userRepo repository.UserRepository
messageRepo repository.MessageRepository
requestRepo repository.GroupJoinRequestRepository
notifyRepo *repository.SystemNotificationRepository
notifyRepo repository.SystemNotificationRepository
sseHub *sse.Hub
cache cache.Cache
}
// NewGroupService 创建群组服务
func NewGroupService(db *gorm.DB, groupRepo repository.GroupRepository, userRepo *repository.UserRepository, messageRepo *repository.MessageRepository, sseHub *sse.Hub, cacheBackend cache.Cache) GroupService {
func NewGroupService(groupRepo repository.GroupRepository, userRepo repository.UserRepository, messageRepo repository.MessageRepository, requestRepo repository.GroupJoinRequestRepository, notifyRepo repository.SystemNotificationRepository, sseHub *sse.Hub, cacheBackend cache.Cache) GroupService {
return &groupService{
db: db,
groupRepo: groupRepo,
userRepo: userRepo,
messageRepo: messageRepo,
requestRepo: repository.NewGroupJoinRequestRepository(db),
notifyRepo: repository.NewSystemNotificationRepository(db),
requestRepo: requestRepo,
notifyRepo: notifyRepo,
sseHub: sseHub,
cache: cacheBackend,
}
@@ -263,43 +261,17 @@ func (s *groupService) CreateGroup(ownerID string, name string, description stri
GroupID: &group.ID,
}
// 在事务中创建会话和参与者
err = s.db.Transaction(func(tx *gorm.DB) error {
// 创建会话
if err := tx.Create(conversation).Error; err != nil {
return err
// 收集所有参与者
participants := make([]string, 0, len(memberIDs)+1)
participants = append(participants, ownerID)
for _, memberID := range memberIDs {
if memberID != ownerID {
participants = append(participants, memberID)
}
}
// 添加群主为会话参与者
ownerParticipant := model.ConversationParticipant{
ConversationID: conversation.ID,
UserID: ownerID,
LastReadSeq: 0,
}
if err := tx.Create(&ownerParticipant).Error; err != nil {
return err
}
// 添加被邀请的成员为会话参与者
for _, memberID := range memberIDs {
if memberID == ownerID {
continue
}
participant := model.ConversationParticipant{
ConversationID: conversation.ID,
UserID: memberID,
LastReadSeq: 0,
}
if err := tx.Create(&participant).Error; err != nil {
// 单个参与者添加失败继续处理其他成员
continue
}
}
return nil
})
if err != nil {
// 使用仓储创建会话参与者
if err := s.messageRepo.CreateConversationWithParticipants(conversation, participants); err != nil {
// 记录错误但不影响群组创建成功
}
@@ -418,30 +390,8 @@ func (s *groupService) TransferOwner(userID string, groupID string, newOwnerID s
return ErrNotGroupMember
}
// 在事务中更新
return s.db.Transaction(func(tx *gorm.DB) error {
// 更新群组的群主
group.OwnerID = newOwnerID
if err := tx.Save(group).Error; err != nil {
return err
}
// 更新原群主为管理员
if err := tx.Model(&model.GroupMember{}).
Where("group_id = ? AND user_id = ?", groupID, userID).
Update("role", model.GroupRoleAdmin).Error; err != nil {
return err
}
// 更新新群主为群主
if err := tx.Model(&model.GroupMember{}).
Where("group_id = ? AND user_id = ?", groupID, newOwnerID).
Update("role", model.GroupRoleOwner).Error; err != nil {
return err
}
return nil
})
// 使用仓储的 TransferOwner 方法(在事务中更新
return s.groupRepo.TransferOwner(groupID, userID, newOwnerID)
}
// GetUserGroups 获取用户加入的群组列表
@@ -593,9 +543,7 @@ func (s *groupService) collectGroupReviewerIDs(groupID string, ownerID string, s
if ownerID != "" && ownerID != skipUserID {
reviewerSet[ownerID] = struct{}{}
}
var reviewers []model.GroupMember
if err := s.db.Where("group_id = ? AND role IN ?", groupID, []string{model.GroupRoleOwner, model.GroupRoleAdmin}).Find(&reviewers).Error; err == nil {
if reviewers, err := s.groupRepo.GetAdmins(groupID); err == nil {
for _, reviewer := range reviewers {
if reviewer.UserID == "" || reviewer.UserID == skipUserID {
continue