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

@@ -26,22 +26,30 @@ type SystemMessageService interface {
// 发送系统公告
SendSystemAnnouncement(ctx context.Context, userIDs []string, title string, content string) error
SendBroadcastAnnouncement(ctx context.Context, title string, content string) error
// 查询通知(供 Handler 层的 REST API 使用)
GetNotifications(receiverID string, page, pageSize int) ([]*model.SystemNotification, int64, error)
GetNotificationsByCursor(receiverID string, cursorStr string, pageSize int) ([]*model.SystemNotification, string, bool, error)
GetUnreadCount(receiverID string) (int64, error)
MarkAsRead(notificationID int64, receiverID string) error
MarkAllAsRead(receiverID string) error
DeleteNotification(notificationID int64, receiverID string) error
}
type systemMessageServiceImpl struct {
notifyRepo *repository.SystemNotificationRepository
notifyRepo repository.SystemNotificationRepository
pushService PushService
userRepo *repository.UserRepository
postRepo *repository.PostRepository
userRepo repository.UserRepository
postRepo repository.PostRepository
cache cache.Cache
}
// NewSystemMessageService 创建系统消息服务
func NewSystemMessageService(
notifyRepo *repository.SystemNotificationRepository,
notifyRepo repository.SystemNotificationRepository,
pushService PushService,
userRepo *repository.UserRepository,
postRepo *repository.PostRepository,
userRepo repository.UserRepository,
postRepo repository.PostRepository,
cacheBackend cache.Cache,
) SystemMessageService {
return &systemMessageServiceImpl{
@@ -402,6 +410,51 @@ func (s *systemMessageServiceImpl) createNotification(ctx context.Context, userI
return notification, nil
}
// GetNotifications 获取通知列表(供 Handler 调用)
func (s *systemMessageServiceImpl) GetNotifications(receiverID string, page, pageSize int) ([]*model.SystemNotification, int64, error) {
return s.notifyRepo.GetByReceiverID(receiverID, page, pageSize)
}
// GetNotificationsByCursor 游标分页获取通知列表(供 Handler 调用)
func (s *systemMessageServiceImpl) GetNotificationsByCursor(receiverID string, cursorStr string, pageSize int) ([]*model.SystemNotification, string, bool, error) {
return s.notifyRepo.GetByReceiverIDCursorLegacy(receiverID, cursorStr, pageSize)
}
// GetUnreadCount 获取未读数量(供 Handler 调用)
func (s *systemMessageServiceImpl) GetUnreadCount(receiverID string) (int64, error) {
return s.notifyRepo.GetUnreadCount(receiverID)
}
// MarkAsRead 标记单条为已读(供 Handler 调用)
func (s *systemMessageServiceImpl) MarkAsRead(notificationID int64, receiverID string) error {
err := s.notifyRepo.MarkAsRead(notificationID, receiverID)
if err != nil {
return err
}
cache.InvalidateUnreadSystem(s.cache, receiverID)
return nil
}
// MarkAllAsRead 标记全部为已读(供 Handler 调用)
func (s *systemMessageServiceImpl) MarkAllAsRead(receiverID string) error {
err := s.notifyRepo.MarkAllAsRead(receiverID)
if err != nil {
return err
}
cache.InvalidateUnreadSystem(s.cache, receiverID)
return nil
}
// DeleteNotification 删除通知(供 Handler 调用)
func (s *systemMessageServiceImpl) DeleteNotification(notificationID int64, receiverID string) error {
err := s.notifyRepo.Delete(notificationID, receiverID)
if err != nil {
return err
}
cache.InvalidateUnreadSystem(s.cache, receiverID)
return nil
}
// getActorInfo 获取操作者信息
func (s *systemMessageServiceImpl) getActorInfo(ctx context.Context, operatorID string) (string, string, error) {
// 从用户仓储获取用户信息