Skip to content

Commit

Permalink
Add AllCoffeeDonations schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbirchard committed Aug 3, 2024
1 parent c16b30d commit 06c4186
Show file tree
Hide file tree
Showing 6 changed files with 788 additions and 1,332 deletions.
15 changes: 6 additions & 9 deletions app/donations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from database import get_db
from database.crud import create_donation, get_donation
from database.models import Donation
from database.schemas import NewDonation
from database.schemas import CoffeeDonation, AllCoffeeDonations

router = APIRouter(prefix="/donation", tags=["donations"])

Expand All @@ -16,9 +16,9 @@
"/",
summary="New BuyMeACoffee donation",
description="Save record of new donation to persistent ledger.",
response_model=NewDonation,
response_model=CoffeeDonation,
)
async def accept_donation(donation: NewDonation, db: Session = Depends(get_db)) -> NewDonation:
async def accept_donation(donation: CoffeeDonation, db: Session = Depends(get_db)) -> CoffeeDonation:
"""
Save BuyMeACoffee donation to database.
Expand All @@ -40,9 +40,9 @@ async def accept_donation(donation: NewDonation, db: Session = Depends(get_db))
"/",
summary="Delete BuyMeACoffee donation record",
description="Delete BuyMeACoffee donation transaction by ID.",
response_model=NewDonation,
response_model=CoffeeDonation,
)
async def delete_donation(donation: NewDonation, db: Session = Depends(get_db)) -> NewDonation:
async def delete_donation(donation: CoffeeDonation, db: Session = Depends(get_db)) -> CoffeeDonation:
"""
Delete BuyMeACoffee donation from database.
Expand All @@ -60,10 +60,7 @@ async def delete_donation(donation: NewDonation, db: Session = Depends(get_db))
return create_donation(db, donation)


@router.get(
"/",
summary="Get all existing donations.",
)
@router.get("/", summary="Get all existing donations.", response_model=AllCoffeeDonations)
async def get_donations(db: Session = Depends(get_db)):
"""
Test endpoint for fetching comments joined with user info.
Expand Down
6 changes: 3 additions & 3 deletions database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from sqlalchemy.orm import Session

from database.models import Account, Donation
from database.schemas import NewDonation
from database.schemas import CoffeeDonation
from log import LOGGER


def get_donation(db: Session, donation: NewDonation) -> Optional[NewDonation]:
def get_donation(db: Session, donation: CoffeeDonation) -> Optional[CoffeeDonation]:
"""
Fetch BuyMeACoffee donation by ID.
Expand All @@ -26,7 +26,7 @@ def get_donation(db: Session, donation: NewDonation) -> Optional[NewDonation]:
return None


def create_donation(db: Session, donation: NewDonation) -> Donation:
def create_donation(db: Session, donation: CoffeeDonation) -> Donation:
"""
Create new BuyMeACoffee donation record.
Expand Down
32 changes: 30 additions & 2 deletions database/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import BaseModel, EmailStr, Field


class NewDonation(BaseModel):
class CoffeeDonation(BaseModel):
"""`BuyMeACoffee` donation."""

# fmt: off
Expand All @@ -24,11 +24,39 @@ class Config:
"email": "fake@example.com",
"count": 1,
"message": "Great tutorials but this is a test message.",
"link": "https://buymeacoffee.com/hackersslackers",
"link": "https://buymeacoffee.com/hackersslackers/c/405127",
"coffee_id": 405127,
}


class AllCoffeeDonations(BaseModel):
"""All `BuyMeACoffee` donations."""

# fmt: off
all_donations: List[CoffeeDonation]
# fmt: on

class Config:
json_schema_extra = [
{
"name": "Fake Todd",
"email": "fake@example.com",
"count": 1,
"message": "Great tutorials but this is a test message.",
"link": "https://buymeacoffee.com/hackersslackers/c/100000",
"coffee_id": 100000,
},
{
"name": "Firstname Lastname",
"email": "firstname.lastname@example.com",
"count": 2,
"message": "Thank you for the tutorials!",
"link": "https://buymeacoffee.com/hackersslackers/c/100001",
"coffee_id": 100001,
},
]


class NewComment(BaseModel):
"""User comment on a post."""

Expand Down
Loading

0 comments on commit 06c4186

Please sign in to comment.