From 70c01f9f87336f320f7851bd289efa64b0d22def Mon Sep 17 00:00:00 2001 From: urec56 Date: Sat, 10 Feb 2024 20:41:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=BD=D1=8B=D1=85?= =?UTF-8?q?=20=D1=87=D0=B0=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat_test/app/users/auth.py | 7 +++-- chat_test/app/users/chat/router.py | 4 +-- chat_test/app/users/dao.py | 47 +++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/chat_test/app/users/auth.py b/chat_test/app/users/auth.py index 169fc66..cdf221e 100644 --- a/chat_test/app/users/auth.py +++ b/chat_test/app/users/auth.py @@ -49,13 +49,14 @@ async def authenticate_user_by_username(username: str, password: str) -> Users | return user -async def get_user_allowed_chats(user_id: int): +async def get_user_allowed_chats_id(user_id: int): user_allowed_chats = await UserDAO.get_user_allowed_chats(user_id) - return user_allowed_chats + user_allowed_chats_id = (chat['chat_id'] for chat in user_allowed_chats) + return user_allowed_chats_id async def validate_user_access_to_chat(user_id: int, chat_id: int): - user_allowed_chats = await get_user_allowed_chats(user_id=user_id) + user_allowed_chats = await get_user_allowed_chats_id(user_id=user_id) if not chat_id in user_allowed_chats: raise UserDontHavePermissionException return True diff --git a/chat_test/app/users/chat/router.py b/chat_test/app/users/chat/router.py index 2b5bdb9..964a9b4 100644 --- a/chat_test/app/users/chat/router.py +++ b/chat_test/app/users/chat/router.py @@ -6,7 +6,7 @@ from app.users.chat.shemas import SMessage, SLastMessages from app.users.dao import UserDAO from app.users.dependencies import get_current_user -from app.users.auth import validate_user_access_to_chat, validate_user_admin, get_user_allowed_chats +from app.users.auth import validate_user_access_to_chat, validate_user_admin from app.users.models import Users router = APIRouter( @@ -17,7 +17,7 @@ router = APIRouter( @router.get('') async def get_all_chats(user: Users = Depends(get_current_user)): - return await get_user_allowed_chats(user.id) + return await UserDAO.get_user_allowed_chats(user.id) @router.post("", status_code=status.HTTP_201_CREATED) diff --git a/chat_test/app/users/dao.py b/chat_test/app/users/dao.py index dd32688..7911bd6 100644 --- a/chat_test/app/users/dao.py +++ b/chat_test/app/users/dao.py @@ -2,8 +2,8 @@ from sqlalchemy import update, select, insert 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.database import async_session_maker, engine +from app.users.chat.models import UsersXChats, Chats from app.users.models import Users, UsersVerificationCodes @@ -29,11 +29,50 @@ class UserDAO(BaseDAO): @classmethod async def get_user_allowed_chats(cls, user_id: int): - query = select(UsersXChats.__table__.columns).where(UsersXChats.user_id == user_id) + """ + WITH chats_with_descriptions AS ( + SELECT * + FROM usersxchats + LEFT JOIN chats ON usersxchats.chat_id = chats.id + ), + + chats_with_avatars as ( + SELECT * + FROM chats_with_descriptions + LEFT JOIN users ON chats_with_descriptions.user_id = users.id + ) + + SELECT chat_id, chat_for, chat_name, avatar_image + FROM chats_with_avatars + WHERE user_id = 1 + """ + chats_with_descriptions = (select(UsersXChats.__table__.columns, Chats.__table__.columns) + .select_from(UsersXChats) + .join(Chats, UsersXChats.chat_id == Chats.id) + ).cte('chats_with_descriptions') + + chats_with_avatars = (select( + chats_with_descriptions.c.chat_id, + chats_with_descriptions.c.chat_for, + chats_with_descriptions.c.chat_name, + Users.id, + Users.avatar_image, + ) + .select_from(chats_with_descriptions) + .join(Users, Users.id == chats_with_descriptions.c.user_id) + .cte('chats_with_avatars')) + query = (select( + chats_with_avatars.c.chat_id, + chats_with_avatars.c.chat_for, + chats_with_avatars.c.chat_name, + chats_with_avatars.c.avatar_image + ) + .select_from(chats_with_avatars) + .where(chats_with_avatars.c.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) + return result @classmethod async def get_user_avatar(cls, user_id: int):