45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
from app.models.mariadb.user import User, UserProfile
|
|
from app.repositories.base import BaseRepository
|
|
|
|
|
|
class UserRepository(BaseRepository[User]):
|
|
def __init__(self, session: AsyncSession):
|
|
super().__init__(User, session)
|
|
|
|
async def get_by_email(self, email: str) -> User | None:
|
|
stmt = select(User).where(User.email == email)
|
|
result = await self.session.execute(stmt)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def get_with_profile(self, user_id: int) -> User | None:
|
|
stmt = (
|
|
select(User)
|
|
.options(selectinload(User.profile))
|
|
.where(User.id == user_id)
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def create_with_profile(
|
|
self, user: User, full_name: str = "", phone: str = "", organization: str = ""
|
|
) -> User:
|
|
self.session.add(user)
|
|
await self.session.flush()
|
|
|
|
profile = UserProfile(
|
|
user_id=user.id, # type: ignore[arg-type]
|
|
full_name=full_name,
|
|
phone=phone,
|
|
organization=organization,
|
|
)
|
|
self.session.add(profile)
|
|
await self.session.flush()
|
|
await self.session.refresh(user)
|
|
return user
|