초기 커밋
This commit is contained in:
44
app/repositories/user_repo.py
Normal file
44
app/repositories/user_repo.py
Normal file
@@ -0,0 +1,44 @@
|
||||
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
|
||||
Reference in New Issue
Block a user