feat: 加入了一些边界检查
This commit is contained in:
@@ -3,10 +3,10 @@ package video
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Like struct {
|
type Like struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uint `gorm:"primaryKey" json:"id"`
|
||||||
VideoID uint `gorm:"index;not null"`
|
VideoID uint `gorm:"uniqueIndex:idx_like_video_account;not null" json:"video_id"`
|
||||||
AccountID uint `gorm:"index;not null"`
|
AccountID uint `gorm:"uniqueIndex:idx_like_video_account;not null" json:"account_id"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LikeRequest struct {
|
type LikeRequest struct {
|
||||||
|
|||||||
@@ -16,7 +16,19 @@ func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository) *LikeServi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||||
if isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID); err == nil && isLiked {
|
exists, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return errors.New("video not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if isLiked {
|
||||||
return errors.New("user has liked this video")
|
return errors.New("user has liked this video")
|
||||||
}
|
}
|
||||||
like.CreatedAt = time.Now()
|
like.CreatedAt = time.Now()
|
||||||
@@ -34,7 +46,19 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||||
if isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID); err == nil && !isLiked {
|
exists, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return errors.New("video not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !isLiked {
|
||||||
return errors.New("user has not liked this video")
|
return errors.New("user has not liked this video")
|
||||||
}
|
}
|
||||||
if err := s.repo.Unlike(ctx, like); err != nil {
|
if err := s.repo.Unlike(ctx, like); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user