Files
VLoop/backend/internal/social/service.go

94 lines
2.4 KiB
Go
Raw Normal View History

2025-12-19 19:03:42 +08:00
package social
import (
"context"
"errors"
"feedsystem_video_go/internal/account"
2025-12-29 04:13:53 +08:00
"feedsystem_video_go/internal/middleware/rabbitmq"
2025-12-19 19:03:42 +08:00
)
type SocialService struct {
repo *SocialRepository
accountrepo *account.AccountRepository
2025-12-29 04:13:53 +08:00
socialMQ *rabbitmq.SocialMQ
2025-12-19 19:03:42 +08:00
}
2025-12-29 04:13:53 +08:00
func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository, socialMQ *rabbitmq.SocialMQ) *SocialService {
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ}
2025-12-19 19:03:42 +08:00
}
func (s *SocialService) Follow(ctx context.Context, social *Social) error {
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
if err != nil {
return err
}
_, err = s.accountrepo.FindByID(ctx, social.VloggerID)
if err != nil {
return err
}
2025-12-23 02:26:53 +08:00
if social.FollowerID == social.VloggerID {
return errors.New("can not follow self")
}
2025-12-19 19:03:42 +08:00
isFollowed, err := s.repo.IsFollowed(ctx, social)
if err != nil {
return err
}
if isFollowed {
return errors.New("already followed")
}
2025-12-29 04:13:53 +08:00
if s.socialMQ != nil {
s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID)
}
2025-12-19 19:03:42 +08:00
return s.repo.Follow(ctx, social)
}
func (s *SocialService) Unfollow(ctx context.Context, social *Social) error {
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
if err != nil {
return err
}
_, err = s.accountrepo.FindByID(ctx, social.VloggerID)
if err != nil {
return err
}
isFollowed, err := s.repo.IsFollowed(ctx, social)
if err != nil {
return err
}
if !isFollowed {
return errors.New("not followed")
}
2025-12-29 04:13:53 +08:00
if s.socialMQ != nil {
s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID)
}
2025-12-19 19:03:42 +08:00
return s.repo.Unfollow(ctx, social)
}
func (s *SocialService) GetAllFollowers(ctx context.Context, VloggerID uint) ([]*account.Account, error) {
_, err := s.accountrepo.FindByID(ctx, VloggerID)
if err != nil {
return nil, err
}
return s.repo.GetAllFollowers(ctx, VloggerID)
}
func (s *SocialService) GetAllVloggers(ctx context.Context, FollowerID uint) ([]*account.Account, error) {
_, err := s.accountrepo.FindByID(ctx, FollowerID)
if err != nil {
return nil, err
}
return s.repo.GetAllVloggers(ctx, FollowerID)
}
func (s *SocialService) IsFollowed(ctx context.Context, social *Social) (bool, error) {
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
if err != nil {
return false, err
}
_, err = s.accountrepo.FindByID(ctx, social.VloggerID)
if err != nil {
return false, err
}
return s.repo.IsFollowed(ctx, social)
}