2024-04-01 01:12:01 +03:00
|
|
|
import re
|
2024-03-31 04:18:41 +03:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from uuid import UUID, uuid4
|
|
|
|
|
2024-04-01 01:12:01 +03:00
|
|
|
from api.domain import DomainValidationError
|
|
|
|
from api.domain.entity import DomainEntity
|
|
|
|
from api.domain.value_obj import DomainValueObject
|
2024-03-31 04:18:41 +03:00
|
|
|
|
|
|
|
|
2024-04-01 01:12:01 +03:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UserEmail(DomainValueObject):
|
|
|
|
value: str
|
2024-03-31 04:18:41 +03:00
|
|
|
|
2024-04-01 01:12:01 +03:00
|
|
|
def __post_init__(self) -> None:
|
|
|
|
pattern = r"^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$"
|
|
|
|
|
|
|
|
if not re.match(pattern, self.value):
|
2024-04-21 23:46:17 +03:00
|
|
|
raise DomainValidationError(
|
|
|
|
"Invalid email format. Email must be in the format 'example@example.com'."
|
|
|
|
)
|
2024-04-01 01:12:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UserFirstName(DomainValueObject):
|
|
|
|
value: str
|
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
if len(self.value) < 1:
|
|
|
|
raise DomainValidationError("First name must be at least 1 character long.")
|
|
|
|
if len(self.value) > 100:
|
2024-04-21 23:46:17 +03:00
|
|
|
raise DomainValidationError(
|
|
|
|
"First name must be at most 100 characters long."
|
|
|
|
)
|
2024-04-01 01:12:01 +03:00
|
|
|
if not self.value.isalpha():
|
|
|
|
raise DomainValidationError("First name must only contain letters.")
|
2024-03-31 04:18:41 +03:00
|
|
|
|
|
|
|
|
2024-04-01 01:12:01 +03:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UserLastName(DomainValueObject):
|
|
|
|
value: str
|
2024-03-31 04:18:41 +03:00
|
|
|
|
2024-04-01 01:12:01 +03:00
|
|
|
def __post_init__(self) -> None:
|
|
|
|
if len(self.value) < 1:
|
|
|
|
raise DomainValidationError("Last name must be at least 1 character long.")
|
|
|
|
if len(self.value) > 100:
|
2024-04-21 23:46:17 +03:00
|
|
|
raise DomainValidationError(
|
|
|
|
"Last name must be at most 100 characters long."
|
|
|
|
)
|
2024-04-01 01:12:01 +03:00
|
|
|
if not self.value.isalpha():
|
|
|
|
raise DomainValidationError("Last name must only contain letters.")
|
2024-03-31 04:18:41 +03:00
|
|
|
|
2024-04-01 01:12:01 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UserId(DomainValueObject):
|
|
|
|
value: UUID
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class User(DomainEntity[UserId]):
|
|
|
|
name: UserFirstName
|
2024-04-21 23:46:17 +03:00
|
|
|
last_name: UserLastName
|
2024-04-01 01:12:01 +03:00
|
|
|
email: UserEmail
|
|
|
|
hashed_password: str
|
|
|
|
|
|
|
|
@staticmethod
|
2024-04-21 23:46:17 +03:00
|
|
|
def create(name: str, last_name: str, email: str, hashed_password: str) -> "User":
|
2024-03-31 04:18:41 +03:00
|
|
|
return User(
|
2024-04-01 01:12:01 +03:00
|
|
|
id=UserId(uuid4()),
|
|
|
|
name=UserFirstName(name),
|
2024-04-21 23:46:17 +03:00
|
|
|
last_name=UserLastName(last_name),
|
2024-04-01 01:12:01 +03:00
|
|
|
email=UserEmail(email),
|
|
|
|
hashed_password=hashed_password,
|
2024-03-31 04:18:41 +03:00
|
|
|
)
|