Files
malenia-backend/repositories/users.py
2026-07-24 11:33:56 +07:00

42 lines
1.3 KiB
Python

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models import User
class UserRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
async def get_user_by_id(self, id: int) -> User | None:
stmt = select(User).where(User.id == id)
res = await self.session.execute(stmt)
return res.scalar_one_or_none()
async def get_user_by_telegram_id(self, telegram_id: int) -> User | None:
stmt = select(User).where(User.telegram_id == telegram_id)
res = await self.session.execute(stmt)
return res.scalar_one_or_none()
async def get_user_by_username(self, username: str) -> User | None:
stmt = select(User).where(User.username == username)
res = await self.session.execute(stmt)
return res.scalar_one_or_none()
async def create(
self,
*,
username: str | None = None,
hashed_password: str | None = None,
telegram_id: int | None = None,
) -> User:
obj = User(
username=username,
hashed_password=hashed_password,
telegram_id=telegram_id,
)
self.session.add(obj)
await self.session.commit()
return obj