31 lines
1004 B
Python
31 lines
1004 B
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from ..database import get_db
|
|
from ..models import Collection
|
|
from ..schemas import CollectionShareOut, CollectionOut, CollectionStockOut
|
|
from ..services.stock_data import get_kline, get_realtime_quote
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/s/{short_code}")
|
|
def get_shared_collection(short_code: str, db: Session = Depends(get_db)):
|
|
col = db.query(Collection).filter(Collection.short_code == short_code).first()
|
|
if not col:
|
|
raise HTTPException(404, "分享链接不存在或已失效")
|
|
|
|
stocks_data = {}
|
|
for stock in col.stocks:
|
|
kline = get_kline(stock.stock_code, months=12)
|
|
quote = get_realtime_quote(stock.stock_code)
|
|
stocks_data[stock.stock_code] = {
|
|
"kline": kline,
|
|
"realtime": quote,
|
|
"added_date": str(stock.added_date),
|
|
}
|
|
|
|
return CollectionShareOut(
|
|
collection=col,
|
|
stocks_data=stocks_data,
|
|
)
|