refactor(service): UserService accept PasswordHasher now
This commit is contained in:
@@ -22,7 +22,9 @@ func main() {
|
|||||||
|
|
||||||
store := memory.New()
|
store := memory.New()
|
||||||
|
|
||||||
userSvc := service.NewUserService(store.Users())
|
hasher := service.NewArgon2idHasher()
|
||||||
|
|
||||||
|
userSvc := service.NewUserService(store.Users(), hasher)
|
||||||
chatSvc := service.NewChatService(store.Chats(), store.Users())
|
chatSvc := service.NewChatService(store.Chats(), store.Users())
|
||||||
messageSvc := service.NewMessageService(store.Messages(), store.Chats())
|
messageSvc := service.NewMessageService(store.Messages(), store.Chats())
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -16,11 +15,12 @@ import (
|
|||||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||||
|
|
||||||
type UserService struct {
|
type UserService struct {
|
||||||
users domain.UserRepository
|
users domain.UserRepository
|
||||||
|
hasher PasswordHasher
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserService(users domain.UserRepository) *UserService {
|
func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserService {
|
||||||
return &UserService{users: users}
|
return &UserService{users: users, hasher: hasher}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UserService) Create(ctx context.Context, username, displayName string) (*domain.User, error) {
|
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{
|
u := &domain.User{
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
Username: username,
|
Username: username,
|
||||||
|
|||||||
Reference in New Issue
Block a user