Skip to content

Commit 06c4186

Browse files
committed
Add AllCoffeeDonations schema.
1 parent c16b30d commit 06c4186

6 files changed

Lines changed: 788 additions & 1332 deletions

File tree

app/donations/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from database import get_db
88
from database.crud import create_donation, get_donation
99
from database.models import Donation
10-
from database.schemas import NewDonation
10+
from database.schemas import CoffeeDonation, AllCoffeeDonations
1111

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

@@ -16,9 +16,9 @@
1616
"/",
1717
summary="New BuyMeACoffee donation",
1818
description="Save record of new donation to persistent ledger.",
19-
response_model=NewDonation,
19+
response_model=CoffeeDonation,
2020
)
21-
async def accept_donation(donation: NewDonation, db: Session = Depends(get_db)) -> NewDonation:
21+
async def accept_donation(donation: CoffeeDonation, db: Session = Depends(get_db)) -> CoffeeDonation:
2222
"""
2323
Save BuyMeACoffee donation to database.
2424
@@ -40,9 +40,9 @@ async def accept_donation(donation: NewDonation, db: Session = Depends(get_db))
4040
"/",
4141
summary="Delete BuyMeACoffee donation record",
4242
description="Delete BuyMeACoffee donation transaction by ID.",
43-
response_model=NewDonation,
43+
response_model=CoffeeDonation,
4444
)
45-
async def delete_donation(donation: NewDonation, db: Session = Depends(get_db)) -> NewDonation:
45+
async def delete_donation(donation: CoffeeDonation, db: Session = Depends(get_db)) -> CoffeeDonation:
4646
"""
4747
Delete BuyMeACoffee donation from database.
4848
@@ -60,10 +60,7 @@ async def delete_donation(donation: NewDonation, db: Session = Depends(get_db))
6060
return create_donation(db, donation)
6161

6262

63-
@router.get(
64-
"/",
65-
summary="Get all existing donations.",
66-
)
63+
@router.get("/", summary="Get all existing donations.", response_model=AllCoffeeDonations)
6764
async def get_donations(db: Session = Depends(get_db)):
6865
"""
6966
Test endpoint for fetching comments joined with user info.

database/crud.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from sqlalchemy.orm import Session
77

88
from database.models import Account, Donation
9-
from database.schemas import NewDonation
9+
from database.schemas import CoffeeDonation
1010
from log import LOGGER
1111

1212

13-
def get_donation(db: Session, donation: NewDonation) -> Optional[NewDonation]:
13+
def get_donation(db: Session, donation: CoffeeDonation) -> Optional[CoffeeDonation]:
1414
"""
1515
Fetch BuyMeACoffee donation by ID.
1616
@@ -26,7 +26,7 @@ def get_donation(db: Session, donation: NewDonation) -> Optional[NewDonation]:
2626
return None
2727

2828

29-
def create_donation(db: Session, donation: NewDonation) -> Donation:
29+
def create_donation(db: Session, donation: CoffeeDonation) -> Donation:
3030
"""
3131
Create new BuyMeACoffee donation record.
3232

database/schemas.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pydantic import BaseModel, EmailStr, Field
77

88

9-
class NewDonation(BaseModel):
9+
class CoffeeDonation(BaseModel):
1010
"""`BuyMeACoffee` donation."""
1111

1212
# fmt: off
@@ -24,11 +24,39 @@ class Config:
2424
"email": "fake@example.com",
2525
"count": 1,
2626
"message": "Great tutorials but this is a test message.",
27-
"link": "https://buymeacoffee.com/hackersslackers",
27+
"link": "https://buymeacoffee.com/hackersslackers/c/405127",
2828
"coffee_id": 405127,
2929
}
3030

3131

32+
class AllCoffeeDonations(BaseModel):
33+
"""All `BuyMeACoffee` donations."""
34+
35+
# fmt: off
36+
all_donations: List[CoffeeDonation]
37+
# fmt: on
38+
39+
class Config:
40+
json_schema_extra = [
41+
{
42+
"name": "Fake Todd",
43+
"email": "fake@example.com",
44+
"count": 1,
45+
"message": "Great tutorials but this is a test message.",
46+
"link": "https://buymeacoffee.com/hackersslackers/c/100000",
47+
"coffee_id": 100000,
48+
},
49+
{
50+
"name": "Firstname Lastname",
51+
"email": "firstname.lastname@example.com",
52+
"count": 2,
53+
"message": "Thank you for the tutorials!",
54+
"link": "https://buymeacoffee.com/hackersslackers/c/100001",
55+
"coffee_id": 100001,
56+
},
57+
]
58+
59+
3260
class NewComment(BaseModel):
3361
"""User comment on a post."""
3462

0 commit comments

Comments
 (0)