Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/model"
|
||||
"highload/internal/repository"
|
||||
"highload/internal/security"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
repo repository.UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(repo repository.UserRepository) *UserService {
|
||||
return &UserService{repo}
|
||||
}
|
||||
|
||||
func (s *UserService) CreateUser(ctx context.Context, username, displayName, password string) (*model.User, error) {
|
||||
|
||||
userOK := checkUsername(username)
|
||||
passwordOK := checkPassword(password)
|
||||
|
||||
if !userOK || !passwordOK {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
if !utf8.ValidString(displayName) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(displayName) > 100 {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
passwordHash, err := security.HashPassword(password)
|
||||
if err != nil {
|
||||
return nil, ErrServerError
|
||||
}
|
||||
|
||||
user, err := s.repo.Create(ctx, username, displayName, passwordHash)
|
||||
if err != nil {
|
||||
return nil, convertRepoError(err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *UserService) GetUser(ctx context.Context, id string) (*model.User, error) {
|
||||
|
||||
_, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
user, err := s.repo.GetUserByID(ctx, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertRepoError(err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user