forked from mtusi2026/mtusi2026lab4
30 lines
761 B
Go
30 lines
761 B
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"mtusi2026lab4/internal/domain"
|
|
"mtusi2026lab4/internal/security"
|
|
)
|
|
|
|
func (s *Service) CreateUser(ctx context.Context, command CreateUserCommand) (domain.User, error) {
|
|
if err := domain.ValidateUser(command.Username, command.DisplayName); err != nil {
|
|
return domain.User{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
|
}
|
|
if err := security.ValidatePassword(command.Password); err != nil {
|
|
return domain.User{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
|
}
|
|
|
|
passwordHash, err := security.HashPassword(command.Password)
|
|
if err != nil {
|
|
return domain.User{}, fmt.Errorf("hash password: %w", err)
|
|
}
|
|
return s.repository.CreateUser(
|
|
ctx,
|
|
command.Username,
|
|
command.DisplayName,
|
|
passwordHash,
|
|
)
|
|
}
|