feat: 完善照片更新和删除业务逻辑
- 实现照片更新功能 (updatePhotoLogic.go) - 支持部分字段更新 (title, description, category_id) - 添加用户权限验证,只能更新自己的照片 - 添加分类存在性验证 - 完善错误处理和响应格式 - 实现照片删除功能 (deletePhotoLogic.go) - 添加用户权限验证,只能删除自己的照片 - 同时删除数据库记录和文件系统文件 - 安全的文件删除处理 - 更新Handler使用统一响应格式 - updatePhotoHandler.go: 使用response.Response统一处理 - deletePhotoHandler.go: 使用response.Response统一处理 - 添加完整API测试用例 (test_photo_crud.http) - 涵盖正常场景和错误场景测试 - 包含权限验证测试 - 更新项目进度 (TASK_PROGRESS.md) - 完成率从8%提升到12% - 更新API接口状态 - 记录技术成果和里程碑
This commit is contained in:
@ -3,8 +3,11 @@ package photo
|
||||
import (
|
||||
"context"
|
||||
|
||||
"photography-backend/internal/model"
|
||||
"photography-backend/internal/svc"
|
||||
"photography-backend/internal/types"
|
||||
"photography-backend/pkg/errorx"
|
||||
fileUtil "photography-backend/pkg/utils/file"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@ -25,7 +28,57 @@ func NewDeletePhotoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delet
|
||||
}
|
||||
|
||||
func (l *DeletePhotoLogic) DeletePhoto(req *types.DeletePhotoRequest) (resp *types.DeletePhotoResponse, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 获取当前用户ID (从JWT中间件获取)
|
||||
userID := l.ctx.Value("userID")
|
||||
if userID == nil {
|
||||
return nil, errorx.NewWithCode(errorx.AuthError)
|
||||
}
|
||||
currentUserID := userID.(int64)
|
||||
|
||||
return
|
||||
// 2. 查询照片是否存在
|
||||
photo, err := l.svcCtx.PhotoModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if err == model.ErrNotFound {
|
||||
return nil, errorx.NewWithCode(errorx.PhotoNotFound)
|
||||
}
|
||||
logx.Errorf("查询照片失败: %v", err)
|
||||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||||
}
|
||||
|
||||
// 3. 检查权限 - 只有照片所有者可以删除
|
||||
if photo.UserId != currentUserID {
|
||||
return nil, errorx.NewWithCode(errorx.Forbidden)
|
||||
}
|
||||
|
||||
// 4. 删除数据库记录
|
||||
err = l.svcCtx.PhotoModel.Delete(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
logx.Errorf("删除照片记录失败: %v", err)
|
||||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||||
}
|
||||
|
||||
// 5. 删除文件系统中的文件
|
||||
// 删除原图
|
||||
if photo.FilePath != "" {
|
||||
if err := fileUtil.DeleteFile(photo.FilePath); err != nil {
|
||||
logx.Errorf("删除原图文件失败: %v", err)
|
||||
// 注意:即使文件删除失败,我们也不回滚数据库操作,因为文件可能已经被手动删除
|
||||
}
|
||||
}
|
||||
|
||||
// 删除缩略图
|
||||
if photo.ThumbnailPath != "" {
|
||||
if err := fileUtil.DeleteFile(photo.ThumbnailPath); err != nil {
|
||||
logx.Errorf("删除缩略图文件失败: %v", err)
|
||||
// 注意:即使文件删除失败,我们也不回滚数据库操作
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 返回成功响应
|
||||
return &types.DeletePhotoResponse{
|
||||
BaseResponse: types.BaseResponse{
|
||||
Code: errorx.Success,
|
||||
Message: "照片删除成功",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user