2023-07-31 14:10:37 +00:00
|
|
|
from typing import Optional, List
|
|
|
|
|
|
|
|
from sqlmodel import select
|
2023-10-25 08:35:42 +00:00
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
2023-07-31 14:10:37 +00:00
|
|
|
|
|
|
|
from gram_core.base_service import BaseService
|
|
|
|
from gram_core.dependence.database import Database
|
2024-01-16 14:38:33 +00:00
|
|
|
from gram_core.services.users.models import UserDataBase as User, PermissionsEnum
|
2023-07-31 14:10:37 +00:00
|
|
|
|
|
|
|
__all__ = ("UserRepository",)
|
|
|
|
|
|
|
|
|
|
|
|
class UserRepository(BaseService.Component):
|
|
|
|
def __init__(self, database: Database):
|
|
|
|
self.engine = database.engine
|
|
|
|
|
|
|
|
async def get_by_user_id(self, user_id: int) -> Optional[User]:
|
|
|
|
async with AsyncSession(self.engine) as session:
|
|
|
|
statement = select(User).where(User.user_id == user_id)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
return results.first()
|
|
|
|
|
|
|
|
async def add(self, user: User):
|
|
|
|
async with AsyncSession(self.engine) as session:
|
|
|
|
session.add(user)
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
async def update(self, user: User) -> User:
|
|
|
|
async with AsyncSession(self.engine) as session:
|
|
|
|
session.add(user)
|
|
|
|
await session.commit()
|
|
|
|
await session.refresh(user)
|
|
|
|
return user
|
|
|
|
|
|
|
|
async def remove(self, user: User):
|
|
|
|
async with AsyncSession(self.engine) as session:
|
|
|
|
await session.delete(user)
|
|
|
|
await session.commit()
|
|
|
|
|
2024-01-16 14:38:33 +00:00
|
|
|
async def get_all(self, is_public: bool = None, is_banned: bool = None) -> List[User]:
|
2023-07-31 14:10:37 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
|
|
|
statement = select(User)
|
2024-01-16 14:38:33 +00:00
|
|
|
if is_public:
|
|
|
|
statement = statement.where(User.permissions == PermissionsEnum.PUBLIC)
|
|
|
|
if is_banned is not None:
|
|
|
|
statement = statement.where(User.is_banned == is_banned)
|
2023-07-31 14:10:37 +00:00
|
|
|
results = await session.exec(statement)
|
|
|
|
return results.all()
|