import io from aiogram import types from load import dp, messages from utils.database.cart import add_to_cart, get_user_cart, del_from_cart from utils.database.market import Catalog from keyboard.inline.cart import cart_list from keyboard.default.main_menu import back_to_main_menu @dp.callback_query_handler(lambda c: c.data.split("|")[0] == "add_to_cart") async def add_cart(callback: types.CallbackQuery): item = int(callback.data.split("|")[1]) item_id = Catalog.get_catalog()[item]["id"] add_to_cart(callback.from_user.id, item_id) await callback.answer("Товар добавлен!") @dp.callback_query_handler(lambda c: c.data.split("|")[0] == "del_from_cart") async def del_cart(callback: types.CallbackQuery): c = callback.data.split("|")[1] item_id = get_user_cart(callback.from_user.id)[int(c)][0] del_from_cart(callback.from_user.id, item_id) items = get_user_cart(callback.from_user.id) if items: index, count = items[0] item = Catalog.get_catalog(index) await callback.message.edit_media( media=types.InputMediaPhoto( media=io.BytesIO(item["image"]), caption=messages.cart_message.format( name=item["name"], description=item["description"], count=count, price=item['price'] * count ), parse_mode="Markdown", ), reply_markup=cart_list(items=len(items) - 1) ) else: await callback.message.delete() await dp.bot.send_message( callback.message.chat.id, messages.cart + " пустая!", reply_markup=back_to_main_menu ) await callback.answer("Товар удалён!") @dp.callback_query_handler(lambda c: c.data.split("|")[0] in ["cart_prev", "cart_next"]) async def next_item(callback: types.CallbackQuery): data = callback.data.split("|") c = int(data[1]) items = get_user_cart(callback.from_user.id) index, count = items[c] item = Catalog.get_catalog(index) await callback.message.edit_media( media=types.InputMediaPhoto( media=io.BytesIO(item["image"]), caption=messages.cart_message.format( name=item["name"], description=item["description"], count=count, price=item['price'] * count ), parse_mode="Markdown", ), reply_markup=cart_list(c, len(items) - 1) )