feat: 0.0.1
This commit is contained in:
146 files changed
+10444
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package gapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
db "github.com/FernandoJVideira/simple_bank/internal/db/sqlc"
|
||||
"github.com/FernandoJVideira/simple_bank/internal/service"
|
||||
"github.com/FernandoJVideira/simple_bank/internal/val"
|
||||
"github.com/FernandoJVideira/simple_bank/pb"
|
||||
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func (s *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
violations := validateCreateUserRequest(req)
|
||||
if violations != nil {
|
||||
return nil, invalidArgumentError(violations)
|
||||
}
|
||||
|
||||
result, err := s.userService.CreateUser(ctx, service.RegisterUserRequest{
|
||||
Username: req.GetUsername(),
|
||||
Password: req.GetPassword(),
|
||||
FullName: req.GetFullName(),
|
||||
Email: req.GetEmail(),
|
||||
})
|
||||
if err != nil {
|
||||
// Map service errors to gRPC status codes
|
||||
if db.ErrorCode(err) == db.UniqueViolation {
|
||||
return nil, status.Errorf(codes.AlreadyExists, "username already exists")
|
||||
}
|
||||
return nil, status.Errorf(codes.Internal, "failed to create user: %v", err)
|
||||
}
|
||||
|
||||
resp := &pb.CreateUserResponse{
|
||||
User: convertUser(result.User),
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func validateCreateUserRequest(req *pb.CreateUserRequest) (violations []*errdetails.BadRequest_FieldViolation) {
|
||||
if err := val.ValidateUsername(req.GetUsername()); err != nil {
|
||||
violations = append(violations, fieldViolation("username", err))
|
||||
}
|
||||
|
||||
if err := val.ValidateFullName(req.GetFullName()); err != nil {
|
||||
violations = append(violations, fieldViolation("full_name", err))
|
||||
}
|
||||
|
||||
if err := val.ValidatePassword(req.GetPassword()); err != nil {
|
||||
violations = append(violations, fieldViolation("password", err))
|
||||
}
|
||||
|
||||
if err := val.ValidateEmail(req.GetEmail()); err != nil {
|
||||
violations = append(violations, fieldViolation("email", err))
|
||||
}
|
||||
return violations
|
||||
}
|
||||
Reference in new issue
Block a user