51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"godemo/user/internal/model"
|
|
"godemo/user/internal/svc"
|
|
"godemo/user/user"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type GetUserInfoLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
|
|
return &GetUserInfoLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// 获取用户信息
|
|
func (l *GetUserInfoLogic) GetUserInfo(in *user.GetUserInfoRequest) (*user.GetUserInfoResponse, error) {
|
|
// todo: add your logic here and delete this line
|
|
var userModel model.User
|
|
err := l.svcCtx.DB.Get(&userModel, "SELECT * FROM users WHERE user_id = $1", in.UserId)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, status.Error(status.Code(err), fmt.Sprintf("用户 %s 不存在", in.UserId))
|
|
}
|
|
return nil, status.Error(status.Code(err), err.Error())
|
|
}
|
|
|
|
return &user.GetUserInfoResponse{
|
|
UserId: userModel.UserId,
|
|
Username: userModel.Username,
|
|
Email: userModel.Email.String,
|
|
CreatedAt: userModel.CreatedAt.Unix(),
|
|
Roles: userModel.Roles,
|
|
}, nil
|
|
}
|