arbret/backend/repositories/user.py

33 lines
1 KiB
Python
Raw Normal View History

2025-12-25 00:59:57 +01:00
"""User repository for database queries."""
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from models import User
class UserRepository:
"""Repository for user-related database queries."""
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_email(self, email: str) -> User | None:
"""Get a user by email."""
result = await self.db.execute(select(User).where(User.email == email))
return result.scalar_one_or_none()
async def get_by_id(self, user_id: int) -> User | None:
"""Get a user by ID."""
result = await self.db.execute(select(User).where(User.id == user_id))
return result.scalar_one_or_none()
async def get_godfather_email(self, godfather_id: int | None) -> str | None:
"""Get the email of a godfather user by ID."""
if not godfather_id:
return None
result = await self.db.execute(
select(User.email).where(User.id == godfather_id)
)
return result.scalar_one_or_none()