diff --git a/backend/internal/http/router.go b/backend/internal/http/router.go index 17d0fff..43be260 100644 --- a/backend/internal/http/router.go +++ b/backend/internal/http/router.go @@ -4,15 +4,17 @@ import ( "feedsystem_video_go/internal/account" "feedsystem_video_go/internal/feed" "feedsystem_video_go/internal/middleware/jwt" + "feedsystem_video_go/internal/middleware/rabbitmq" rediscache "feedsystem_video_go/internal/middleware/redis" "feedsystem_video_go/internal/social" "feedsystem_video_go/internal/video" + "log" "github.com/gin-gonic/gin" "gorm.io/gorm" ) -func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine { +func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine { r := gin.Default() r.Static("/static", "./.run/uploads") // account @@ -77,8 +79,13 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine { protectedCommentGroup.POST("/delete", commentHandler.DeleteComment) } // social + socialMQ, err := rabbitmq.NewSocialMQ(rmq) + if err != nil { + log.Printf("SocialMQ init failed (mq disabled): %v", err) + socialMQ = nil + } socialRepository := social.NewSocialRepository(db) - socialService := social.NewSocialService(socialRepository, accountRepository) + socialService := social.NewSocialService(socialRepository, accountRepository, socialMQ) socialHandler := social.NewSocialHandler(socialService) socialGroup := r.Group("/social") protectedSocialGroup := socialGroup.Group("") diff --git a/backend/internal/social/service.go b/backend/internal/social/service.go index be1d0d9..8aa2c0f 100644 --- a/backend/internal/social/service.go +++ b/backend/internal/social/service.go @@ -4,15 +4,17 @@ import ( "context" "errors" "feedsystem_video_go/internal/account" + "feedsystem_video_go/internal/middleware/rabbitmq" ) type SocialService struct { repo *SocialRepository accountrepo *account.AccountRepository + socialMQ *rabbitmq.SocialMQ } -func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository) *SocialService { - return &SocialService{repo: repo, accountrepo: accountrepo} +func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository, socialMQ *rabbitmq.SocialMQ) *SocialService { + return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ} } func (s *SocialService) Follow(ctx context.Context, social *Social) error { @@ -34,6 +36,9 @@ func (s *SocialService) Follow(ctx context.Context, social *Social) error { if isFollowed { return errors.New("already followed") } + if s.socialMQ != nil { + s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID) + } return s.repo.Follow(ctx, social) } @@ -53,6 +58,9 @@ func (s *SocialService) Unfollow(ctx context.Context, social *Social) error { if !isFollowed { return errors.New("not followed") } + if s.socialMQ != nil { + s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID) + } return s.repo.Unfollow(ctx, social) }