60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from fastapi import APIRouter, HTTPException, Query
|
|
|
|
from app.service.models import Transaction
|
|
from app.service.recon_service import (
|
|
FlagType,
|
|
StatusType,
|
|
add_transaction,
|
|
delete_transaction_by_id,
|
|
get_transaction_by_id,
|
|
list_transactions,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api", tags=["Transactions"])
|
|
|
|
|
|
@router.get(
|
|
"/transactions", response_model=list[Transaction], summary="List transactions"
|
|
)
|
|
async def list_transactions_endpoint(
|
|
status: StatusType | None = Query(None, description="Filter by status"),
|
|
flag: FlagType | None = Query(None, description="Filter by flag"),
|
|
) -> list[Transaction]:
|
|
return list_transactions(status=status, flag=flag)
|
|
|
|
|
|
@router.get(
|
|
"/transactions/{transaction_id}",
|
|
response_model=Transaction,
|
|
summary="Get a transaction",
|
|
)
|
|
async def get_transaction_endpoint(transaction_id: str) -> Transaction:
|
|
transaction = get_transaction_by_id(transaction_id)
|
|
if transaction is None:
|
|
raise HTTPException(status_code=404, detail=f"{transaction_id} not found")
|
|
return transaction
|
|
|
|
|
|
@router.post(
|
|
"/transactions",
|
|
response_model=Transaction,
|
|
status_code=201,
|
|
summary="Submit a transaction",
|
|
)
|
|
async def create_transaction_endpoint(transaction: Transaction) -> Transaction:
|
|
created = add_transaction(transaction)
|
|
if not created:
|
|
raise HTTPException(
|
|
status_code=409, detail=f"{transaction.transaction_id} already exists"
|
|
)
|
|
return transaction
|
|
|
|
|
|
@router.delete(
|
|
"/transactions/{transaction_id}", status_code=204, summary="Delete a transaction"
|
|
)
|
|
async def delete_transaction_endpoint(transaction_id: str) -> None:
|
|
removed = delete_transaction_by_id(transaction_id)
|
|
if not removed:
|
|
raise HTTPException(status_code=404, detail=f"{transaction_id} not found")
|