You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
60 lines
1.3 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdateUserProfileLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewUpdateUserProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserProfileLogic {
|
|
return &UpdateUserProfileLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UpdateUserProfileLogic) UpdateUserProfile(req *types.UpdateProfileReq) (resp *types.UserInfo, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
var user model.User
|
|
if err := l.svcCtx.DB.First(&user, userID).Error; err != nil {
|
|
return nil, errorx.ErrUserNotFound
|
|
}
|
|
|
|
// 更新字段
|
|
if req.Nickname != "" {
|
|
user.Nickname = req.Nickname
|
|
}
|
|
if req.Avatar != "" {
|
|
user.Avatar = req.Avatar
|
|
}
|
|
|
|
if err := l.svcCtx.DB.Save(&user).Error; err != nil {
|
|
l.Errorf("Failed to update user: %v", err)
|
|
return nil, errorx.ErrServerError
|
|
}
|
|
|
|
return &types.UserInfo{
|
|
ID: uint(user.ID),
|
|
Phone: user.Phone,
|
|
Email: user.Email,
|
|
Nickname: user.Nickname,
|
|
Avatar: user.Avatar,
|
|
SurveyCompleted: user.SurveyCompleted,
|
|
}, nil
|
|
}
|
|
|