- 实现完整的用户创建逻辑,包含唯一性验证和密码加密 - 实现用户详情查询,安全过滤密码字段 - 实现用户信息更新,支持部分字段更新和唯一性验证 - 实现用户删除功能,包含存在性检查和日志记录 - 创建完整的API测试用例,覆盖正常和错误场景 - 更新任务进度文档,标记第10个任务为已完成 - 提升项目整体完成率至35%,中优先级任务完成率至25%
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"photography-backend/internal/svc"
|
|
"photography-backend/internal/types"
|
|
"photography-backend/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
type GetUserLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 获取用户详情
|
|
func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
|
|
return &GetUserLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetUserLogic) GetUser(req *types.GetUserRequest) (resp *types.GetUserResponse, err error) {
|
|
// 查询用户
|
|
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
|
|
if err != nil {
|
|
if err == sqlx.ErrNotFound {
|
|
return &types.GetUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.UserNotFound,
|
|
Message: "用户不存在",
|
|
},
|
|
}, nil
|
|
}
|
|
logx.WithContext(l.ctx).Error("查询用户失败: ", err)
|
|
return &types.GetUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.ServerError,
|
|
Message: "查询用户失败",
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// 返回用户信息(不包含密码)
|
|
return &types.GetUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.Success,
|
|
Message: "查询成功",
|
|
},
|
|
Data: types.User{
|
|
Id: user.Id,
|
|
Username: user.Username,
|
|
Email: user.Email,
|
|
Avatar: user.Avatar,
|
|
Status: int(user.Status),
|
|
CreatedAt: user.CreatedAt.Unix(),
|
|
UpdatedAt: user.UpdatedAt.Unix(),
|
|
},
|
|
}, nil
|
|
}
|