visus-plus/api/app/application/users_repository.py

36 lines
1021 B
Python

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import joinedload
from app.domain.models.users import User
class UsersRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_all(self):
stmt = select(User)
result = await self.db.execute(stmt)
return result.scalars().all()
async def get_by_id(self, user_id: int):
stmt = select(User).filter(User.id == user_id)
result = await self.db.execute(stmt)
return result.scalars().first()
async def get_by_login(self, user_login: str):
stmt = (
select(User)
.filter(User.login == user_login)
.options(joinedload(User.role))
)
result = await self.db.execute(stmt)
return result.scalars().first()
async def create(self, user: User) -> User:
self.db.add(user)
await self.db.commit()
await self.db.refresh(user)
return user