feat(video): add likescount and updatelikescount
This commit is contained in:
@@ -7,11 +7,12 @@ import (
|
||||
)
|
||||
|
||||
type LikeService struct {
|
||||
repo *LikeRepository
|
||||
repo *LikeRepository
|
||||
VideoRepo *VideoRepository
|
||||
}
|
||||
|
||||
func NewLikeService(repo *LikeRepository) *LikeService {
|
||||
return &LikeService{repo: repo}
|
||||
func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository) *LikeService {
|
||||
return &LikeService{repo: repo, VideoRepo: videoRepo}
|
||||
}
|
||||
|
||||
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
@@ -19,14 +20,34 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
return errors.New("user has liked this video")
|
||||
}
|
||||
like.CreatedAt = time.Now()
|
||||
return s.repo.Like(ctx, like)
|
||||
if err := s.repo.Like(ctx, like); err != nil {
|
||||
return err
|
||||
}
|
||||
likesCount, err := s.GetLikesCount(ctx, like.VideoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.VideoRepo.UpdateLikesCount(ctx, like.VideoID, likesCount); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||
if isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID); err == nil && !isLiked {
|
||||
return errors.New("user has not liked this video")
|
||||
}
|
||||
return s.repo.Unlike(ctx, like)
|
||||
if err := s.repo.Unlike(ctx, like); err != nil {
|
||||
return err
|
||||
}
|
||||
likesCount, err := s.GetLikesCount(ctx, like.VideoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.VideoRepo.UpdateLikesCount(ctx, like.VideoID, likesCount); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user