Skip to content

Commit

Permalink
Replace Chat model usages with ChatRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamroy95 committed Nov 15, 2023
1 parent 19e280d commit 69446c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aiogram.utils.markdown import hbold, hpre

from app.infrastructure.database.models import Chat
from app.infrastructure.database.repo.chat import ChatRepo
from app.utils.log import Logger

logger = Logger(__name__)
Expand Down Expand Up @@ -83,9 +84,9 @@ async def cancel_state(message: types.Message, state: FSMContext):


@router.message(F.message.content_types == types.ContentType.MIGRATE_TO_CHAT_ID)
async def chat_migrate(message: types.Message, chat: Chat):
async def chat_migrate(message: types.Message, chat: Chat, chat_repo: ChatRepo):
old_id = message.chat.id
new_id = message.migrate_to_chat_id
chat.chat_id = new_id
await chat.save()
await chat_repo.update(chat)
logger.info(f"Migrate chat from {old_id} to {new_id}")
5 changes: 3 additions & 2 deletions app/handlers/karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aiogram.utils.text_decorations import html_decoration as hd

from app.infrastructure.database.models import Chat, User
from app.infrastructure.database.repo.chat import ChatRepo
from app.models.config import Config
from app.services.karma import get_me_chat_info, get_me_info
from app.services.karma import get_top as get_karma_top
Expand All @@ -16,10 +17,10 @@


@router.message(Command("top", prefix="!"), F.chat.type == "private")
async def get_top_from_private(message: types.Message, user: User):
async def get_top_from_private(message: types.Message, user: User, chat_repo: ChatRepo):
parts = message.text.split(maxsplit=1)
if len(parts) > 1:
chat = await Chat.get(chat_id=int(parts[1]))
chat = await chat_repo.get_by_id(chat_id=int(parts[1]))
else:
return await message.reply(
"Эту команду можно использовать только в группах "
Expand Down
6 changes: 6 additions & 0 deletions app/infrastructure/database/repo/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class ChatRepo:
def __init__(self, session: BaseDBAsyncClient | None = None):
self.session = session

async def get_by_id(self, chat_id: int) -> Chat:
return await Chat.get(chat_id=chat_id, using_db=self.session)

async def update(self, chat: Chat):
await chat.save(using_db=self.session)

async def create_from_tg_chat(self, chat) -> Chat:
chat = await Chat.create(
chat_id=chat.id,
Expand Down

0 comments on commit 69446c3

Please sign in to comment.