Skip to content

Commit

Permalink
Character: Added Favorite Lines to Char Model
Browse files Browse the repository at this point in the history
  • Loading branch information
BertiRean committed Oct 25, 2023
1 parent 2b93452 commit c75f9c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/models/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
28 changes: 27 additions & 1 deletion app/routes/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()


0 comments on commit c75f9c0

Please sign in to comment.