bot rafactor and bugfix

This commit is contained in:
2025-08-19 04:45:16 +09:00
parent 43dda889f8
commit a8d860ed87
31 changed files with 4396 additions and 613 deletions

93
app/services/channels.py Normal file
View File

@@ -0,0 +1,93 @@
"""Сервис для работы с каналами."""
from typing import List, Optional
from sqlalchemy import select
from app.models.channel import Channel, BotChannel
from app.models.bot import Bot
from app.db.session import async_session_maker
class ChannelService:
"""Сервис для работы с каналами."""
@staticmethod
async def get_user_channels(user_id: int) -> List[Channel]:
"""Получает список каналов пользователя."""
async with async_session_maker() as session:
stmt = select(Channel).where(Channel.owner_id == user_id)
result = await session.execute(stmt)
return list(result.scalars().all())
@staticmethod
async def get_channel(channel_id: int) -> Optional[Channel]:
"""Получает канал по ID."""
async with async_session_maker() as session:
stmt = select(Channel).where(Channel.id == channel_id)
result = await session.execute(stmt)
return result.scalars().first()
@staticmethod
async def get_bot_channels(bot_id: int) -> List[Channel]:
"""Получает список каналов бота."""
async with async_session_maker() as session:
stmt = select(Channel).where(Channel.bot_id == bot_id)
result = await session.execute(stmt)
return list(result.scalars().all())
@staticmethod
async def add_channel(
owner_id: int,
bot_id: int,
chat_id: int,
title: Optional[str] = None,
username: Optional[str] = None
) -> Channel:
"""Добавляет новый канал."""
async with async_session_maker() as session:
channel = Channel(
owner_id=owner_id,
bot_id=bot_id,
chat_id=chat_id,
title=title,
username=username
)
session.add(channel)
await session.commit()
await session.refresh(channel)
return channel
@staticmethod
async def update_channel(
channel_id: int,
title: Optional[str] = None,
username: Optional[str] = None
) -> bool:
"""Обновляет данные канала."""
async with async_session_maker() as session:
stmt = select(Channel).where(Channel.id == channel_id)
result = await session.execute(stmt)
channel = result.scalars().first()
if not channel:
return False
if title is not None:
channel.title = title
if username is not None:
channel.username = username
await session.commit()
return True
@staticmethod
async def delete_channel(channel_id: int) -> bool:
"""Удаляет канал."""
async with async_session_maker() as session:
stmt = select(Channel).where(Channel.id == channel_id)
result = await session.execute(stmt)
channel = result.scalars().first()
if not channel:
return False
await session.delete(channel)
await session.commit()
return True