Skip to content

Instantly share code, notes, and snippets.

@F0rzend
Created April 7, 2024 23:10
Show Gist options
  • Save F0rzend/09faca9f6cfb6fd6966e3aa5b80438cd to your computer and use it in GitHub Desktop.
Save F0rzend/09faca9f6cfb6fd6966e3aa5b80438cd to your computer and use it in GitHub Desktop.
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
}
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
}
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