Skip to content

Commit

Permalink
Add db session injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamroy95 committed Nov 22, 2023
1 parent 69446c3 commit 469bbf4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/middlewares/db_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from aiogram import BaseMiddleware, types
from aiogram.dispatcher.event.bases import CancelHandler
from aiogram.types import TelegramObject
from tortoise import BaseDBAsyncClient
from tortoise.transactions import in_transaction

from app.infrastructure.database.models import User
from app.infrastructure.database.repo.chat import ChatRepo
Expand All @@ -30,14 +32,20 @@ async def __call__(
user: types.User = data.get("event_from_user", None)
if isinstance(event, types.Message) and event.sender_chat:
raise CancelHandler
await self.setup_chat(data, user, chat)
return await handler(event, data)

async with in_transaction() as session:
await self.setup_chat(session, data, user, chat)
return await handler(event, data)

async def setup_chat(
self, data: dict, user: types.User, chat: Optional[types.Chat] = None
self,
session: BaseDBAsyncClient,
data: dict,
user: types.User,
chat: Optional[types.Chat] = None,
):
try:
chat_repo = ChatRepo()
chat_repo = ChatRepo(session)

async with self.lock_factory.get_lock(user.id):
user = await User.get_or_create_from_tg_user(user)
Expand Down

0 comments on commit 469bbf4

Please sign in to comment.