fix: lazy load

This commit is contained in:
Artem Tsyrulnikov
2025-11-10 16:31:02 +03:00
parent b43042c745
commit f8edd81395
3 changed files with 21 additions and 9 deletions

View File

@@ -1 +0,0 @@
"""tgex backend package."""

View File

@@ -135,7 +135,14 @@ class Postgres(DatabaseBase):
async def add_external_channel_to_targets(
self, external_channel_id: uuid.UUID, target_channel_ids: list[uuid.UUID]
) -> None:
external_channel = await self.session.get(domain.ExternalChannel, external_channel_id)
stmt = (
select(domain.ExternalChannel)
.where(domain.ExternalChannel.id == external_channel_id)
.options(selectinload(domain.ExternalChannel.target_channels))
)
result = await self.session.execute(stmt)
external_channel = result.scalar_one_or_none()
if not external_channel:
raise ValueError(f'ExternalChannel {external_channel_id} not found')
@@ -149,7 +156,14 @@ class Postgres(DatabaseBase):
async def remove_external_channel_from_target(
self, external_channel_id: uuid.UUID, target_channel_id: uuid.UUID
) -> None:
external_channel = await self.session.get(domain.ExternalChannel, external_channel_id)
stmt = (
select(domain.ExternalChannel)
.where(domain.ExternalChannel.id == external_channel_id)
.options(selectinload(domain.ExternalChannel.target_channels))
)
result = await self.session.execute(stmt)
external_channel = result.scalar_one_or_none()
if not external_channel:
raise ValueError(f'ExternalChannel {external_channel_id} not found')

View File

@@ -10,13 +10,12 @@ log = logging.getLogger(__name__)
async def disconnect_target_chan_by_tg_id(self: 'Usecase', input: dto.DisconnectTargetChanByTgIdInput) -> None:
# Получаем user_id по telegram_id
user = await self.database.get_user(telegram_id=input.user_telegram_id)
if not user:
log.warning(f'User with telegram_id {input.user_telegram_id} not found when disconnecting channel')
return
async with self.database.transaction():
user = await self.database.get_user(telegram_id=input.user_telegram_id)
if not user:
log.warning(f'User with telegram_id {input.user_telegram_id} not found when disconnecting channel')
return
channel = await self.database.get_target_channel(user.id, telegram_id=input.telegram_id)
if not channel:
log.warning(f'Target channel {input.telegram_id} not found')