94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
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
|
|
}
|