feat: 点赞相关rabbitMQ

This commit is contained in:
Leon
2025-12-30 01:23:54 +08:00
parent 0d96f8df88
commit 3800373166
5 changed files with 380 additions and 43 deletions

View File

@@ -2,7 +2,9 @@ package video
import (
"context"
"errors"
"github.com/go-sql-driver/mysql"
"gorm.io/gorm"
)
@@ -24,6 +26,31 @@ func (r *LikeRepository) Unlike(ctx context.Context, like *Like) error {
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{}).