remove fucking uow and replace it by TransactionContextManager

This commit is contained in:
2024-04-23 08:10:17 +00:00
parent 12d61e01e1
commit ad92682eda
11 changed files with 108 additions and 89 deletions

View File

@@ -2,7 +2,7 @@ from fastapi import FastAPI
from sqlalchemy.ext.asyncio import (AsyncEngine, AsyncSession,
async_sessionmaker)
from api.application.abstractions.uow import UnitOfWork
from api.application.abstractions.transaction import TransactionContextManager
from api.application.protocols.date_time import DateTimeProvider
from api.application.protocols.jwt import JwtTokenProcessor
from api.application.protocols.password_hasher import PasswordHasher
@@ -16,8 +16,8 @@ from api.domain.user.repository import UserRepository
from api.infrastructure.auth.jwt_settings import JwtSettings
from api.infrastructure.dependencies.adapters import (create_engine,
create_session_maker,
new_session,
new_unit_of_work)
get_transaction_context,
new_session)
from api.infrastructure.dependencies.configs import (app_settings,
get_db_settings,
get_jwt_settings)
@@ -43,7 +43,7 @@ def init_dependencies(app: FastAPI) -> None:
app.dependency_overrides[async_sessionmaker[AsyncSession]] = create_session_maker
app.dependency_overrides[AsyncSession] = new_session
app.dependency_overrides[UnitOfWork] = new_unit_of_work
app.dependency_overrides[TransactionContextManager] = get_transaction_context
app.dependency_overrides[DateTimeProvider] = get_date_time_provider
app.dependency_overrides[PasswordHasher] = get_password_hasher

View File

@@ -2,18 +2,27 @@ from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from api.domain.error import DomainValidationError
from api.domain.user.error import (
UserAlreadyExistsError,
UserInvalidCredentialsError,
UserIsNotAuthorizedError,
)
from api.domain.user.error import (UserAlreadyExistsError,
UserInvalidCredentialsError,
UserIsNotAuthorizedError)
from api.infrastructure.persistence.error import TransactionContextManagerError
async def validation_error_exc_handler(request: Request, exc: DomainValidationError) -> JSONResponse:
async def transaction_error_exec_handler(
request: Request, exc: TransactionContextManagerError
) -> JSONResponse:
return JSONResponse(status_code=400, content={"detail": exc.message})
async def user_authentication_error_exc_handler(request: Request, exc: UserIsNotAuthorizedError) -> JSONResponse:
async def validation_error_exc_handler(
request: Request, exc: DomainValidationError
) -> JSONResponse:
return JSONResponse(status_code=400, content={"detail": exc.message})
async def user_authentication_error_exc_handler(
request: Request, exc: UserIsNotAuthorizedError
) -> JSONResponse:
return JSONResponse(
status_code=401,
content={"detail": exc.message},
@@ -21,7 +30,9 @@ async def user_authentication_error_exc_handler(request: Request, exc: UserIsNot
)
async def user_already_exist_error_exc_handler(request: Request, exc: UserAlreadyExistsError) -> JSONResponse:
async def user_already_exist_error_exc_handler(
request: Request, exc: UserAlreadyExistsError
) -> JSONResponse:
return JSONResponse(status_code=409, content={"detail": exc.message})
@@ -36,6 +47,16 @@ def init_exc_handlers(app: FastAPI) -> None:
DomainValidationError,
validation_error_exc_handler,
)
app.add_exception_handler(UserIsNotAuthorizedError, user_authentication_error_exc_handler)
app.add_exception_handler(UserAlreadyExistsError, user_already_exist_error_exc_handler)
app.add_exception_handler(UserInvalidCredentialsError, user_invalid_credentials_error_exc_handler)
app.add_exception_handler(
UserIsNotAuthorizedError, user_authentication_error_exc_handler
)
app.add_exception_handler(
UserAlreadyExistsError, user_already_exist_error_exc_handler
)
app.add_exception_handler(
UserInvalidCredentialsError, user_invalid_credentials_error_exc_handler
)
app.add_exception_handler(
TransactionContextManagerError, transaction_error_exec_handler
)