Created
April 7, 2024 23:10
-
-
Save F0rzend/09faca9f6cfb6fd6966e3aa5b80438cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package domain | |
type Name struct { | |
value string | |
} | |
func NewName(name string) (Name, error) { | |
if len(name) < 3 || len(name) > 100 { | |
return Name{}, fmt.Errorf("%w: name length must be less than 3 and gather than 100", ErrInvalidNameLength) | |
} | |
return Name{value: name}, nil | |
} | |
func (n Name) String() string { | |
return n.value | |
} | |
type UserID struct { | |
value int64 | |
} | |
func NewUserID(value int64) (UserID, error) { | |
if value <= 0 { | |
return UserID{}, fmt.Errorf("%w: user id must be greater than 0, but got %d", ErrInvalidID, value) | |
} | |
return UserID{value: value}, nil | |
} | |
func (uid UserID) Int64() int64 { | |
return cid.value | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
type CreateUserCommand struct { | |
Name domain.Name | |
} | |
func NewCreateUserCommand(userName string) (*CreateUserCommand, error) { | |
name, err := domain.NewName(userName) | |
// handle err | |
return &CreateUserCommand{ | |
Name: name, | |
}, nil | |
} | |
func (s *Service) CreateUser(ctx context.Context, cmd *CreateUserCommand) (domain.UserID, error) { | |
id, err := s.repo.NextID(ctx) | |
// handle err | |
user := domain.NewUser(id, cmd.Name, nil) | |
err = s.repo.Save(ctx, user) | |
// handle err | |
return id, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package presentation | |
func (h *Handlers) CreateUser( | |
ctx context.Context, | |
req *grpc.Request, | |
) (*grpc.NewUserResponse, error) { | |
cmd, err := service.NewCreateUsertCommand( | |
req.GetName(), | |
) | |
// handle err | |
userID, err := h.service.CreateUser(ctx, cmd) | |
// handle err | |
return &grpc.CreateUserResponse{UserId: userID.Int64()}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment