from __future__ import annotations from sqlalchemy import func from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.models.entities import User class UserRepository: def __init__(self, session: AsyncSession) -> None: self.session = session async def count_users(self) -> int: result = await self.session.execute(select(func.count()).select_from(User)) return int(result.scalar_one()) async def get_by_username(self, username: str) -> User | None: result = await self.session.execute(select(User).where(User.username == username)) return result.scalar_one_or_none() async def get_by_id(self, user_id: str) -> User | None: result = await self.session.execute(select(User).where(User.id == user_id)) return result.scalar_one_or_none() async def create(self, user: User) -> User: self.session.add(user) await self.session.commit() await self.session.refresh(user) return user