-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.py
95 lines (76 loc) · 2.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from uuid import UUID
from decimal import Decimal
from fastapi import FastAPI
from bankaccounts.application import BankAccounts
app = FastAPI()
accounts = BankAccounts()
@app.get("/accounts/{account_id}")
def get_account(account_id: UUID):
try:
account = accounts.get_account(account_id)
except Exception as e:
return {"error": e.__class__.__name__}
return account
@app.post("/accounts")
def open_account(full_name: str, email_address: str):
try:
account_id = accounts.open_account(full_name, email_address)
except Exception as e:
return {"error": e.__class__.__name__}
return account_id
@app.post("/account/{account_id}/deposit")
def deposit_funds(account_id: UUID, amount: Decimal):
try:
accounts.deposit_funds(
credit_account_id=account_id,
amount=amount,
)
account = accounts.get_account(account_id)
except Exception as e:
return {"error": e.__class__.__name__}
return account
@app.post("/account/{account_id}/withdraw")
def withdraw_funds(account_id: UUID, amount: Decimal):
try:
accounts.withdraw_funds(
debit_account_id=account_id,
amount=amount,
)
account = accounts.get_account(account_id)
except Exception as e:
return {"error": e.__class__.__name__}
return account
@app.post("/account/{account_id}/transfer")
def transfer_funds(account_id: UUID, to_account_id: UUID, amount: Decimal):
try:
accounts.transfer_funds(
debit_account_id=account_id,
credit_account_id=to_account_id,
amount=amount,
)
account = accounts.get_account(account_id)
except Exception as e:
return {"error": e.__class__.__name__}
return account
@app.post("/account/{account_id}/overdraft")
def set_overdraft_limit(account_id: UUID, limit: Decimal):
try:
accounts.set_overdraft_limit(
account_id=account_id,
overdraft_limit=limit,
)
account = accounts.get_account(account_id)
except Exception as e:
return {"error": e.__class__.__name__}
return account
@app.post("/account/{account_id}/close")
def close_account(account_id: UUID):
try:
accounts.close_account(account_id)
account = accounts.get_account(account_id)
except Exception as e:
return {"error": e.__class__.__name__}
return account
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info", reload=True)