1
- from uuid import UUID
2
1
from decimal import Decimal
2
+ from uuid import UUID
3
+
3
4
from fastapi import FastAPI
5
+ from pydantic import BaseModel
4
6
5
7
from bankaccounts .application import BankAccounts
6
8
7
9
app = FastAPI ()
8
10
accounts = BankAccounts ()
9
11
10
12
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 )
12
22
async def get_account (account_id : UUID ):
13
23
try :
14
24
account = accounts .get_account (account_id )
15
25
except Exception as e :
16
26
return {"error" : e .__class__ .__name__ }
17
- return account
27
+ return account . __dict__
18
28
19
29
20
30
@app .post ("/accounts" )
@@ -26,7 +36,7 @@ async def open_account(full_name: str, email_address: str):
26
36
return account_id
27
37
28
38
29
- @app .post ("/account/{account_id}/deposit" )
39
+ @app .post ("/account/{account_id}/deposit" , response_model = AccountDetails )
30
40
async def deposit_funds (account_id : UUID , amount : Decimal ):
31
41
try :
32
42
accounts .deposit_funds (
@@ -36,10 +46,10 @@ async def deposit_funds(account_id: UUID, amount: Decimal):
36
46
account = accounts .get_account (account_id )
37
47
except Exception as e :
38
48
return {"error" : e .__class__ .__name__ }
39
- return account
49
+ return account . __dict__
40
50
41
51
42
- @app .post ("/account/{account_id}/withdraw" )
52
+ @app .post ("/account/{account_id}/withdraw" , response_model = AccountDetails )
43
53
async def withdraw_funds (account_id : UUID , amount : Decimal ):
44
54
try :
45
55
accounts .withdraw_funds (
@@ -49,10 +59,10 @@ async def withdraw_funds(account_id: UUID, amount: Decimal):
49
59
account = accounts .get_account (account_id )
50
60
except Exception as e :
51
61
return {"error" : e .__class__ .__name__ }
52
- return account
62
+ return account . __dict__
53
63
54
64
55
- @app .post ("/account/{account_id}/transfer" )
65
+ @app .post ("/account/{account_id}/transfer" , response_model = AccountDetails )
56
66
async def transfer_funds (account_id : UUID , to_account_id : UUID , amount : Decimal ):
57
67
try :
58
68
accounts .transfer_funds (
@@ -63,10 +73,10 @@ async def transfer_funds(account_id: UUID, to_account_id: UUID, amount: Decimal)
63
73
account = accounts .get_account (account_id )
64
74
except Exception as e :
65
75
return {"error" : e .__class__ .__name__ }
66
- return account
76
+ return account . __dict__
67
77
68
78
69
- @app .post ("/account/{account_id}/overdraft" )
79
+ @app .post ("/account/{account_id}/overdraft" , response_model = AccountDetails )
70
80
async def set_overdraft_limit (account_id : UUID , limit : Decimal ):
71
81
try :
72
82
accounts .set_overdraft_limit (
@@ -76,17 +86,17 @@ async def set_overdraft_limit(account_id: UUID, limit: Decimal):
76
86
account = accounts .get_account (account_id )
77
87
except Exception as e :
78
88
return {"error" : e .__class__ .__name__ }
79
- return account
89
+ return account . __dict__
80
90
81
91
82
- @app .post ("/account/{account_id}/close" )
92
+ @app .post ("/account/{account_id}/close" , response_model = AccountDetails )
83
93
async def close_account (account_id : UUID ):
84
94
try :
85
95
accounts .close_account (account_id )
86
96
account = accounts .get_account (account_id )
87
97
except Exception as e :
88
98
return {"error" : e .__class__ .__name__ }
89
- return account
99
+ return account . __dict__
90
100
91
101
92
102
if __name__ == "__main__" :
0 commit comments