fix: GetUserByUsername RPC 查询逻辑

This commit is contained in:
zetaloop
2026-04-03 22:32:46 +08:00
parent f04f6cbcfd
commit 15c940a3e9
@@ -2,11 +2,12 @@ package logic
import ( import (
"context" "context"
"encoding/json"
"errors"
"juwan-backend/app/users/rpc/internal/models/users" "juwan-backend/app/users/rpc/internal/models/users"
"juwan-backend/app/users/rpc/internal/svc" "juwan-backend/app/users/rpc/internal/svc"
"juwan-backend/app/users/rpc/pb" "juwan-backend/app/users/rpc/pb"
"juwan-backend/common/converter"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@@ -26,22 +27,31 @@ func NewGetUserByUsernameLogic(ctx context.Context, svcCtx *svc.ServiceContext)
} }
func (l *GetUserByUsernameLogic) GetUserByUsername(in *pb.GetUserByUsernameReq) (*pb.GetUserByUsernameResp, error) { func (l *GetUserByUsernameLogic) GetUserByUsername(in *pb.GetUserByUsernameReq) (*pb.GetUserByUsernameResp, error) {
pbUsers := &pb.Users{}
//user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
user, err := l.svcCtx.UsersModelRO.Users.Query().Where(users.UsernameEQ(in.Username)).First(l.ctx) user, err := l.svcCtx.UsersModelRO.Users.Query().Where(users.UsernameEQ(in.Username)).First(l.ctx)
if err == nil || user != nil {
return &pb.GetUserByUsernameResp{
Users: pbUsers,
}, nil
}
if err.Error() != "not found" {
return nil, err
}
err = converter.StructToStruct(user, pbUsers)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &pb.GetUserByUsernameResp{}, nil
verificationStatus, err := json.Marshal(user.VerificationStatus)
if err != nil {
logx.Errorf("json marshal verification status failed: %v", err)
return nil, errors.New("marshal verification status failed")
}
pbUser := &pb.Users{
Id: user.ID,
Username: user.Username,
Nickname: user.Nickname,
Avatar: user.Avatar,
Bio: user.Bio,
CurrentRole: user.CurrentRole,
VerifiedRoles: user.VerifiedRoles.Elements,
VerificationStatus: string(verificationStatus),
IsAdmin: user.IsAdmin,
CreatedAt: user.CreatedAt.Unix(),
UpdatedAt: user.UpdatedAt.Unix(),
DeletedAt: user.DeletedAt.Unix(),
}
return &pb.GetUserByUsernameResp{Users: pbUser}, nil
} }