fix: Social GetAllFollowers/Vloggers + Like ListLikedVideos 加 Limit(200) 防海量数据

This commit is contained in:
Sisyphus
2026-04-25 17:45:33 +08:00
parent 48a7eea0f9
commit 1cadc95863
2 changed files with 195 additions and 192 deletions

View File

@@ -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
}

View File

@@ -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
}