Files
shveitechbot/handlers/client.py

141 lines
3.9 KiB
Python

from re import Match
from aiogram import Router, F
from aiogram.types import CallbackQuery, Message
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from sqlalchemy.ext.asyncio import AsyncSession
from config import PAGE_SIZE
from misc.kb import main_menu_kb, render_category
from misc.kb.client import get_back_to_catalogue, render_products
from repositories.categories import CategoriesRepository
from repositories.products import ProductRepository
from services.orders import OrderService
router = Router()
@router.message(Command("start"))
async def main_menu(msg: Message, state: FSMContext):
await state.clear()
await msg.answer(
"hii!", reply_markup=main_menu_kb(msg.from_user.id)
) # TODO: Write a welcome message
@router.callback_query(F.data.startswith("menu:"))
async def main_menu_cb(cb: CallbackQuery, state: FSMContext):
await state.clear()
await cb.message.edit_text(
"hii!", reply_markup=main_menu_kb(cb.from_user.id)
) # TODO: Write a welcome message
@router.message(Command("orders"))
async def get_orders(
msg: Message,
state: FSMContext,
order_service: OrderService,
session: AsyncSession,
):
await state.clear()
count = await order_service.get_order_count(session, msg.from_user.id)
await msg.answer(f"you have {count} orders.")
@router.callback_query(F.data.startswith("cat:"))
async def subcatalogue(
cb: CallbackQuery,
state: FSMContext,
session: AsyncSession,
categories_repo: CategoriesRepository,
products_repo: ProductRepository,
):
await state.clear()
cat_id = cb.data.split(":")[1]
if cat_id == "root":
children = await categories_repo.get_categories_by_parent_id(session)
kb = render_category(children, show_menu=True)
await cb.message.edit_text(
"category selection",
reply_markup=kb,
)
return
cat_id = int(cat_id)
children = await categories_repo.get_categories_by_parent_id(session, cat_id)
category = await categories_repo.get_category_by_id(session, cat_id)
parent_id = category.parent_id or "root"
if not children:
products = await products_repo.get_product_by_category(
session, cat_id, limit=PAGE_SIZE + 1, offset=0
)
products_len = len(products)
if not products:
await cb.message.edit_text(
"🍃 Здесь ничего нет...",
reply_markup=get_back_to_catalogue(parent_id),
)
return
await cb.message.edit_text(
"hya",
reply_markup=render_products(
products[:PAGE_SIZE],
cat_id=cat_id,
page=0,
show_prev=products_len < 0,
show_next=products_len > PAGE_SIZE,
),
)
return
kb = render_category(children, parent_id)
await cb.message.edit_text(
"category selection",
reply_markup=kb,
)
@router.callback_query(
F.data.regexp(r"products:(?P<category_id>.*):(?P<page_id>\d+)").as_(
"pagination_match"
)
)
async def products_pagination(
cb: CallbackQuery,
state: FSMContext,
pagination_match: Match[str],
session: AsyncSession,
products_repo: ProductRepository,
):
await state.clear()
pagination_data = pagination_match.groupdict()
category_id = int(pagination_data.get("category_id"))
page_id = int(pagination_data.get("page_id", 0))
products = await products_repo.get_product_by_category(
session, category_id, limit=PAGE_SIZE + 1, offset=page_id * PAGE_SIZE
)
products_len = len(products)
await cb.message.edit_reply_markup(
reply_markup=render_products(
products[:PAGE_SIZE],
cat_id=category_id,
page=page_id,
show_prev=page_id > 0,
show_next=products_len > PAGE_SIZE,
),
)