diff --git a/chat_test/app/exceptions.py b/chat_test/app/exceptions.py index e203bbc..f9dbd2a 100644 --- a/chat_test/app/exceptions.py +++ b/chat_test/app/exceptions.py @@ -62,3 +62,8 @@ class IncorrectPasswordException(BlackPhoenixException): class PasswordsМismatchException(BlackPhoenixException): status_code = status.HTTP_409_CONFLICT detail = "Пароли не совпадают" + + +class UsersIdIsTheSameException(BlackPhoenixException): + status_code = status.HTTP_409_CONFLICT + detail = "Айди юзеров совпадают" diff --git a/chat_test/app/users/chat/dao.py b/chat_test/app/users/chat/dao.py index cfdf06a..38ab76f 100644 --- a/chat_test/app/users/chat/dao.py +++ b/chat_test/app/users/chat/dao.py @@ -42,15 +42,37 @@ class ChatDAO(BaseDAO): @classmethod async def get_last_message(cls, chat_id): - query = select(Messages.__table__.columns).where( - and_( - Messages.chat_id == chat_id, - Messages.visibility == True - ) - ).order_by(Messages.created_at.desc()).limit(1) + """ + WITH messages_with_users AS ( + SELECT * + FROM messages + LEFT JOIN users ON messages.user_id = users.id + ) + + SELECT message, image_url, chat_id, user_id, created_at, avatar_image + FROM messages_with_users + WHERE visibility = true AND chat_id = 2 + ORDER BY created_at DESC + LIMIT 1; + """ + messages_with_users = ( + select(Messages.__table__.columns, Users.__table__.columns) + .select_from(Messages) + .join(Users, Messages.user_id == Users.id)).cte('messages_with_users') + + messages = (select(messages_with_users.c.message, messages_with_users.c.image_url, + messages_with_users.c.chat_id, messages_with_users.c.user_id, + messages_with_users.c.created_at, messages_with_users.c.avatar_image) + .where( + and_( + messages_with_users.c.chat_id == chat_id, + messages_with_users.c.visibility == True + ) + ).order_by(messages_with_users.c.created_at.desc()).limit(1)) async with async_session_maker() as session: - result = await session.execute(query) + result = await session.execute(messages) result = result.mappings().all() + print(result) if result: return result[0] diff --git a/chat_test/app/users/chat/router.py b/chat_test/app/users/chat/router.py index 7b37e77..4a10079 100644 --- a/chat_test/app/users/chat/router.py +++ b/chat_test/app/users/chat/router.py @@ -1,7 +1,6 @@ from fastapi import APIRouter, Depends, status - -from app.exceptions import UserDontHavePermissionException, MessageNotFoundException +from app.exceptions import UserDontHavePermissionException, MessageNotFoundException, UsersIdIsTheSameException from app.users.chat.dao import ChatDAO from app.users.chat.shemas import SMessage @@ -15,6 +14,7 @@ router = APIRouter( ) +# Мусор @router.get("") async def root(user: Users = Depends(get_current_user)): print(user) @@ -25,14 +25,12 @@ async def root(user: Users = Depends(get_current_user)): async def add_message_to_chat( chat_id: int, message: str, - image_url: str = None, user: Users = Depends(get_current_user) ): send_message_to_chat = await ChatDAO.send_message( user_id=user.id, chat_id=chat_id, message=message, - image_url=image_url ) @@ -54,25 +52,25 @@ async def delete_message_from_chat( @router.get("/get_last_message", response_model=SMessage) async def get_last_message(chat_id: int, user: Users = Depends(get_current_user)): - message = dict(await ChatDAO.get_last_message(chat_id=chat_id)) - user_avatar = await UserDAO.get_user_avatar(user_id=user.id) - message["user_avatar"] = user_avatar - print(message) - if message is not None: - return message - raise MessageNotFoundException + await validate_user_access_to_chat(chat_id=chat_id, user_id=user.id) + message = await ChatDAO.get_last_message(chat_id=chat_id) + if message is None: + raise MessageNotFoundException + return message -@router.post("/create_chat") +# Не доделан +@router.post("/create_chat", status_code=status.HTTP_201_CREATED) async def create_chat( user_to_exclude: int, user: Users = Depends(get_current_user) ): + if user.id == user_to_exclude: + raise UsersIdIsTheSameException created_chat = await ChatDAO.create(user_id=user_to_exclude) return created_chat + @router.get("/last_messages") async def get_last_messages(): pass - - diff --git a/chat_test/app/users/chat/shemas.py b/chat_test/app/users/chat/shemas.py index fdce15d..0b713ec 100644 --- a/chat_test/app/users/chat/shemas.py +++ b/chat_test/app/users/chat/shemas.py @@ -10,7 +10,7 @@ class SMessage(BaseModel): chat_id: int user_id: int created_at: datetime - user_avatar: str + avatar_image: str class Config: from_attributes = True