Skip to content

Commit

Permalink
Move out code about moving sticker into interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayurifag committed Sep 16, 2024
1 parent 205de31 commit 3cc8dce
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
15 changes: 10 additions & 5 deletions frontend/src/app/stickerpack/[name]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
45 changes: 19 additions & 26 deletions src/telegram_stickers_organizer/api/sticker_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3cc8dce

Please sign in to comment.