Files
budget-forecast/api/app/account/account_routes.py

55 lines
2.1 KiB
Python

from uuid import UUID
from fastapi import APIRouter, HTTPException, Depends
from fastapi_pagination import Page
from fastapi_pagination.ext.sqlmodel import paginate
from account.models import Account, AccountCreate, AccountRead, AccountUpdate
from db import SessionDep
from user.manager import get_current_user
router = APIRouter()
@router.post("")
def create_account(account: AccountCreate, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
result = Account.create(account, session)
return result
@router.get("")
def read_accounts(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_accounts())
@router.get("")
def read_assets(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_assets())
@router.get("")
def read_liabilities(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_liabilities())
@router.get("/{account_id}")
def read_account(account_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
account = Account.get(session, account_id)
if not account:
raise HTTPException(status_code=404, detail="Account not found")
return account
@router.put("/{account_id}")
def update_account(account_id: UUID, account: AccountUpdate, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
db_account = Account.get(session, account_id)
if not db_account:
raise HTTPException(status_code=404, detail="Account not found")
account_data = account.model_dump(exclude_unset=True)
account = Account.update(session, db_account, account_data)
return account
@router.delete("/{account_id}")
def delete_account(account_id: UUID, session: SessionDep, current_user=Depends(get_current_user)):
account = Account.get(session, account_id)
if not account:
raise HTTPException(status_code=404, detail="Account not found")
Account.delete(session, account)
return {"ok": True}