From 0c64280a419b4b3e384d30b6061429462a832b39 Mon Sep 17 00:00:00 2001 From: BertiRean Date: Tue, 24 Oct 2023 23:02:48 -0300 Subject: [PATCH] Characters: Added Route for get favorite lines --- app/models/character.py | 6 +++++- app/routes/character.py | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/models/character.py b/app/models/character.py index bf6fb39..cb835dc 100644 --- a/app/models/character.py +++ b/app/models/character.py @@ -82,4 +82,8 @@ class UpdateCharacterLineFavorite(BaseModel): favorite : bool class UpdateCharacterLineFavoriteResponse(BaseModel): - status : str = "success" \ No newline at end of file + status : str = "success" + +class ExportCharacterLinesResponse(BaseModel): + status : str = "success" + lines : dict \ No newline at end of file diff --git a/app/routes/character.py b/app/routes/character.py index 3dd50a6..40258f0 100644 --- a/app/routes/character.py +++ b/app/routes/character.py @@ -11,7 +11,7 @@ from fastapi.encoders import jsonable_encoder from models.character import (Character, CharacterDataResponse, CharacterResponse, DeleteCharacterBody, - DeleteCharacterResponse, UpdateCharacter, UpdateCharacterLineFavorite, UpdateCharacterLineFavoriteResponse, + DeleteCharacterResponse, ExportCharacterLinesResponse, UpdateCharacter, UpdateCharacterLineFavorite, UpdateCharacterLineFavoriteResponse, UserCharactersResponse) from models.user import User @@ -198,4 +198,30 @@ async def mark_favorite_line(characterId : PydanticObjectId, payload : UpdateCha return UpdateCharacterLineFavoriteResponse() + +@router.get( + "/export-dialogues/{userId}", + response_description="Get a Json File with all dialogues of characters", + response_model=ExportCharacterLinesResponse, +) +async def export_character_lines(userId : PydanticObjectId): + user = await User.find_one(User.id == userId, fetch_links=True) + + if not user: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=f"User with id {userId} not found", + ) + + lines = {} + for char in user.characters: + if len(char.favorite_dialogues) == 0: + continue + lines[char.name] = [] + for line in char.favorite_dialogues: + lines[char.name].append(line) + + + return ExportCharacterLinesResponse(status="success", lines=lines) + \ No newline at end of file