diff --git a/fia_api/tests/test_flashcards.py b/fia_api/tests/test_flashcards.py index fc329e5..8f9241c 100644 --- a/fia_api/tests/test_flashcards.py +++ b/fia_api/tests/test_flashcards.py @@ -75,6 +75,9 @@ async def test_flashcards( get_flashcards_url = fastapi_app.url_path_for("get_flashcards") update_flashcard_url = fastapi_app.url_path_for("update_flashcard") delete_flashcard_url = fastapi_app.url_path_for("delete_flashcard") + create_flashcard_url = fastapi_app.url_path_for("create_flashcard") + + conversation_id = str(uuid.uuid4()) # No flashcards by default: response = await client.get( @@ -88,7 +91,7 @@ async def test_flashcards( username, "front of card", "back of card", - str(uuid.uuid4()), + conversation_id, explanation="Explainer", ) @@ -153,3 +156,21 @@ async def test_flashcards( headers=auth_headers, ) assert len(response.json()["flashcards"]) == 2 + + # Manually create one: + response = await client.post( + create_flashcard_url, + headers=auth_headers, + json={ + "conversation_id": conversation_id, + "front": "FFF", + "back": "BBB", + "both_sides": True, + }, + ) + + response = await client.get( + get_flashcards_url, + headers=auth_headers, + ) + assert len(response.json()["flashcards"]) == 4 diff --git a/fia_api/web/api/flashcards/schema.py b/fia_api/web/api/flashcards/schema.py index 5a2ef5f..0e82975 100644 --- a/fia_api/web/api/flashcards/schema.py +++ b/fia_api/web/api/flashcards/schema.py @@ -16,6 +16,15 @@ class Flashcard(BaseModel): last_reviewed_date: datetime +class CreateFlashcardRequest(BaseModel): + """Request object for manually creating a flashcard.""" + + conversation_id: str + front: str + back: str + both_sides: Optional[bool] = False + + class UpdateFlashcardRequest(BaseModel): """Request object for updating a flashcard.""" diff --git a/fia_api/web/api/flashcards/utils.py b/fia_api/web/api/flashcards/utils.py index 09af0d2..72b0596 100644 --- a/fia_api/web/api/flashcards/utils.py +++ b/fia_api/web/api/flashcards/utils.py @@ -12,7 +12,7 @@ async def create_flashcard( # noqa: WPS211 back: str, conversation_id: str, explanation: Optional[str] = None, - both_sides: bool = False, + both_sides: Optional[bool] = False, ) -> None: """ Create a flashcard given the params. diff --git a/fia_api/web/api/flashcards/views.py b/fia_api/web/api/flashcards/views.py index 09c93c8..a97d92c 100644 --- a/fia_api/web/api/flashcards/views.py +++ b/fia_api/web/api/flashcards/views.py @@ -5,10 +5,12 @@ from fia_api.db.models.flashcard_model import FLASHCARD_EASE, FlashcardModel from fia_api.db.models.user_model import UserModel from fia_api.web.api.flashcards.schema import ( + CreateFlashcardRequest, DeleteFlashcardRequest, GetFlashcardsResponse, UpdateFlashcardRequest, ) +from fia_api.web.api.flashcards.utils import create_flashcard as create_flashcard_util from fia_api.web.api.flashcards.utils import format_flashcards_for_response from fia_api.web.api.user.schema import AuthenticatedUser from fia_api.web.api.user.utils import get_current_user @@ -101,3 +103,23 @@ async def delete_flashcard( ) await flashcard.delete() + + +@router.post("/create-flashcard", status_code=200) # noqa: WPS432 +async def create_flashcard( + create_flashcard_request: CreateFlashcardRequest, + user: AuthenticatedUser = Depends(get_current_user), +) -> None: + """ + Create a flashcard. + + :param create_flashcard_request: The flashcard ID to create. + :param user: The AuthenticatedUser making the request. + """ + await create_flashcard_util( + user.username, + create_flashcard_request.front, + create_flashcard_request.back, + create_flashcard_request.conversation_id, + both_sides=create_flashcard_request.both_sides, + )