Переделка бд

This commit is contained in:
urec56 2024-06-08 17:50:47 +05:00
parent 89340eff19
commit c865196f29
6 changed files with 37 additions and 58 deletions

View file

@ -3,15 +3,12 @@ from fastapi import APIRouter, Depends, status
from app.config import settings
from app.chat.exceptions import (
UserDontHavePermissionException,
MessageNotFoundException,
UserCanNotReadThisChatException,
)
from app.chat.shemas import (
SMessage,
SLastMessages,
SPinnedChat,
SDeletedUser,
SDeletedChat,
SAllowedChats,
SMessageList,
SPinnedChats,
@ -146,20 +143,21 @@ async def invite_to_chat(
@router.delete(
"/delete_chat/{chat_id}",
status_code=status.HTTP_200_OK,
response_model=SDeletedChat,
response_model=None,
)
async def delete_chat(chat_id: int, user: SUser = Depends(check_verificated_user_with_exc), uow=Depends(UnitOfWork)):
async with uow:
chat = await uow.chat.find_one_or_none(id=chat_id)
if user.id == chat.created_by:
return {"deleted_chat": await uow.chat.delete_chat(chat_id)}
raise UserDontHavePermissionException
if not user.id == chat.created_by:
raise UserDontHavePermissionException
await uow.chat.delete_chat(chat_id)
await uow.commit()
@router.delete(
"/delete_user_from_chat/{chat_id}",
status_code=status.HTTP_200_OK,
response_model=SDeletedUser,
response_model=None,
)
async def delete_user_from_chat(
chat_id: int,
@ -169,9 +167,10 @@ async def delete_user_from_chat(
):
async with uow:
chat = await uow.chat.find_one_or_none(id=chat_id)
if user.id == chat.created_by:
return {"deleted_user": await uow.chat.delete_user(chat_id=chat_id, user_id=user_id)}
raise UserDontHavePermissionException
if not user.id == chat.created_by:
raise UserDontHavePermissionException
await uow.chat.delete_user_from_chat(chat_id=chat_id, user_id=user_id)
await uow.commit()
@router.post(
@ -183,6 +182,7 @@ async def pinn_chat(chat_id: int, user: SUser = Depends(check_verificated_user_w
await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
async with uow:
await uow.chat.pin_chat(chat_id=chat_id, user_id=user.id)
await uow.commit()
return {"chat_id": chat_id, "user_id": user.id}
@ -195,6 +195,7 @@ async def unpinn_chat(chat_id: int, user: SUser = Depends(check_verificated_user
await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
async with uow:
await uow.chat.unpin_chat(chat_id=chat_id, user_id=user.id)
await uow.commit()
return {"chat_id": chat_id, "user_id": user.id}

View file

@ -30,14 +30,6 @@ class SPinnedChat(BaseModel):
chat_id: int
class SDeletedUser(BaseModel):
deleted_user: bool
class SDeletedChat(BaseModel):
deleted_user: bool
class SChat(BaseModel):
chat_id: int
chat_for: int

View file

@ -89,8 +89,8 @@ class ConnectionManager:
async def unpin(self, uow: UnitOfWork, _: int, chat_id: int, message: dict) -> dict:
message = SUnpinMessage.model_validate(message)
unpinned_message = await self.unpin_message(uow=uow, chat_id=chat_id, message_id=message.id)
new_message = {"flag": "unpin", "id": unpinned_message}
await self.unpin_message(uow=uow, chat_id=chat_id, message_id=message.id)
new_message = {"flag": "unpin", "id": message.id}
return new_message
@staticmethod
@ -116,9 +116,8 @@ class ConnectionManager:
return pinned_message
@staticmethod
async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> int:
unpinned_message_id = await MessageService.unpin_message(uow=uow, chat_id=chat_id, message_id=message_id)
return unpinned_message_id
async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> None:
await MessageService.unpin_message(uow=uow, chat_id=chat_id, message_id=message_id)
manager = ConnectionManager()

View file

@ -163,34 +163,24 @@ class ChatDAO(BaseDAO):
)
await self.session.execute(stmt)
async def delete_chat(self, chat_id: int) -> bool:
async def delete_chat(self, chat_id: int) -> None:
query = update(Chats).where(Chats.id == chat_id).values(visibility=False)
await self.session.execute(query)
await self.session.commit()
return True
async def delete_user(self, chat_id: int, user_id: int) -> bool:
async def delete_user_from_chat(self, chat_id: int, user_id: int) -> None:
query = delete(UserChat).where(UserChat.chat_id == chat_id, UserChat.user_id == user_id)
await self.session.execute(query)
await self.session.commit()
return True
async def pin_chat(self, chat_id: int, user_id: int) -> bool:
query = select(PinnedChat.chat_id).where(PinnedChat.user_id == user_id)
result = await self.session.execute(query)
result = result.scalars().all()
if chat_id in result:
async def pin_chat(self, chat_id: int, user_id: int) -> None:
try:
stmt = insert(PinnedChat).values(chat_id=chat_id, user_id=user_id)
await self.session.execute(stmt)
except IntegrityError:
raise UserAlreadyPinnedChatException
stmt = insert(PinnedChat).values(chat_id=chat_id, user_id=user_id)
await self.session.execute(stmt)
await self.session.commit()
return True
async def unpin_chat(self, chat_id: int, user_id: int) -> bool:
async def unpin_chat(self, chat_id: int, user_id: int) -> None:
query = delete(PinnedChat).where(PinnedChat.chat_id == chat_id, PinnedChat.user_id == user_id)
await self.session.execute(query)
await self.session.commit()
return True
async def get_pinned_chats(self, user_id: int):
chats_with_descriptions = (
@ -231,15 +221,13 @@ class ChatDAO(BaseDAO):
result = result.mappings().all()
return result
async def pin_message(self, chat_id: int, message_id: int, user_id: int) -> bool:
query = insert(PinnedMessage).values(chat_id=chat_id, message_id=message_id, user_id=user_id)
await self.session.execute(query)
return True
async def pin_message(self, chat_id: int, message_id: int, user_id: int) -> None:
stmt = insert(PinnedMessage).values(chat_id=chat_id, message_id=message_id, user_id=user_id)
await self.session.execute(stmt)
async def unpin_message(self, chat_id: int, message_id: int) -> bool:
query = delete(PinnedMessage).where(PinnedMessage.chat_id == chat_id, PinnedMessage.message_id == message_id)
await self.session.execute(query)
return True
async def unpin_message(self, chat_id: int, message_id: int) -> None:
stmt = delete(PinnedMessage).where(PinnedMessage.chat_id == chat_id, PinnedMessage.message_id == message_id)
await self.session.execute(stmt)
async def get_pinned_messages(self, chat_id: int) -> list[dict]:
query = (

View file

@ -1,5 +1,5 @@
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import mapped_column
from app.database import Base
@ -7,6 +7,5 @@ from app.database import Base
class PinnedChat(Base):
__tablename__ = "pinned_chat"
id: Mapped[int] = mapped_column(primary_key=True)
user_id = mapped_column(ForeignKey("users.id"))
chat_id = mapped_column(ForeignKey("chat.id"))
user_id = mapped_column(ForeignKey("users.id"), primary_key=True)
chat_id = mapped_column(ForeignKey("chat.id"), primary_key=True)

View file

@ -35,13 +35,13 @@ class MessageService:
@staticmethod
async def pin_message(uow: UnitOfWork, chat_id: int, user_id: int, message_id: int) -> SMessage:
async with uow:
pinned_message = await uow.chat.pin_message(chat_id=chat_id, message_id=message_id, user_id=user_id)
await uow.chat.pin_message(chat_id=chat_id, message_id=message_id, user_id=user_id)
pinned_message = await uow.chat.get_message_by_id(message_id)
await uow.commit()
return pinned_message
@staticmethod
async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> int:
async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> None:
async with uow:
unpinned_message = await uow.chat.unpin_message(chat_id=chat_id, message_id=message_id)
await uow.chat.unpin_message(chat_id=chat_id, message_id=message_id)
await uow.commit()
return unpinned_message