Skip to content

Commit

Permalink
Supports manual flashcard creation. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
conor-f committed Sep 25, 2023
1 parent a6c22ba commit 04048de
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
23 changes: 22 additions & 1 deletion fia_api/tests/test_flashcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -88,7 +91,7 @@ async def test_flashcards(
username,
"front of card",
"back of card",
str(uuid.uuid4()),
conversation_id,
explanation="Explainer",
)

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions fia_api/web/api/flashcards/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
2 changes: 1 addition & 1 deletion fia_api/web/api/flashcards/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions fia_api/web/api/flashcards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

0 comments on commit 04048de

Please sign in to comment.