102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
package social
|
|
|
|
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, socialMQ *rabbitmq.SocialMQ) *SocialService {
|
|
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ}
|
|
}
|
|
|
|
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
|
|
}
|
|
if social.FollowerID == social.VloggerID {
|
|
return errors.New("can not follow self")
|
|
}
|
|
isFollowed, err := s.repo.IsFollowed(ctx, social)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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)
|
|
}
|
|
|
|
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")
|
|
}
|
|
if s.socialMQ != nil {
|
|
s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID)
|
|
}
|
|
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) CountFollowers(ctx context.Context, vloggerID uint) (int64, error) {
|
|
return s.repo.CountFollowers(ctx, vloggerID)
|
|
}
|
|
|
|
func (s *SocialService) CountVloggers(ctx context.Context, followerID uint) (int64, error) {
|
|
return s.repo.CountVloggers(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)
|
|
}
|