This commit is contained in:
2026-06-03 13:52:45 +03:00
commit f7309c4b4a
140 changed files with 7134 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
from __future__ import annotations
from datetime import datetime
from fastapi import status
from sqlalchemy import func, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from src.application.abstractions.repositories import IAdminUserRepository
from src.application.contracts import ILogger
from src.application.domain.entities.admin_user import AdminUserEntity
from src.application.domain.exceptions import ApplicationException
from src.infrastructure.database.models import AdminUserModel
class AdminUserRepository(IAdminUserRepository):
def __init__(self, session: AsyncSession, logger: ILogger):
self._session = session
self._logger = logger
def _to_entity(self, m: AdminUserModel) -> AdminUserEntity:
return AdminUserEntity(
id=m.id,
email=m.email,
password_hash=m.password_hash,
first_name=m.first_name,
last_name=m.last_name,
role=m.role,
is_active=m.is_active,
last_login_at=m.last_login_at,
created_at=m.created_at,
updated_at=m.updated_at,
)
async def get_by_email(self, email: str) -> AdminUserEntity:
try:
stmt = select(AdminUserModel).where(func.lower(AdminUserModel.email) == email.lower())
result = await self._session.execute(stmt)
user = result.scalar_one_or_none()
if user is None:
raise ApplicationException(status_code=status.HTTP_404_NOT_FOUND, message='Admin user not found')
return self._to_entity(user)
except ApplicationException:
raise
except SQLAlchemyError as exc:
self._logger.exception(str(exc))
raise ApplicationException(status_code=500, message='Database error')
async def get_by_id(self, admin_user_id: str) -> AdminUserEntity:
try:
stmt = select(AdminUserModel).where(AdminUserModel.id == admin_user_id)
result = await self._session.execute(stmt)
user = result.scalar_one_or_none()
if user is None:
raise ApplicationException(status_code=status.HTTP_404_NOT_FOUND, message='Admin user not found')
return self._to_entity(user)
except ApplicationException:
raise
except SQLAlchemyError as exc:
self._logger.exception(str(exc))
raise ApplicationException(status_code=500, message='Database error')
async def update_last_login(self, admin_user_id: str, *, last_login_at: datetime) -> None:
await self._session.execute(
update(AdminUserModel)
.where(AdminUserModel.id == admin_user_id)
.values(last_login_at=last_login_at)
)
await self._session.flush()