refactor(service): UserService accept PasswordHasher now

This commit is contained in:
Голиков Maximillian Антон
2026-10-01 15:29:12 +03:00
parent 4161333bf8
commit cfabbfb5a0
2 changed files with 7 additions and 17 deletions
+3 -1
View File
@@ -22,7 +22,9 @@ func main() {
store := memory.New()
userSvc := service.NewUserService(store.Users())
hasher := service.NewArgon2idHasher()
userSvc := service.NewUserService(store.Users(), hasher)
chatSvc := service.NewChatService(store.Chats(), store.Users())
messageSvc := service.NewMessageService(store.Messages(), store.Chats())
+4 -16
View File
@@ -2,7 +2,6 @@ package service
import (
"context"
"errors"
"regexp"
"strings"
"time"
@@ -16,11 +15,12 @@ import (
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
type UserService struct {
users domain.UserRepository
users domain.UserRepository
hasher PasswordHasher
}
func NewUserService(users domain.UserRepository) *UserService {
return &UserService{users: users}
func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserService {
return &UserService{users: users, hasher: hasher}
}
func (s *UserService) Create(ctx context.Context, username, displayName string) (*domain.User, error) {
@@ -43,18 +43,6 @@ func (s *UserService) Create(ctx context.Context, username, displayName string)
}
}
_, err := s.users.GetByUsername(ctx, username)
if err == nil {
return nil, &domain.AlreadyExistsError{
Entity: "user",
Field: "username",
Value: username,
}
}
if !errors.Is(err, domain.ErrNotFound) {
return nil, err
}
u := &domain.User{
ID: uuid.NewString(),
Username: username,