fix: 修复后端导入错误并统一错误处理机制
## 主要修复内容 ### 🔧 导入错误修复 - 修复 updateCategoryLogic.go 缺失的导入 (errorx, model, sql, time) - 修复 loginLogic.go 中 errors 包应为 errorx 包的问题 - 修复 uploadPhotoLogic.go 中错误处理不统一的问题 - 修复 photo 查询相关文件缺失 model 包导入 ### ⚡ 错误处理统一化 - 统一使用项目自定义的 errorx 包替代标准库 errors - 完善 model.ErrNotFound 错误判断逻辑 - 添加详细的错误日志记录 - 统一响应代码使用 errorx.Success ### 🆕 错误代码扩展 - 新增 UserDisabled (1003) 错误代码 - 新增 InvalidParameter (400) 错误代码别名 - 完善错误代码到 HTTP 状态码的映射 - 修复重复错误代码导致的编译问题 ### ✅ 代码质量保证 - 解决所有编译错误,确保 go build 成功 - 修复 15 个后端逻辑文件的导入问题 - 整理 go.mod 依赖包 - 更新项目任务进度文档 ## 影响的文件 - backend/internal/logic/auth/loginLogic.go - backend/internal/logic/category/updateCategoryLogic.go - backend/internal/logic/photo/uploadPhotoLogic.go - backend/internal/logic/photo/getPhotoLogic.go - backend/internal/logic/photo/getPhotoListLogic.go - backend/pkg/errorx/errorx.go - TASK_PROGRESS.md
This commit is contained in:
@ -2,9 +2,13 @@ package category
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"photography-backend/internal/model"
|
||||
"photography-backend/internal/svc"
|
||||
"photography-backend/internal/types"
|
||||
"photography-backend/pkg/errorx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@ -25,7 +29,61 @@ func NewUpdateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up
|
||||
}
|
||||
|
||||
func (l *UpdateCategoryLogic) UpdateCategory(req *types.UpdateCategoryRequest) (resp *types.UpdateCategoryResponse, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数验证
|
||||
if req.Name == "" && req.Description == "" {
|
||||
return nil, errorx.NewWithCode(errorx.InvalidParameter)
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 查询分类是否存在
|
||||
category, err := l.svcCtx.CategoryModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if err == model.ErrNotFound {
|
||||
return nil, errorx.NewWithCode(errorx.CategoryNotFound)
|
||||
}
|
||||
logx.Errorf("查询分类失败: %v", err)
|
||||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||||
}
|
||||
|
||||
// 3. 检查名称是否重复 (如果要更新名称)
|
||||
if req.Name != "" && req.Name != category.Name {
|
||||
existingCategory, err := l.svcCtx.CategoryModel.FindOneByName(l.ctx, req.Name)
|
||||
if err != nil && err != model.ErrNotFound {
|
||||
logx.Errorf("查询分类名称失败: %v", err)
|
||||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||||
}
|
||||
if existingCategory != nil {
|
||||
return nil, errorx.NewWithCode(errorx.CategoryExists)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 更新分类信息
|
||||
if req.Name != "" {
|
||||
category.Name = req.Name
|
||||
}
|
||||
if req.Description != "" {
|
||||
category.Description = sql.NullString{String: req.Description, Valid: true}
|
||||
}
|
||||
category.UpdatedAt = time.Now()
|
||||
|
||||
// 5. 保存到数据库
|
||||
err = l.svcCtx.CategoryModel.Update(l.ctx, category)
|
||||
if err != nil {
|
||||
logx.Errorf("更新分类失败: %v", err)
|
||||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||||
}
|
||||
|
||||
// 6. 构造响应
|
||||
return &types.UpdateCategoryResponse{
|
||||
BaseResponse: types.BaseResponse{
|
||||
Code: errorx.Success,
|
||||
Message: "分类更新成功",
|
||||
},
|
||||
Data: types.Category{
|
||||
Id: category.Id,
|
||||
Name: category.Name,
|
||||
Description: category.Description.String,
|
||||
CreatedAt: category.CreatedAt.Unix(),
|
||||
UpdatedAt: category.UpdatedAt.Unix(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user