
- Set up project structure with FastAPI - Implement user and account management - Add send and receive money functionality - Set up transaction processing system - Add JWT authentication - Configure SQLAlchemy with SQLite - Set up Alembic for database migrations - Create comprehensive API documentation
92 lines
2.9 KiB
Python
92 lines
2.9 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models, schemas
|
|
from app.api import deps
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.Account])
|
|
def read_accounts(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Retrieve accounts for the current user.
|
|
"""
|
|
accounts = crud.get_user_accounts(db, owner_id=current_user.id, skip=skip, limit=limit)
|
|
return accounts
|
|
|
|
|
|
@router.post("/", response_model=schemas.Account)
|
|
def create_account(
|
|
account_in: schemas.AccountCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Create new account for the current user.
|
|
"""
|
|
account = crud.create_account(db, account_in=account_in, owner_id=current_user.id)
|
|
return account
|
|
|
|
|
|
@router.get("/{account_id}", response_model=schemas.Account)
|
|
def read_account(
|
|
account_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Get account by ID.
|
|
"""
|
|
account = crud.get_account_by_id(db, id=account_id)
|
|
if not account:
|
|
raise HTTPException(status_code=404, detail="Account not found")
|
|
if account.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
return account
|
|
|
|
|
|
@router.put("/{account_id}", response_model=schemas.Account)
|
|
def update_account(
|
|
account_id: int,
|
|
account_in: schemas.AccountUpdate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Update an account.
|
|
"""
|
|
account = crud.get_account_by_id(db, id=account_id)
|
|
if not account:
|
|
raise HTTPException(status_code=404, detail="Account not found")
|
|
if account.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
account = crud.update_account(db, account=account, account_in=account_in)
|
|
return account
|
|
|
|
|
|
@router.delete("/{account_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_account(
|
|
account_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
db: Session = Depends(deps.get_db),
|
|
) -> None:
|
|
"""
|
|
Delete an account.
|
|
"""
|
|
account = crud.get_account_by_id(db, id=account_id)
|
|
if not account:
|
|
raise HTTPException(status_code=404, detail="Account not found")
|
|
if account.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
if account.balance > 0:
|
|
raise HTTPException(status_code=400, detail="Cannot delete account with positive balance")
|
|
crud.delete_account(db, account=account)
|
|
return None |