Skip to content

Commit a0cb516

Browse files
committed
Added AccountDetails response model.
1 parent 12edfc8 commit a0cb516

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

main.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
from uuid import UUID
21
from decimal import Decimal
2+
from uuid import UUID
3+
34
from fastapi import FastAPI
5+
from pydantic import BaseModel
46

57
from bankaccounts.application import BankAccounts
68

79
app = FastAPI()
810
accounts = BankAccounts()
911

1012

11-
@app.get("/accounts/{account_id}")
13+
class AccountDetails(BaseModel):
14+
full_name: str
15+
email_address: str
16+
balance: Decimal
17+
overdraft_limit: Decimal
18+
is_closed: bool
19+
20+
21+
@app.get("/accounts/{account_id}", response_model=AccountDetails)
1222
async def get_account(account_id: UUID):
1323
try:
1424
account = accounts.get_account(account_id)
1525
except Exception as e:
1626
return {"error": e.__class__.__name__}
17-
return account
27+
return account.__dict__
1828

1929

2030
@app.post("/accounts")
@@ -26,7 +36,7 @@ async def open_account(full_name: str, email_address: str):
2636
return account_id
2737

2838

29-
@app.post("/account/{account_id}/deposit")
39+
@app.post("/account/{account_id}/deposit", response_model=AccountDetails)
3040
async def deposit_funds(account_id: UUID, amount: Decimal):
3141
try:
3242
accounts.deposit_funds(
@@ -36,10 +46,10 @@ async def deposit_funds(account_id: UUID, amount: Decimal):
3646
account = accounts.get_account(account_id)
3747
except Exception as e:
3848
return {"error": e.__class__.__name__}
39-
return account
49+
return account.__dict__
4050

4151

42-
@app.post("/account/{account_id}/withdraw")
52+
@app.post("/account/{account_id}/withdraw", response_model=AccountDetails)
4353
async def withdraw_funds(account_id: UUID, amount: Decimal):
4454
try:
4555
accounts.withdraw_funds(
@@ -49,10 +59,10 @@ async def withdraw_funds(account_id: UUID, amount: Decimal):
4959
account = accounts.get_account(account_id)
5060
except Exception as e:
5161
return {"error": e.__class__.__name__}
52-
return account
62+
return account.__dict__
5363

5464

55-
@app.post("/account/{account_id}/transfer")
65+
@app.post("/account/{account_id}/transfer", response_model=AccountDetails)
5666
async def transfer_funds(account_id: UUID, to_account_id: UUID, amount: Decimal):
5767
try:
5868
accounts.transfer_funds(
@@ -63,10 +73,10 @@ async def transfer_funds(account_id: UUID, to_account_id: UUID, amount: Decimal)
6373
account = accounts.get_account(account_id)
6474
except Exception as e:
6575
return {"error": e.__class__.__name__}
66-
return account
76+
return account.__dict__
6777

6878

69-
@app.post("/account/{account_id}/overdraft")
79+
@app.post("/account/{account_id}/overdraft", response_model=AccountDetails)
7080
async def set_overdraft_limit(account_id: UUID, limit: Decimal):
7181
try:
7282
accounts.set_overdraft_limit(
@@ -76,17 +86,17 @@ async def set_overdraft_limit(account_id: UUID, limit: Decimal):
7686
account = accounts.get_account(account_id)
7787
except Exception as e:
7888
return {"error": e.__class__.__name__}
79-
return account
89+
return account.__dict__
8090

8191

82-
@app.post("/account/{account_id}/close")
92+
@app.post("/account/{account_id}/close", response_model=AccountDetails)
8393
async def close_account(account_id: UUID):
8494
try:
8595
accounts.close_account(account_id)
8696
account = accounts.get_account(account_id)
8797
except Exception as e:
8898
return {"error": e.__class__.__name__}
89-
return account
99+
return account.__dict__
90100

91101

92102
if __name__ == "__main__":

0 commit comments

Comments
 (0)