sqlalchemy is a good orm

This commit is contained in:
2023-06-06 17:55:46 +03:00
parent e37e3db701
commit 6962420e28
5 changed files with 75 additions and 29 deletions

View File

@ -25,9 +25,7 @@ import keyboards
# DB
from db.base import Session, engine, Base
from db.models import User
# from db.engine import Database
# from sqlalchemy import select
from db.models import User, Queue, QueueUser
bot = AsyncTeleBot(token, state_storage=StatePickleStorage())
@ -47,12 +45,44 @@ async def start(msg: Message):
session.add(new_user)
session.commit()
await bot.send_message(chat_id=msg.chat.id, text="Регистрация прошла успешно, добро пожаловать!")
print(session.query(User).all(), flush=True)
# if msg.chat.type in ("group", "supergroup"):
# await bot.send_message(chat_id=msg.chat.id, text=textbook.start_group)
# else:
# await bot.send_message(chat_id=msg.chat.id, text=textbook.start)
@bot.message_handler(commands=["new_queue"])
async def nq(msg: Message):
user = session.query(User).filter_by(id=msg.from_user.id).first()
queue = Queue(owner_id=user.id)
session.add(queue)
session.commit()
await bot.send_message(chat_id=msg.chat.id, text=f"Создана новая очередь: {queue.id}")
@bot.message_handler(commands=["take_part"])
async def tp(msg: Message):
try:
command, queue_id = msg.text.split()
except:
await bot.send_message(chat_id=msg.chat.id, text="Вы забыли указать очередь")
return
qu = QueueUser(user_id=msg.from_user.id, queue_id=queue_id)
session.add(qu)
session.commit()
await bot.send_message(chat_id=msg.chat.id, text="Вы приняли участие в очереди!")
@bot.message_handler(commands=["queue"])
async def q(msg: Message):
try:
command, queue_id = msg.text.split()
except:
await bot.send_message(chat_id=msg.chat.id, text="Вы забыли указать очередь")
return
queue = session.query(Queue).filter_by(id=queue_id).first()
if queue:
users = [q.user.name for q in queue.users]
await bot.send_message(chat_id=msg.chat.id, text=f"Пользователи этой очереди: {', '.join(users)}")
else:
await bot.send_message(chat_id=msg.chat.id, text="Очередь не найдена!")
async def main():
a = asyncio.create_task(bot.polling(non_stop=True))
@ -61,7 +91,6 @@ async def main():
if __name__ == "__main__":
print("Bot started", flush=True)
# db = Database()
Base.metadata.create_all(engine)
session = Session()
bot.add_custom_filter(StateFilter(bot))