2024-04-23 11:10:17 +03:00
|
|
|
from api.application.abstractions.transaction import TransactionContextManager
|
2024-04-01 12:19:10 +03:00
|
|
|
from api.application.contracts.auth.auth_request import UserCreateRequest
|
2024-04-01 01:12:01 +03:00
|
|
|
from api.application.protocols.password_hasher import PasswordHasher
|
2024-03-31 04:18:41 +03:00
|
|
|
from api.domain.user.model import User
|
|
|
|
from api.domain.user.repository import UserRepository
|
|
|
|
|
|
|
|
|
|
|
|
class CreateUser:
|
2024-04-01 01:12:01 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
2024-04-23 11:10:17 +03:00
|
|
|
transaction: TransactionContextManager,
|
2024-04-01 01:12:01 +03:00
|
|
|
user_repository: UserRepository,
|
|
|
|
password_hasher: PasswordHasher,
|
|
|
|
) -> None:
|
2024-04-23 11:10:17 +03:00
|
|
|
self.transaction = transaction
|
2024-03-31 04:18:41 +03:00
|
|
|
self.user_repository = user_repository
|
2024-04-01 01:12:01 +03:00
|
|
|
self.hasher = password_hasher
|
2024-03-31 04:18:41 +03:00
|
|
|
|
|
|
|
async def execute(self, request: UserCreateRequest) -> None:
|
2024-04-01 01:12:01 +03:00
|
|
|
user = User.create(
|
|
|
|
name=request.name,
|
2024-04-21 23:46:17 +03:00
|
|
|
last_name=request.last_name,
|
2024-04-01 01:12:01 +03:00
|
|
|
email=request.email,
|
|
|
|
hashed_password=self.hasher.hash_password(request.password),
|
|
|
|
)
|
2024-04-21 23:46:17 +03:00
|
|
|
|
2024-04-23 11:10:17 +03:00
|
|
|
async with self.transaction as tr:
|
2024-04-21 23:46:17 +03:00
|
|
|
await self.user_repository.create_user(user=user)
|
2024-04-23 11:10:17 +03:00
|
|
|
await tr.commit()
|