ms-tester/internal/transport/user_server.go
2024-07-27 07:31:04 +00:00

125 lines
3.1 KiB
Go

package transport
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
userv1 "ms-auth/pkg/go/gen/user/v1"
"strings"
)
func (s *AuthServer) CreateUser(ctx context.Context, req *userv1.CreateUserRequest) (*userv1.CreateUserResponse, error) {
user := req.GetUser()
if user == nil {
return nil, status.Errorf(codes.Unknown, "") // FIXME
}
id, err := s.userService.CreateUser(
ctx,
req.GetToken(),
user.GetUsername(),
user.GetPassword(),
user.Email,
AsTimeP(user.ExpiresAt),
AsInt32P(user.Role),
)
if err != nil {
return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
}
return &userv1.CreateUserResponse{
Id: *id,
}, nil
}
func (s *AuthServer) ReadUser(ctx context.Context, req *userv1.ReadUserRequest) (*userv1.ReadUserResponse, error) {
user, err := s.userService.ReadUser(
ctx,
req.GetToken(),
req.GetId(),
)
if err != nil {
return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
}
return &userv1.ReadUserResponse{
User: &userv1.ReadUserResponse_User{
Id: user.Id,
Username: user.Username,
Email: user.Email,
ExpiresAt: AsTimestampP(&user.ExpiresAt),
CreatedAt: AsTimestampP(&user.CreatedAt),
Role: *AsRoleP(&user.Role),
},
}, nil
}
func (s *AuthServer) UpdateUser(ctx context.Context, req *userv1.UpdateUserRequest) (*emptypb.Empty, error) {
user := req.GetUser()
if user == nil {
return nil, status.Errorf(codes.Unknown, "") // FIXME
}
err := s.userService.UpdateUser(
ctx,
req.GetToken(),
user.GetId(),
user.Username,
user.Password,
user.Email,
AsTimeP(user.ExpiresAt),
AsInt32P(user.Role),
)
if err != nil {
return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
}
return &emptypb.Empty{}, nil
}
func (s *AuthServer) DeleteUser(ctx context.Context, req *userv1.DeleteUserRequest) (*emptypb.Empty, error) {
err := s.userService.DeleteUser(
ctx,
req.GetToken(),
req.GetId(),
)
if err != nil {
return nil, status.Errorf(codes.Unknown, err.Error()) // FIXME
}
return &emptypb.Empty{}, nil
}
func (s *AuthServer) ConfirmEmail(ctx context.Context, req *userv1.ConfirmEmailRequest) (*emptypb.Empty, error) {
panic("not implemented")
}
func (s *AuthServer) RegisterUser(ctx context.Context, req *userv1.RegisterUserRequest) (*emptypb.Empty, error) {
panic("not implemented")
}
func (s *AuthServer) ConfirmRegisterUser(ctx context.Context, req *userv1.ConfirmRegisterUserRequest) (*emptypb.Empty, error) {
panic("not implemented")
}
func (s *AuthServer) ResetPassword(ctx context.Context, req *userv1.ResetPasswordRequest) (*emptypb.Empty, error) {
panic("not implemented")
}
func (s *AuthServer) ConfirmResetPassword(ctx context.Context, req *userv1.ConfirmResetPasswordRequest) (*emptypb.Empty, error) {
panic("not implemented")
}
func shortenEmail(email *string) *string {
if email == nil {
return nil
}
parts := strings.Split(*email, "@")
p1 := parts[0]
p2 := parts[1]
a := "****"
if len(p1) <= 4 {
e := a + "@" + p2
return &e
}
e := p1[:len(p1)-4] + a + "@" + p2
return &e
}