From c75f9c004b34877bedf7297691a17fc479ed9f5c Mon Sep 17 00:00:00 2001 From: BertiRean Date: Tue, 24 Oct 2023 22:24:31 -0300 Subject: [PATCH] Character: Added Favorite Lines to Char Model --- app/models/character.py | 8 ++++++++ app/routes/character.py | 28 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/models/character.py b/app/models/character.py index f1c9fde..bf6fb39 100644 --- a/app/models/character.py +++ b/app/models/character.py @@ -11,6 +11,7 @@ class Character(Document): traits: List[str] image: Optional[str] = None last_update : datetime = datetime.now() + favorite_dialogues : List[str] = [] class Settings: name = "characters" @@ -75,3 +76,10 @@ class UserCharactersResponse(BaseModel): class DeleteCharacterBody(BaseModel): userId: PydanticObjectId + +class UpdateCharacterLineFavorite(BaseModel): + line : str + favorite : bool + +class UpdateCharacterLineFavoriteResponse(BaseModel): + status : str = "success" \ No newline at end of file diff --git a/app/routes/character.py b/app/routes/character.py index 4c45569..3dd50a6 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, + DeleteCharacterResponse, UpdateCharacter, UpdateCharacterLineFavorite, UpdateCharacterLineFavoriteResponse, UserCharactersResponse) from models.user import User @@ -173,3 +173,29 @@ async def delete_character(characterId: PydanticObjectId, payload: DeleteCharact status_code=status.HTTP_400_BAD_REQUEST, detail="Error in delete character process", ) + +@router.put( + "/{characterId}/favorite", + response_description="Mark a dialogue line as favorite", + response_model=UpdateCharacterLineFavoriteResponse +) +async def mark_favorite_line(characterId : PydanticObjectId, payload : UpdateCharacterLineFavorite): + character = await Character.get(characterId) + + if not character: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=f"Character with {characterId} not found", + ) + + update_query = "" + if payload.favorite: + update_query = { "$push" : {"favorite_dialogues" : payload.line} } + else: + update_query = {"$pull" : {"favorite_dialogues" : payload.line}} + + await character.update(update_query) + + return UpdateCharacterLineFavoriteResponse() + + \ No newline at end of file