You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

30 lines
1022 B

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