move: 后端
This commit is contained in:
56
backend/internal/video/like_repo.go
Normal file
56
backend/internal/video/like_repo.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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) 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
|
||||
}
|
||||
Reference in New Issue
Block a user