feat(video): add like/unlike/isliked/getlikescount APIs for videos

This commit is contained in:
Leon
2025-12-09 15:30:16 +08:00
parent 72f35f7a7a
commit eeafe9d32a
6 changed files with 228 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
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) GetLikesCount(ctx context.Context, videoID uint) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&Like{}).
Where("video_id = ?", videoID).
Count(&count).Error
if err != nil {
return 0, err
}
return count, nil
}