From 1cadc958635ce2ccf76a7b6f345d6a37b5db5505 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Sat, 25 Apr 2026 17:45:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Social=20GetAllFollowers/Vloggers=20+=20?= =?UTF-8?q?Like=20ListLikedVideos=20=E5=8A=A0=20Limit(200)=20=E9=98=B2?= =?UTF-8?q?=E6=B5=B7=E9=87=8F=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/social/repo.go | 184 ++++++++++++------------- backend/internal/video/like_repo.go | 203 ++++++++++++++-------------- 2 files changed, 195 insertions(+), 192 deletions(-) diff --git a/backend/internal/social/repo.go b/backend/internal/social/repo.go index 6dd5928..f990510 100644 --- a/backend/internal/social/repo.go +++ b/backend/internal/social/repo.go @@ -1,91 +1,93 @@ -package social - -import ( - "context" - "feedsystem_video_go/internal/account" - - "gorm.io/gorm" -) - -type SocialRepository struct { - db *gorm.DB -} - -func NewSocialRepository(db *gorm.DB) *SocialRepository { - return &SocialRepository{db: db} -} - -func (r *SocialRepository) Follow(ctx context.Context, social *Social) error { - return r.db.WithContext(ctx).Create(social).Error -} - -func (r *SocialRepository) Unfollow(ctx context.Context, social *Social) error { - return r.db.WithContext(ctx). - Where("follower_id = ? AND vlogger_id = ?", social.FollowerID, social.VloggerID). - Delete(&Social{}).Error -} - -func (r *SocialRepository) GetAllFollowers(ctx context.Context, VloggerID uint) ([]*account.Account, error) { - var relations []Social - if err := r.db.WithContext(ctx). - Model(&Social{}). - Where("vlogger_id = ?", VloggerID). - Find(&relations).Error; err != nil { - return nil, err - } - - followerIDs := make([]uint, 0, len(relations)) - for _, rel := range relations { - followerIDs = append(followerIDs, rel.FollowerID) - } - if len(followerIDs) == 0 { - return []*account.Account{}, nil - } - - var followers []*account.Account - if err := r.db.WithContext(ctx). - Model(&account.Account{}). - Where("id IN ?", followerIDs). - Find(&followers).Error; err != nil { - return nil, err - } - return followers, nil -} - -func (r *SocialRepository) GetAllVloggers(ctx context.Context, FollowerID uint) ([]*account.Account, error) { - var relations []Social - if err := r.db.WithContext(ctx). - Model(&Social{}). - Where("follower_id = ?", FollowerID). - Find(&relations).Error; err != nil { - return nil, err - } - - vloggerIDs := make([]uint, 0, len(relations)) - for _, rel := range relations { - vloggerIDs = append(vloggerIDs, rel.VloggerID) - } - if len(vloggerIDs) == 0 { - return []*account.Account{}, nil - } - - var vloggers []*account.Account - if err := r.db.WithContext(ctx). - Model(&account.Account{}). - Where("id IN ?", vloggerIDs). - Find(&vloggers).Error; err != nil { - return nil, err - } - return vloggers, nil -} - -func (r *SocialRepository) IsFollowed(ctx context.Context, social *Social) (bool, error) { - var count int64 - if err := r.db.WithContext(ctx). - Model(&Social{}). - Where("follower_id = ? AND vlogger_id = ?", social.FollowerID, social.VloggerID). - Count(&count).Error; err != nil { - return false, err - } - return count > 0, nil -} +package social + +import ( + "context" + "feedsystem_video_go/internal/account" + + "gorm.io/gorm" +) + +type SocialRepository struct { + db *gorm.DB +} + +func NewSocialRepository(db *gorm.DB) *SocialRepository { + return &SocialRepository{db: db} +} + +func (r *SocialRepository) Follow(ctx context.Context, social *Social) error { + return r.db.WithContext(ctx).Create(social).Error +} + +func (r *SocialRepository) Unfollow(ctx context.Context, social *Social) error { + return r.db.WithContext(ctx). + Where("follower_id = ? AND vlogger_id = ?", social.FollowerID, social.VloggerID). + Delete(&Social{}).Error +} + +func (r *SocialRepository) GetAllFollowers(ctx context.Context, VloggerID uint) ([]*account.Account, error) { + var relations []Social + if err := r.db.WithContext(ctx). + Model(&Social{}). + Where("vlogger_id = ?", VloggerID). + Limit(200). + Find(&relations).Error; err != nil { + return nil, err + } + + followerIDs := make([]uint, 0, len(relations)) + for _, rel := range relations { + followerIDs = append(followerIDs, rel.FollowerID) + } + if len(followerIDs) == 0 { + return []*account.Account{}, nil + } + + var followers []*account.Account + if err := r.db.WithContext(ctx). + Model(&account.Account{}). + Where("id IN ?", followerIDs). + Find(&followers).Error; err != nil { + return nil, err + } + return followers, nil +} + +func (r *SocialRepository) GetAllVloggers(ctx context.Context, FollowerID uint) ([]*account.Account, error) { + var relations []Social + if err := r.db.WithContext(ctx). + Model(&Social{}). + Where("follower_id = ?", FollowerID). + Limit(200). + Find(&relations).Error; err != nil { + return nil, err + } + + vloggerIDs := make([]uint, 0, len(relations)) + for _, rel := range relations { + vloggerIDs = append(vloggerIDs, rel.VloggerID) + } + if len(vloggerIDs) == 0 { + return []*account.Account{}, nil + } + + var vloggers []*account.Account + if err := r.db.WithContext(ctx). + Model(&account.Account{}). + Where("id IN ?", vloggerIDs). + Find(&vloggers).Error; err != nil { + return nil, err + } + return vloggers, nil +} + +func (r *SocialRepository) IsFollowed(ctx context.Context, social *Social) (bool, error) { + var count int64 + if err := r.db.WithContext(ctx). + Model(&Social{}). + Where("follower_id = ? AND vlogger_id = ?", social.FollowerID, social.VloggerID). + Count(&count).Error; err != nil { + return false, err + } + return count > 0, nil +} diff --git a/backend/internal/video/like_repo.go b/backend/internal/video/like_repo.go index 237c6c3..c17ec96 100644 --- a/backend/internal/video/like_repo.go +++ b/backend/internal/video/like_repo.go @@ -1,101 +1,102 @@ -package video - -import ( - "context" - "errors" - - "github.com/go-sql-driver/mysql" - "gorm.io/gorm" -) - -type LikeRepository struct { - db *gorm.DB -} - -func NewLikeRepository(db *gorm.DB) *LikeRepository { - return &LikeRepository{db: db} -} - -func (r *LikeRepository) Like(ctx context.Context, like *Like) error { - return r.db.WithContext(ctx).Create(like).Error -} - -func (r *LikeRepository) Unlike(ctx context.Context, like *Like) error { - return r.db.WithContext(ctx). - Where("video_id = ? AND account_id = ?", like.VideoID, like.AccountID). - Delete(&Like{}).Error -} - -func (r *LikeRepository) LikeIgnoreDuplicate(ctx context.Context, like *Like) (created bool, err error) { - if like == nil || like.VideoID == 0 || like.AccountID == 0 { - return false, nil - } - err = r.db.WithContext(ctx).Create(like).Error - if err == nil { - return true, nil - } - var mysqlErr *mysql.MySQLError - if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { - return false, nil - } - return false, err -} - -func (r *LikeRepository) DeleteByVideoAndAccount(ctx context.Context, videoID, accountID uint) (deleted bool, err error) { - if videoID == 0 || accountID == 0 { - return false, nil - } - res := r.db.WithContext(ctx). - Where("video_id = ? AND account_id = ?", videoID, accountID). - Delete(&Like{}) - return res.RowsAffected > 0, res.Error -} - -func (r *LikeRepository) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) { - var count int64 - err := r.db.WithContext(ctx).Model(&Like{}). - Where("video_id = ? AND account_id = ?", videoID, accountID). - Count(&count).Error - if err != nil { - return false, err - } - return count > 0, nil -} - -func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, accountID uint) (map[uint]bool, error) { - likeMap := make(map[uint]bool) - if len(videoIDs) == 0 { - return likeMap, nil - } - if accountID == 0 { - return likeMap, nil - } - var likes []Like - err := r.db.WithContext(ctx).Model(&Like{}). - Where("video_id IN ? AND account_id = ?", videoIDs, accountID). - Find(&likes).Error - if err != nil { - return nil, err - } - for _, like := range likes { - likeMap[like.VideoID] = true - } - return likeMap, nil -} - -func (r *LikeRepository) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) { - var videos []Video - if accountID == 0 { - return videos, nil - } - err := r.db.WithContext(ctx). - Model(&Video{}). - Joins("JOIN likes ON likes.video_id = videos.id"). - Where("likes.account_id = ?", accountID). - Order("likes.created_at desc"). - Find(&videos).Error - if err != nil { - return nil, err - } - return videos, nil -} +package video + +import ( + "context" + "errors" + + "github.com/go-sql-driver/mysql" + "gorm.io/gorm" +) + +type LikeRepository struct { + db *gorm.DB +} + +func NewLikeRepository(db *gorm.DB) *LikeRepository { + return &LikeRepository{db: db} +} + +func (r *LikeRepository) Like(ctx context.Context, like *Like) error { + return r.db.WithContext(ctx).Create(like).Error +} + +func (r *LikeRepository) Unlike(ctx context.Context, like *Like) error { + return r.db.WithContext(ctx). + Where("video_id = ? AND account_id = ?", like.VideoID, like.AccountID). + Delete(&Like{}).Error +} + +func (r *LikeRepository) LikeIgnoreDuplicate(ctx context.Context, like *Like) (created bool, err error) { + if like == nil || like.VideoID == 0 || like.AccountID == 0 { + return false, nil + } + err = r.db.WithContext(ctx).Create(like).Error + if err == nil { + return true, nil + } + var mysqlErr *mysql.MySQLError + if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { + return false, nil + } + return false, err +} + +func (r *LikeRepository) DeleteByVideoAndAccount(ctx context.Context, videoID, accountID uint) (deleted bool, err error) { + if videoID == 0 || accountID == 0 { + return false, nil + } + res := r.db.WithContext(ctx). + Where("video_id = ? AND account_id = ?", videoID, accountID). + Delete(&Like{}) + return res.RowsAffected > 0, res.Error +} + +func (r *LikeRepository) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) { + var count int64 + err := r.db.WithContext(ctx).Model(&Like{}). + Where("video_id = ? AND account_id = ?", videoID, accountID). + Count(&count).Error + if err != nil { + return false, err + } + return count > 0, nil +} + +func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, accountID uint) (map[uint]bool, error) { + likeMap := make(map[uint]bool) + if len(videoIDs) == 0 { + return likeMap, nil + } + if accountID == 0 { + return likeMap, nil + } + var likes []Like + err := r.db.WithContext(ctx).Model(&Like{}). + Where("video_id IN ? AND account_id = ?", videoIDs, accountID). + Find(&likes).Error + if err != nil { + return nil, err + } + for _, like := range likes { + likeMap[like.VideoID] = true + } + return likeMap, nil +} + +func (r *LikeRepository) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) { + var videos []Video + if accountID == 0 { + return videos, nil + } + err := r.db.WithContext(ctx). + Model(&Video{}). + Joins("JOIN likes ON likes.video_id = videos.id"). + Where("likes.account_id = ?", accountID). + Order("likes.created_at desc"). + Limit(200). + Find(&videos).Error + if err != nil { + return nil, err + } + return videos, nil +}