-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.py
More file actions
54 lines (40 loc) · 1.93 KB
/
transaction.py
File metadata and controls
54 lines (40 loc) · 1.93 KB
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
from fastapi import APIRouter, HTTPException, Path, Depends
from pydantic import BaseModel, Field, EmailStr
from sqlalchemy.orm import Session
from database.data import get_db
from database.models.table import Account,Transaction
transactions_router = APIRouter(tags=["Transactions"])
#pydantic model for transactions
class transaction_actions(BaseModel):
email: EmailStr
name:str
surname: str
amount: float= Field(gt=0,description="Amount must be positive")
#endpoint for adding funds to account
@transactions_router.post('/account/{account_id}/deposit/')
async def deposit(account_id:int = Path(description= "Account ID to deposit funds"), account_data : transaction_actions = Depends(),db: Session = Depends(get_db)):
account = db.query(Account).filter(Account.id == account_id).first()
if not account:
raise HTTPException(status_code=404, detail="Account not found")
account.balance += account_data.amount
db.commit()
db.refresh(account)
return {
"message": f"Successfully deposited {account_data.amount} to account {account_id}.",
"new_balance": account.balance
}
#endpoint for withdrawing fund from account
@transactions_router.post('/account/{account_id}/withdraw')
async def withdraw_funds(account_id: int = Path(description="Account ID to withdraw funs"), account_data: transaction_actions = Depends(),db: Session = Depends(get_db)):
account = db.query(Account).filter(Account.id == account_id).first()
if not account:
raise HTTPException(status_code=404, detail="Account not found")
if account.balance < account_data.amount:
raise HTTPException(status_code=404, detail="Insufficient funds")
account.balance -= account_data.amount
db.commit()
db.refresh(account)
return {
"message": f"Successfully withdraw {account_data.amount} from account {account_id}.",
"new_balance": account.balance
}