chat_back/chat_test/app/users/dao.py

43 lines
1.6 KiB
Python

from sqlalchemy import update, select
from sqlalchemy.exc import SQLAlchemyError
from app.dao.base import BaseDAO
from app.database import async_session_maker
from app.users.chat.models import UsersXChats
from app.users.models import Users
class UserDAO(BaseDAO):
model = Users
@classmethod
async def change_data(cls, user_id: int, **data_to_change):
query = update(Users).where(Users.id == user_id).values(**data_to_change)
async with async_session_maker() as session:
await session.execute(query)
await session.commit()
query = select(Users.username).where(Users.id == user_id)
result = await session.execute(query)
return result.scalar()
@classmethod
async def get_user_role(cls, user_id: int):
query = select(Users.role).where(Users.id == user_id)
async with async_session_maker() as session:
result = await session.execute(query)
return result.scalar()
@classmethod
async def get_user_allowed_chats(cls, user_id: int):
query = select(UsersXChats.__table__.columns).where(UsersXChats.user_id == user_id)
async with async_session_maker() as session:
result = await session.execute(query)
result = result.mappings().all()
return (res['chat_id'] for res in result)
@classmethod
async def get_user_avatar(cls, user_id: int):
query = select(Users.avatar_image).where(Users.id == user_id)
async with async_session_maker() as session:
result = await session.execute(query)
return result.scalar()