Products Rendering, removal of deprecated files
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
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 services.categories import CategoryService
|
||||
from repositories.products import ProductRepository
|
||||
from services.orders import OrderService
|
||||
|
||||
router = Router()
|
||||
@@ -17,7 +20,7 @@ async def main_menu(msg: Message, state: FSMContext):
|
||||
await state.clear()
|
||||
|
||||
await msg.answer(
|
||||
"hii!", reply_markup=await main_menu_kb(msg.from_user.id)
|
||||
"hii!", reply_markup=main_menu_kb(msg.from_user.id)
|
||||
) # TODO: Write a welcome message
|
||||
|
||||
|
||||
@@ -26,7 +29,7 @@ async def main_menu_cb(cb: CallbackQuery, state: FSMContext):
|
||||
await state.clear()
|
||||
|
||||
await cb.message.edit_text(
|
||||
"hii!", reply_markup=await main_menu_kb(cb.from_user.id)
|
||||
"hii!", reply_markup=main_menu_kb(cb.from_user.id)
|
||||
) # TODO: Write a welcome message
|
||||
|
||||
|
||||
@@ -47,27 +50,91 @@ async def get_orders(
|
||||
async def subcatalogue(
|
||||
cb: CallbackQuery,
|
||||
state: FSMContext,
|
||||
category_service: CategoryService,
|
||||
session: AsyncSession,
|
||||
categories_repo: CategoriesRepository,
|
||||
products_repo: ProductRepository,
|
||||
):
|
||||
await state.clear()
|
||||
|
||||
cat_id = cb.data.split(":")[1]
|
||||
|
||||
if cat_id == "root":
|
||||
children = await category_service.get_children(session)
|
||||
kb = await render_category(children, show_menu=True)
|
||||
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
|
||||
|
||||
else:
|
||||
cat_id = int(cat_id)
|
||||
cat_id = int(cat_id)
|
||||
|
||||
children = await category_service.get_children(session, cat_id)
|
||||
category = await categories_repo.get_category_by_id(session, cat_id)
|
||||
parent_id = category.parent_id or "root"
|
||||
kb = await render_category(children, parent_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,
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user