55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from typing import List
|
|
|
|
from app.db.database import get_db
|
|
from app.db.models import AccountStatus, Holding
|
|
from pydantic import BaseModel
|
|
|
|
router = APIRouter()
|
|
|
|
class AccountStatusSchema(BaseModel):
|
|
totalAssets: float
|
|
buyingPower: float
|
|
dailyProfit: float
|
|
dailyProfitRate: float
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
class HoldingSchema(BaseModel):
|
|
stockCode: str
|
|
stockName: str
|
|
quantity: int
|
|
avgPrice: float
|
|
currentPrice: float
|
|
profit: float
|
|
profitRate: float
|
|
marketValue: float
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
@router.get("/summary", response_model=AccountStatusSchema)
|
|
async def get_account_summary(db: AsyncSession = Depends(get_db)):
|
|
stmt = select(AccountStatus).where(AccountStatus.id == 1)
|
|
result = await db.execute(stmt)
|
|
status = result.scalar_one_or_none()
|
|
|
|
if not status:
|
|
# Return default zeroed if not initialized
|
|
return AccountStatusSchema(totalAssets=0, buyingPower=0, dailyProfit=0, dailyProfitRate=0)
|
|
|
|
return status
|
|
|
|
@router.get("/holdings", response_model=List[HoldingSchema])
|
|
async def get_holdings(market: str = None, db: AsyncSession = Depends(get_db)):
|
|
# TODO: Filter by market if Holding table supports it.
|
|
# Current Holding model doesn't have 'market' column explicitly, but we can assume mixed or add it.
|
|
# For now, return all.
|
|
stmt = select(Holding)
|
|
result = await db.execute(stmt)
|
|
holdings = result.scalars().all()
|
|
return holdings
|