From 3cc8dce15d33a7016a4096bcf51bbe15ff9149a6 Mon Sep 17 00:00:00 2001 From: Vladislav Ponomarev Date: Mon, 16 Sep 2024 20:44:00 +0700 Subject: [PATCH] Move out code about moving sticker into interactor --- frontend/src/app/stickerpack/[name]/page.tsx | 15 ++++--- .../api/sticker_controller.py | 45 ++++++++----------- .../move_sticker_to_another_set.py | 36 +++++++++++++++ 3 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 src/telegram_stickers_organizer/interactors/move_sticker_to_another_set.py diff --git a/frontend/src/app/stickerpack/[name]/page.tsx b/frontend/src/app/stickerpack/[name]/page.tsx index 6dbab2f..559ecb3 100644 --- a/frontend/src/app/stickerpack/[name]/page.tsx +++ b/frontend/src/app/stickerpack/[name]/page.tsx @@ -81,18 +81,23 @@ export default function StickerPackDetail() { file_id: selectedStickerForMove, user_id: stickerPack.user_id }); + if (response.data.success) { + // Refresh the sticker pack data after successful move + const updatedPackResponse = await axios.get(`http://localhost:8000/api/stickerpack/${name}`); + setStickerPack(updatedPackResponse.data); setSelectedStickerForMove(null); - setStickerPacksForMove([]); - const updatedPack = await axios.get(`http://localhost:8000/api/stickerpack/${stickerPack.name}`); - setStickerPack(updatedPack.data); - } else { - console.error(`Error moving sticker to pack: ${destinationPack.name}`, response.data.error); } } catch (error) { + if (axios.isAxiosError(error) && error.response) { + alert(`Error moving sticker: ${error.response.data.error}`); + } else { + alert('An unexpected error occurred while moving the sticker'); + } console.error(`Error moving sticker to pack: ${destinationPack.name}`, error); } } + setSelectedStickerForMove(null); }; return ( diff --git a/src/telegram_stickers_organizer/api/sticker_controller.py b/src/telegram_stickers_organizer/api/sticker_controller.py index 071d0a5..0af16f5 100644 --- a/src/telegram_stickers_organizer/api/sticker_controller.py +++ b/src/telegram_stickers_organizer/api/sticker_controller.py @@ -6,11 +6,10 @@ ) from telegram_stickers_organizer.utils.sticker_helpers import ( get_sticker_set, - add_stickers_to_set, ) from telegram_stickers_organizer.dispatcher import bot from telegram_stickers_organizer.interactors.download_sticker import download_sticker -import logging +from ..interactors.move_sticker_to_another_set import move_sticker_to_another_set async def get_stickerpacks(request): @@ -80,7 +79,16 @@ async def get_stickerpack_preview(request): async def get_all_sticker_sets(request): sticker_sets = db_get_all_sticker_sets() - return web.json_response(sticker_sets) + + response_data = [ + { + "user_id": sticker_set["user_id"], + "name": sticker_set["set_name"], + "title": sticker_set["title"], + } + for sticker_set in sticker_sets + ] + return web.json_response(response_data) async def delete_sticker_set(request): @@ -102,35 +110,20 @@ async def move_sticker(request): file_id = data.get("file_id") user_id = data.get("user_id") - if not all([source_pack, destination_pack, file_id]): + if not all([source_pack, destination_pack, file_id, user_id]): return web.json_response( { "success": False, - "error": "source_pack, destination_pack, and file_id are required", + "error": "source_pack, destination_pack, file_id, and user_id are required", }, status=400, ) - try: - # Get the source sticker set - source_set = await get_sticker_set(source_pack) - - # Find the sticker with the given file_id in the source set - sticker = next((s for s in source_set.stickers if s.file_id == file_id), None) - - if not sticker: - return web.json_response( - {"success": False, "error": "Sticker not found in the source pack"}, - status=404, - ) - - # Add the sticker to the destination pack - await add_stickers_to_set(user_id, destination_pack, [sticker]) - - # Remove the sticker from the source pack - await bot.delete_sticker_from_set(file_id) + success, error = await move_sticker_to_another_set( + source_pack, destination_pack, file_id, user_id + ) + if success: return web.json_response({"success": True}) - except Exception as e: - logging.error(f"Error moving sticker: {e}") - return web.json_response({"success": False, "error": str(e)}, status=500) + else: + return web.json_response({"success": False, "error": error}, status=400) diff --git a/src/telegram_stickers_organizer/interactors/move_sticker_to_another_set.py b/src/telegram_stickers_organizer/interactors/move_sticker_to_another_set.py new file mode 100644 index 0000000..5e48dee --- /dev/null +++ b/src/telegram_stickers_organizer/interactors/move_sticker_to_another_set.py @@ -0,0 +1,36 @@ +from ..utils.sticker_helpers import get_sticker_set, add_stickers_to_set +from ..dispatcher import bot +from ..constants import MAX_STICKERS_IN_STICKERSET +import logging + + +async def move_sticker_to_another_set( + source_pack: str, destination_pack: str, file_id: str, user_id: int +): + try: + # Get the source sticker set + source_set = await get_sticker_set(source_pack) + + # Find the sticker with the given file_id in the source set + sticker = next((s for s in source_set.stickers if s.file_id == file_id), None) + + if not sticker: + return False, "Sticker not found in the source pack" + + # Get the destination sticker set + destination_set = await get_sticker_set(destination_pack) + + # Check if the destination pack is full + if len(destination_set.stickers) >= MAX_STICKERS_IN_STICKERSET: + return False, "Destination pack is full (maximum 120 stickers)" + + # Add the sticker to the destination pack + await add_stickers_to_set(user_id, destination_pack, [sticker]) + + # Remove the sticker from the source pack + await bot.delete_sticker_from_set(file_id) + + return True, None + except Exception as e: + logging.error(f"Error moving sticker: {e}") + return False, str(e)