fea: 修改下架移除点赞记录删除

This commit is contained in:
zerosaturation 2026-05-22 18:49:24 +08:00
parent e29a718b2f
commit ce4fd85926
5 changed files with 23 additions and 38 deletions

View File

@ -67,7 +67,7 @@ func (r *assetLikeRepository) Create(like *models.AssetLike) error {
return err
}
if exists {
return errors.New("already liked")
return errors.New("本次展示已点赞")
}
if err := r.db.Create(like).Error; err != nil {

View File

@ -147,7 +147,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Int64("asset_id", assetID),
zap.Int64("user_id", userID),
)
return asset.LikeCount, fmt.Errorf("asset already liked")
return asset.LikeCount, fmt.Errorf("本次展示已点赞")
}
// 3. 开启事务
@ -167,7 +167,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
)
// 唯一约束冲突 = 已经点赞过
if isUniqueConstraintViolation(err) {
return fmt.Errorf("asset already liked")
return fmt.Errorf("本次展示已点赞")
}
return fmt.Errorf("failed to create like record: %w", err)
}

View File

@ -152,16 +152,8 @@ func (s *exhibitionService) RemoveFromSlot(userID, starID int64, req *pb.RemoveF
return err
}
// 5. 清除该资产的点赞记录(不阻断主流程)
// 目的:允许同一用户在下次展出时可以再次点赞
go func(assetID int64) {
if err := s.assetClient.ClearAssetLikeRecords(assetID); err != nil {
logger.Logger.Error("Failed to clear asset like records after removal",
zap.Int64("asset_id", assetID),
zap.Error(err),
)
}
}(exhibition.AssetID)
// 5. 保留点赞记录,允许下次展出时用户重新点赞
// 点赞记录用于历史查询,每次展出通过 exhibition_id 区分
// 6. 发布事件
// TODO: 实现事件发布逻辑
// go s.publishEvent("gallery.remove_from_slot", exhibition)
@ -311,15 +303,8 @@ func (s *exhibitionService) RemoveExhibitionByAsset(ctx context.Context, assetID
return err
}
// 3. 清除该资产的点赞记录(不阻断主流程)
go func(assetID int64) {
if err := s.assetClient.ClearAssetLikeRecords(assetID); err != nil {
logger.Logger.Error("Failed to clear asset like records after removal",
zap.Int64("asset_id", assetID),
zap.Error(err),
)
}
}(exhibition.AssetID)
// 3. 保留点赞记录,允许下次展出时用户重新点赞
// 点赞记录用于历史查询,每次展出通过 exhibition_id 区分
return nil
}

View File

@ -262,7 +262,7 @@ func (p *SocialProvider) LikeAsset(ctx context.Context, req *pb.LikeAssetRequest
return &pb.LikeAssetResponse{
Base: &common.BaseResponse{
Code: common.StatusCode_STATUS_INTERNAL_ERROR,
Message: "Failed to like asset: " + err.Error(),
Message: err.Error(),
Timestamp: time.Now().UnixMilli(),
},
}, nil
@ -308,7 +308,7 @@ func (p *SocialProvider) UnlikeAsset(ctx context.Context, req *pb.UnlikeAssetReq
return &pb.UnlikeAssetResponse{
Base: &common.BaseResponse{
Code: common.StatusCode_STATUS_INTERNAL_ERROR,
Message: "Failed to unlike asset: " + err.Error(),
Message: err.Error(),
Timestamp: time.Now().UnixMilli(),
},
}, nil
@ -356,7 +356,7 @@ func (p *SocialProvider) CheckAssetLike(ctx context.Context, req *pb.CheckAssetL
return &pb.CheckAssetLikeResponse{
Base: &common.BaseResponse{
Code: common.StatusCode_STATUS_INTERNAL_ERROR,
Message: "Failed to check asset like: " + err.Error(),
Message: err.Error(),
Timestamp: time.Now().UnixMilli(),
},
IsLiked: false,

View File

@ -47,7 +47,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return fmt.Errorf("failed to get asset: %w", err)
return fmt.Errorf("获取藏品信息失败,请稍后重试")
}
if getAssetResp.Base.Code != pbCommon.StatusCode_STATUS_OK {
@ -56,7 +56,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Int32("code", int32(getAssetResp.Base.Code)),
zap.String("message", getAssetResp.Base.Message),
)
return fmt.Errorf("asset not found: %s", getAssetResp.Base.Message)
return fmt.Errorf("藏品不存在")
}
// 2. 检查是否已点赞
@ -69,7 +69,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return fmt.Errorf("failed to check asset like: %w", err)
return fmt.Errorf("点赞失败,请稍后重试")
}
if checkLikeResp.IsLiked {
@ -77,7 +77,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Int64("asset_id", assetID),
zap.Int64("user_id", userID),
)
return fmt.Errorf("already liked this asset")
return fmt.Errorf("当前展示已点赞")
}
// 3. 调用 Asset Service 点赞接口
@ -90,7 +90,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return fmt.Errorf("failed to like asset: %w", err)
return fmt.Errorf("点赞失败,请稍后重试")
}
if likeResp.Base.Code != pbCommon.StatusCode_STATUS_OK {
@ -99,7 +99,7 @@ func (s *AssetLikeService) LikeAsset(ctx context.Context, assetID, userID, starI
zap.Int32("code", int32(likeResp.Base.Code)),
zap.String("message", likeResp.Base.Message),
)
return fmt.Errorf("like asset failed: %s", likeResp.Base.Message)
return fmt.Errorf("点赞失败: %s", likeResp.Base.Message)
}
logger.Logger.Info("Successfully liked asset",
@ -132,7 +132,7 @@ func (s *AssetLikeService) UnlikeAsset(ctx context.Context, assetID, userID, sta
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return fmt.Errorf("failed to check asset like: %w", err)
return fmt.Errorf("取消点赞失败,请稍后重试")
}
if !checkLikeResp.IsLiked {
@ -153,7 +153,7 @@ func (s *AssetLikeService) UnlikeAsset(ctx context.Context, assetID, userID, sta
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return fmt.Errorf("failed to unlike asset: %w", err)
return fmt.Errorf("取消点赞失败,请稍后重试")
}
if unlikeResp.Base.Code != pbCommon.StatusCode_STATUS_OK {
@ -162,7 +162,7 @@ func (s *AssetLikeService) UnlikeAsset(ctx context.Context, assetID, userID, sta
zap.Int32("code", int32(unlikeResp.Base.Code)),
zap.String("message", unlikeResp.Base.Message),
)
return fmt.Errorf("unlike asset failed: %s", unlikeResp.Base.Message)
return fmt.Errorf("取消点赞失败: %s", unlikeResp.Base.Message)
}
logger.Logger.Info("Successfully unliked asset",
@ -194,7 +194,7 @@ func (s *AssetLikeService) CheckAssetLike(ctx context.Context, assetID, userID,
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return false, fmt.Errorf("failed to check asset like: %w", err)
return false, fmt.Errorf("取消点赞失败,请稍后重试")
}
if checkLikeResp.Base.Code != pbCommon.StatusCode_STATUS_OK {
@ -203,7 +203,7 @@ func (s *AssetLikeService) CheckAssetLike(ctx context.Context, assetID, userID,
zap.Int32("code", int32(checkLikeResp.Base.Code)),
zap.String("message", checkLikeResp.Base.Message),
)
return false, fmt.Errorf("check asset like failed: %s", checkLikeResp.Base.Message)
return false, fmt.Errorf("查询点赞状态失败: %s", checkLikeResp.Base.Message)
}
return checkLikeResp.IsLiked, nil
@ -229,7 +229,7 @@ func (s *AssetLikeService) GetAssetLikes(ctx context.Context, assetID int64, pag
zap.Error(err),
zap.Int64("asset_id", assetID),
)
return nil, fmt.Errorf("failed to get asset likes: %w", err)
return nil, fmt.Errorf("获取点赞列表失败,请稍后重试")
}
if getLikesResp.Base.Code != pbCommon.StatusCode_STATUS_OK {
@ -238,7 +238,7 @@ func (s *AssetLikeService) GetAssetLikes(ctx context.Context, assetID int64, pag
zap.Int32("code", int32(getLikesResp.Base.Code)),
zap.String("message", getLikesResp.Base.Message),
)
return nil, fmt.Errorf("get asset likes failed: %s", getLikesResp.Base.Message)
return nil, fmt.Errorf("获取点赞列表失败: %s", getLikesResp.Base.Message)
}
return getLikesResp, nil