- 实现完整的用户创建逻辑,包含唯一性验证和密码加密 - 实现用户详情查询,安全过滤密码字段 - 实现用户信息更新,支持部分字段更新和唯一性验证 - 实现用户删除功能,包含存在性检查和日志记录 - 创建完整的API测试用例,覆盖正常和错误场景 - 更新任务进度文档,标记第10个任务为已完成 - 提升项目整体完成率至35%,中优先级任务完成率至25%
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"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 UpdateUserLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 更新用户
|
|
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
|
return &UpdateUserLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserRequest) (resp *types.UpdateUserResponse, err error) {
|
|
// 检查用户是否存在
|
|
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
|
|
if err != nil {
|
|
if err == sqlx.ErrNotFound {
|
|
return &types.UpdateUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.UserNotFound,
|
|
Message: "用户不存在",
|
|
},
|
|
}, nil
|
|
}
|
|
logx.WithContext(l.ctx).Error("查询用户失败: ", err)
|
|
return &types.UpdateUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.ServerError,
|
|
Message: "查询用户失败",
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// 检查用户名唯一性(如果要更新用户名)
|
|
if req.Username != "" && strings.TrimSpace(req.Username) != user.Username {
|
|
existingUser, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, req.Username)
|
|
if err == nil && existingUser != nil {
|
|
return &types.UpdateUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.UserExists,
|
|
Message: "用户名已存在",
|
|
},
|
|
}, nil
|
|
}
|
|
user.Username = strings.TrimSpace(req.Username)
|
|
}
|
|
|
|
// 检查邮箱唯一性(如果要更新邮箱)
|
|
if req.Email != "" && strings.TrimSpace(req.Email) != user.Email {
|
|
existingUser, err := l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
|
|
if err == nil && existingUser != nil {
|
|
return &types.UpdateUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.UserExists,
|
|
Message: "邮箱已存在",
|
|
},
|
|
}, nil
|
|
}
|
|
user.Email = strings.TrimSpace(req.Email)
|
|
}
|
|
|
|
// 更新其他字段
|
|
if req.Avatar != "" {
|
|
user.Avatar = req.Avatar
|
|
}
|
|
|
|
if req.Status >= 0 {
|
|
user.Status = int64(req.Status)
|
|
}
|
|
|
|
// 更新时间
|
|
user.UpdatedAt = time.Now()
|
|
|
|
// 执行更新
|
|
err = l.svcCtx.UserModel.Update(l.ctx, user)
|
|
if err != nil {
|
|
logx.WithContext(l.ctx).Error("更新用户失败: ", err)
|
|
return &types.UpdateUserResponse{
|
|
BaseResponse: types.BaseResponse{
|
|
Code: errorx.ServerError,
|
|
Message: "更新用户失败",
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// 返回更新后的用户信息(不包含密码)
|
|
return &types.UpdateUserResponse{
|
|
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
|
|
}
|