48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
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
|
|
}
|