Третий стрим
This commit is contained in:
50
app/utils/db_actions.py
Normal file
50
app/utils/db_actions.py
Normal file
@ -0,0 +1,50 @@
|
||||
from sqlalchemy import select, insert, delete
|
||||
|
||||
from app.models import User, Favorites
|
||||
from app.schemas.favorites_schema import FavoritesSchemaOutput
|
||||
from app.schemas.user_schema import UserSchemaOutput
|
||||
from app.settings import sessionmaker
|
||||
|
||||
|
||||
async def get_user(telegram_id: int):
|
||||
async with sessionmaker() as session:
|
||||
query = select(User).where(User.telegram_id == telegram_id)
|
||||
result = await session.execute(query)
|
||||
user = result.scalar_one_or_none()
|
||||
if user:
|
||||
return UserSchemaOutput(**user.__dict__)
|
||||
return user
|
||||
|
||||
|
||||
async def create_user(telegram_id: int):
|
||||
async with sessionmaker() as session:
|
||||
query = insert(User).values(telegram_id=telegram_id).returning(User.id, User.telegram_id, User.added_at)
|
||||
result = await session.execute(query)
|
||||
await session.commit()
|
||||
user = result.mappings().first()
|
||||
return UserSchemaOutput(**user)
|
||||
|
||||
|
||||
async def get_favorites(user_id: int):
|
||||
async with sessionmaker() as session:
|
||||
query = select(Favorites).where(Favorites.user_id == user_id)
|
||||
result = await session.execute(query)
|
||||
favorites = result.scalars().all()
|
||||
return [FavoritesSchemaOutput(**favorite.__dict__) for favorite in favorites]
|
||||
|
||||
|
||||
async def add_favorite(user_id: int, command: str):
|
||||
async with sessionmaker() as session:
|
||||
query = insert(Favorites).values(user_id=user_id, command=command).returning(Favorites.id, Favorites.user_id,
|
||||
Favorites.command)
|
||||
result = await session.execute(query)
|
||||
await session.commit()
|
||||
favorite = result.mappings().first()
|
||||
return FavoritesSchemaOutput(**favorite)
|
||||
|
||||
|
||||
async def del_favorite(favorite_id: int):
|
||||
async with sessionmaker() as session:
|
||||
query = delete(Favorites).where(Favorites.id == favorite_id)
|
||||
await session.execute(query)
|
||||
await session.commit()
|
@ -3,3 +3,8 @@ from aiogram.fsm.state import StatesGroup, State
|
||||
|
||||
class ExecuteCommandsSteps(StatesGroup):
|
||||
EXECUTE = State()
|
||||
|
||||
|
||||
class FavoritesCommandsSteps(StatesGroup):
|
||||
ADD = State()
|
||||
DEL = State()
|
20
app/utils/text_splitter.py
Normal file
20
app/utils/text_splitter.py
Normal file
@ -0,0 +1,20 @@
|
||||
import re
|
||||
|
||||
|
||||
def split_text(message: str):
|
||||
messages = []
|
||||
lines = re.split(r"\n", message)
|
||||
|
||||
temp = ""
|
||||
|
||||
for line in lines:
|
||||
if len(temp) + len(line) < 4096:
|
||||
temp += line + "\n"
|
||||
else:
|
||||
messages.append(temp)
|
||||
temp = line + "\n"
|
||||
|
||||
if temp:
|
||||
messages.append(temp)
|
||||
|
||||
return messages
|
Reference in New Issue
Block a user