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

@@ -1,10 +1,8 @@
package wire
import (
"carrot_bbs/internal/cache"
"carrot_bbs/internal/handler"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/repository"
"carrot_bbs/internal/service"
"github.com/google/wire"
@@ -101,10 +99,8 @@ func ProvideMessageHandler(
// ProvideSystemMessageHandler 提供系统消息处理器
func ProvideSystemMessageHandler(
systemMsgService service.SystemMessageService,
systemNotificationRepo *repository.SystemNotificationRepository,
cacheBackend cache.Cache,
) *handler.SystemMessageHandler {
return handler.NewSystemMessageHandler(systemMsgService, systemNotificationRepo, cacheBackend)
return handler.NewSystemMessageHandler(systemMsgService)
}
// ProvideGroupHandler 提供群组处理器

View File

@@ -135,8 +135,8 @@ func ProvideBuiltinHooks(manager *hook.Manager) *hook.BuiltinHooks {
func ProvideModerationHooks(
manager *hook.Manager,
postAIService *service.PostAIService,
postRepo *repository.PostRepository,
commentRepo *repository.CommentRepository,
postRepo repository.PostRepository,
commentRepo repository.CommentRepository,
cfg *config.Config,
) *hook.ModerationHooks {
strictMode := cfg.OpenAI.StrictModeration

View File

@@ -19,6 +19,7 @@ var RepositorySet = wire.NewSet(
repository.NewDeviceTokenRepository,
repository.NewSystemNotificationRepository,
repository.NewGroupRepository,
repository.NewGroupJoinRequestRepository,
repository.NewStickerRepository,
repository.NewVoteRepository,
repository.NewChannelRepository,
@@ -34,7 +35,7 @@ var RepositorySet = wire.NewSet(
)
// ProvideUserActivityRepository 提供用户活跃数据仓储
func ProvideUserActivityRepository(db *gorm.DB, cache cache.Cache) *repository.UserActivityRepository {
func ProvideUserActivityRepository(db *gorm.DB, cache cache.Cache) repository.UserActivityRepository {
return repository.NewUserActivityRepository(db, cache)
}

View File

@@ -86,9 +86,9 @@ func ProvidePostAIService(client openai.Client) *service.PostAIService {
// ProvidePushService 提供推送服务
func ProvidePushService(
pushRepo *repository.PushRecordRepository,
deviceTokenRepo *repository.DeviceTokenRepository,
messageRepo *repository.MessageRepository,
pushRepo repository.PushRecordRepository,
deviceTokenRepo repository.DeviceTokenRepository,
messageRepo repository.MessageRepository,
sseHub *sse.Hub,
) service.PushService {
return service.NewPushService(pushRepo, deviceTokenRepo, messageRepo, sseHub)
@@ -96,17 +96,17 @@ func ProvidePushService(
// ProvideSystemMessageService 提供系统消息服务
func ProvideSystemMessageService(
notifyRepo *repository.SystemNotificationRepository,
notifyRepo repository.SystemNotificationRepository,
pushService service.PushService,
userRepo *repository.UserRepository,
postRepo *repository.PostRepository,
userRepo repository.UserRepository,
postRepo repository.PostRepository,
cacheBackend cache.Cache,
) service.SystemMessageService {
return service.NewSystemMessageService(notifyRepo, pushService, userRepo, postRepo, cacheBackend)
}
func ProvidePostService(
postRepo *repository.PostRepository,
postRepo repository.PostRepository,
systemMessageService service.SystemMessageService,
postAIService *service.PostAIService,
cacheBackend cache.Cache,
@@ -119,8 +119,8 @@ func ProvidePostService(
}
func ProvideCommentService(
commentRepo *repository.CommentRepository,
postRepo *repository.PostRepository,
commentRepo repository.CommentRepository,
postRepo repository.PostRepository,
systemMessageService service.SystemMessageService,
postAIService *service.PostAIService,
cacheBackend cache.Cache,
@@ -131,17 +131,16 @@ func ProvideCommentService(
// ProvideMessageService 提供消息服务
func ProvideMessageService(
db *gorm.DB,
messageRepo *repository.MessageRepository,
messageRepo repository.MessageRepository,
cacheBackend cache.Cache,
uploadService *service.UploadService,
) *service.MessageService {
return service.NewMessageService(db, messageRepo, cacheBackend, uploadService)
return service.NewMessageService(messageRepo, cacheBackend, uploadService)
}
// ProvideNotificationService 提供通知服务
func ProvideNotificationService(
notificationRepo *repository.NotificationRepository,
notificationRepo repository.NotificationRepository,
cacheBackend cache.Cache,
) *service.NotificationService {
return service.NewNotificationService(notificationRepo, cacheBackend)
@@ -149,7 +148,7 @@ func ProvideNotificationService(
// ProvideUserService 提供用户服务
func ProvideUserService(
userRepo *repository.UserRepository,
userRepo repository.UserRepository,
systemMessageService service.SystemMessageService,
emailService service.EmailService,
cacheBackend cache.Cache,
@@ -166,8 +165,8 @@ func ProvideStickerService(
// ProvideVoteService 提供投票服务
func ProvideVoteService(
voteRepo *repository.VoteRepository,
postRepo *repository.PostRepository,
voteRepo repository.VoteRepository,
postRepo repository.PostRepository,
cache cache.Cache,
postAIService *service.PostAIService,
systemMessageService service.SystemMessageService,
@@ -175,21 +174,20 @@ func ProvideVoteService(
return service.NewVoteService(voteRepo, postRepo, cache, postAIService, systemMessageService)
}
func ProvideChannelService(channelRepo *repository.ChannelRepository, cacheBackend cache.Cache) service.ChannelService {
func ProvideChannelService(channelRepo repository.ChannelRepository, cacheBackend cache.Cache) service.ChannelService {
return service.NewChannelService(channelRepo, cacheBackend)
}
// ProvideChatService 提供聊天服务
// Note: sensitiveService 传 nil与 main.go 保持一致
func ProvideChatService(
db *gorm.DB,
messageRepo *repository.MessageRepository,
userRepo *repository.UserRepository,
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
sseHub *sse.Hub,
cacheBackend cache.Cache,
uploadService *service.UploadService,
) service.ChatService {
return service.NewChatService(db, messageRepo, userRepo, nil, sseHub, cacheBackend, uploadService)
return service.NewChatService(messageRepo, userRepo, nil, sseHub, cacheBackend, uploadService)
}
// ProvideScheduleService 提供日程服务
@@ -210,14 +208,15 @@ func ProvideScheduleSyncService(
// ProvideGroupService 提供群组服务
func ProvideGroupService(
db *gorm.DB,
groupRepo repository.GroupRepository,
userRepo *repository.UserRepository,
messageRepo *repository.MessageRepository,
userRepo repository.UserRepository,
messageRepo repository.MessageRepository,
requestRepo repository.GroupJoinRequestRepository,
notifyRepo repository.SystemNotificationRepository,
sseHub *sse.Hub,
cacheBackend cache.Cache,
) service.GroupService {
return service.NewGroupService(db, groupRepo, userRepo, messageRepo, sseHub, cacheBackend)
return service.NewGroupService(groupRepo, userRepo, messageRepo, requestRepo, notifyRepo, sseHub, cacheBackend)
}
// ProvideUploadService 提供上传服务
@@ -229,7 +228,7 @@ func ProvideUploadService(
}
// ProvideUserActivityService 提供用户活跃统计服务
func ProvideUserActivityService(repo *repository.UserActivityRepository) service.UserActivityService {
func ProvideUserActivityService(repo repository.UserActivityRepository) service.UserActivityService {
return service.NewUserActivityService(repo)
}
@@ -242,24 +241,24 @@ func ProvideRoleService(
}
// ProvideAdminUserService 提供管理端用户服务
func ProvideAdminUserService(userRepo *repository.UserRepository) service.AdminUserService {
func ProvideAdminUserService(userRepo repository.UserRepository) service.AdminUserService {
return service.NewAdminUserService(userRepo)
}
// ProvideAdminPostService 提供管理端帖子服务
func ProvideAdminPostService(postRepo *repository.PostRepository, cacheBackend cache.Cache) service.AdminPostService {
func ProvideAdminPostService(postRepo repository.PostRepository, cacheBackend cache.Cache) service.AdminPostService {
return service.NewAdminPostService(postRepo, cacheBackend)
}
// ProvideAdminCommentService 提供管理端评论服务
func ProvideAdminCommentService(commentRepo *repository.CommentRepository, postRepo *repository.PostRepository) service.AdminCommentService {
func ProvideAdminCommentService(commentRepo repository.CommentRepository, postRepo repository.PostRepository) service.AdminCommentService {
return service.NewAdminCommentService(commentRepo, postRepo)
}
// ProvideAdminGroupService 提供管理端群组服务
func ProvideAdminGroupService(
groupRepo repository.GroupRepository,
userRepo *repository.UserRepository,
userRepo repository.UserRepository,
) service.AdminGroupService {
return service.NewAdminGroupService(groupRepo, userRepo)
}
@@ -267,21 +266,20 @@ func ProvideAdminGroupService(
// ProvideAdminDashboardService 提供管理端仪表盘服务
func ProvideAdminDashboardService(
db *gorm.DB,
userRepo *repository.UserRepository,
postRepo *repository.PostRepository,
commentRepo *repository.CommentRepository,
userRepo repository.UserRepository,
postRepo repository.PostRepository,
commentRepo repository.CommentRepository,
groupRepo repository.GroupRepository,
activityRepo *repository.UserActivityRepository,
activityRepo repository.UserActivityRepository,
) service.AdminDashboardService {
return service.NewAdminDashboardService(db, userRepo, postRepo, commentRepo, groupRepo, activityRepo)
}
// ProvideAsyncLogManager 提供异步日志管理器
func ProvideAsyncLogManager(
db *gorm.DB,
operationRepo *repository.OperationLogRepository,
loginRepo *repository.LoginLogRepository,
dataChangeRepo *repository.DataChangeLogRepository,
operationRepo repository.OperationLogRepository,
loginRepo repository.LoginLogRepository,
dataChangeRepo repository.DataChangeLogRepository,
logger *zap.Logger,
cfg *config.Config,
) *service.AsyncLogManager {
@@ -300,13 +298,13 @@ func ProvideAsyncLogManager(
asyncCfg.FlushInterval = 3 * time.Second
}
return service.NewAsyncLogManager(db, operationRepo, loginRepo, dataChangeRepo, logger, asyncCfg)
return service.NewAsyncLogManager(operationRepo, loginRepo, dataChangeRepo, logger, asyncCfg)
}
// ProvideOperationLogService 提供操作日志服务
func ProvideOperationLogService(
asyncManager *service.AsyncLogManager,
operationRepo *repository.OperationLogRepository,
operationRepo repository.OperationLogRepository,
) service.OperationLogService {
return service.NewOperationLogService(asyncManager, operationRepo)
}
@@ -314,7 +312,7 @@ func ProvideOperationLogService(
// ProvideLoginLogService 提供登录日志服务
func ProvideLoginLogService(
asyncManager *service.AsyncLogManager,
loginRepo *repository.LoginLogRepository,
loginRepo repository.LoginLogRepository,
) service.LoginLogService {
return service.NewLoginLogService(asyncManager, loginRepo)
}
@@ -322,16 +320,16 @@ func ProvideLoginLogService(
// ProvideDataChangeLogService 提供数据变更日志服务
func ProvideDataChangeLogService(
asyncManager *service.AsyncLogManager,
dataChangeRepo *repository.DataChangeLogRepository,
dataChangeRepo repository.DataChangeLogRepository,
) service.DataChangeLogService {
return service.NewDataChangeLogService(asyncManager, dataChangeRepo)
}
// ProvideLogCleanupService 提供日志清理服务
func ProvideLogCleanupService(
operationRepo *repository.OperationLogRepository,
loginRepo *repository.LoginLogRepository,
dataChangeRepo *repository.DataChangeLogRepository,
operationRepo repository.OperationLogRepository,
loginRepo repository.LoginLogRepository,
dataChangeRepo repository.DataChangeLogRepository,
logger *zap.Logger,
cfg *config.Config,
) service.LogCleanupService {
@@ -355,7 +353,7 @@ func ProvideLogService(
}
// ProvideQRCodeLoginService 提供二维码登录服务
func ProvideHotRankWorker(cfg *config.Config, postRepo *repository.PostRepository, cacheBackend cache.Cache) *service.HotRankWorker {
func ProvideHotRankWorker(cfg *config.Config, postRepo repository.PostRepository, cacheBackend cache.Cache) *service.HotRankWorker {
return service.NewHotRankWorker(cfg, postRepo, cacheBackend)
}
@@ -372,8 +370,8 @@ func ProvideQRCodeLoginService(
// ProvideMaterialService 提供学习资料服务
func ProvideMaterialService(
subjectRepo *repository.MaterialSubjectRepository,
fileRepo *repository.MaterialFileRepository,
subjectRepo repository.MaterialSubjectRepository,
fileRepo repository.MaterialFileRepository,
) service.MaterialService {
return service.NewMaterialService(subjectRepo, fileRepo)
}