From ad9924394423728b514507e6aeb7ca2a58f34f29 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 16 Jul 2025 12:41:26 +0800 Subject: [PATCH] Completed task --- python-fastapi/main.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/python-fastapi/main.py b/python-fastapi/main.py index af6d4c4..9293288 100644 --- a/python-fastapi/main.py +++ b/python-fastapi/main.py @@ -11,8 +11,8 @@ class Transfer(BaseModel): app = FastAPI() accounts = { - '1': {'amount': 1000}, - '2': {'amount': 1000}, + '1': 1000, + '2': 1000, } transactions: list[Transfer] = [] @@ -21,11 +21,32 @@ transactions: list[Transfer] = [] def transfer(transfer: Transfer): amount = transfer.amount sender = transfer.sender - receiver = transfer.receive + receiver = transfer.receiver if amount <= 0: return {"error": "Amount must be greater than 0"} + if accounts.get(sender) <= amount: + return {"error": "Sender does not have sufficient funds. "} + else: + # Minus sender's amount + accounts[sender] = accounts.get(sender) - amount + + # Add receiver amount + accounts[receiver] = accounts.get(receiver) + amount + + # Include the transaction + transactions.append(transfer) + print(f"transfer {amount} from {sender} to {receiver}") + print(transactions) + print(accounts.get("1")) + print(accounts.get("2")) + return {"amount": amount, "sender": sender, "receiver": receiver} + + +@app.get("/transactions") +def getTransactions(): + return transactions