backend / app /api /routers /income_statement.py
praneethys's picture
get income_statement report by report_id (#11)
5edc2d7 verified
Raw
History Blame
2.4 kB
from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.model.transaction import Transaction as TransactionModel
from app.model.income_statement import IncomeStatement as IncomeStatementModel
from app.schema.index import IncomeStatementCreate, IncomeStatementResponse
from app.engine.postgresdb import get_db_session
from app.service.income_statement import call_llm_to_create_income_statement
income_statement_router = r = APIRouter(prefix="/api/v1/income_statement", tags=["income_statement"])
@r.post(
"/",
responses={
200: {"description": "New transaction created"},
400: {"description": "Bad request"},
500: {"description": "Internal server error"},
},
)
async def create_income_statement(payload: IncomeStatementCreate, db: AsyncSession = Depends(get_db_session)) -> None:
try:
await call_llm_to_create_income_statement(payload, db)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@r.get(
"/{user_id}",
response_model=List[IncomeStatementResponse],
responses={
200: {"description": "New user created"},
400: {"description": "Bad request"},
204: {"description": "No content"},
500: {"description": "Internal server error"},
},
)
async def get_income_statements(
user_id: int, db: AsyncSession = Depends(get_db_session)
) -> List[IncomeStatementResponse]:
"""
Retrieve all income statements.
"""
result = await IncomeStatementModel.get_by_user(db, user_id)
all_rows = result.all()
if len(all_rows) == 0:
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail="No income statements found for this user")
return all_rows
@r.get(
"/{report_id}",
response_model=IncomeStatementResponse,
responses={
200: {"description": "Income statement found"},
404: {"description": "Income statement not found"},
500: {"description": "Internal server error"},
},
)
async def get_income_statement(report_id: int, db: AsyncSession = Depends(get_db_session)) -> IncomeStatementResponse:
income_statement = await IncomeStatementModel.get(db, id=report_id)
if not income_statement:
raise HTTPException(status_code=404, detail="Income statement not found")
return income_statement