Добавлено получение доступных чатов
This commit is contained in:
parent
db9c3514a7
commit
70c01f9f87
3 changed files with 49 additions and 9 deletions
|
@ -49,13 +49,14 @@ async def authenticate_user_by_username(username: str, password: str) -> Users |
|
||||||
return user
|
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)
|
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):
|
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:
|
if not chat_id in user_allowed_chats:
|
||||||
raise UserDontHavePermissionException
|
raise UserDontHavePermissionException
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -6,7 +6,7 @@ from app.users.chat.shemas import SMessage, SLastMessages
|
||||||
|
|
||||||
from app.users.dao import UserDAO
|
from app.users.dao import UserDAO
|
||||||
from app.users.dependencies import get_current_user
|
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
|
from app.users.models import Users
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
|
@ -17,7 +17,7 @@ router = APIRouter(
|
||||||
|
|
||||||
@router.get('')
|
@router.get('')
|
||||||
async def get_all_chats(user: Users = Depends(get_current_user)):
|
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)
|
@router.post("", status_code=status.HTTP_201_CREATED)
|
||||||
|
|
|
@ -2,8 +2,8 @@ from sqlalchemy import update, select, insert
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
|
||||||
from app.dao.base import BaseDAO
|
from app.dao.base import BaseDAO
|
||||||
from app.database import async_session_maker
|
from app.database import async_session_maker, engine
|
||||||
from app.users.chat.models import UsersXChats
|
from app.users.chat.models import UsersXChats, Chats
|
||||||
from app.users.models import Users, UsersVerificationCodes
|
from app.users.models import Users, UsersVerificationCodes
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,11 +29,50 @@ class UserDAO(BaseDAO):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get_user_allowed_chats(cls, user_id: int):
|
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:
|
async with async_session_maker() as session:
|
||||||
result = await session.execute(query)
|
result = await session.execute(query)
|
||||||
result = result.mappings().all()
|
result = result.mappings().all()
|
||||||
return (res['chat_id'] for res in result)
|
return result
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get_user_avatar(cls, user_id: int):
|
async def get_user_avatar(cls, user_id: int):
|
||||||
|
|
Loading…
Add table
Reference in a new issue