79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""
|
|
매매일지 API 엔드포인트
|
|
|
|
- GET /: 최근 AI 생성 매매일지 목록
|
|
- GET /{journal_id}: 특정 일지 상세
|
|
"""
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, desc
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
import logging
|
|
|
|
from app.core.database import get_db
|
|
from app.models.models import TradeJournal
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("")
|
|
async def list_journals(
|
|
limit: int = Query(default=20, ge=1, le=100),
|
|
offset: int = Query(default=0, ge=0),
|
|
stock_code: Optional[str] = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""최근 AI 생성 매매일지 목록 조회"""
|
|
try:
|
|
query = select(TradeJournal).order_by(desc(TradeJournal.journal_date))
|
|
if stock_code:
|
|
query = query.where(TradeJournal.stock_code == stock_code)
|
|
query = query.offset(offset).limit(limit)
|
|
|
|
result = await db.execute(query)
|
|
journals = result.scalars().all()
|
|
|
|
return [
|
|
{
|
|
"id": j.id,
|
|
"trade_id": j.trade_id,
|
|
"stock_code": j.stock_code,
|
|
"stock_name": j.stock_name,
|
|
"journal_date": j.journal_date.isoformat() if j.journal_date else None,
|
|
"content": j.content,
|
|
"strategy_name": j.strategy_name,
|
|
"mood": j.mood,
|
|
"tags": j.tags,
|
|
"created_at": j.created_at.isoformat() if j.created_at else None,
|
|
}
|
|
for j in journals
|
|
]
|
|
except Exception as e:
|
|
logger.error(f"journals list error: {e}", exc_info=True)
|
|
return []
|
|
|
|
|
|
@router.get("/{journal_id}")
|
|
async def get_journal(journal_id: int, db: AsyncSession = Depends(get_db)):
|
|
"""특정 매매일지 상세 조회"""
|
|
result = await db.execute(select(TradeJournal).where(TradeJournal.id == journal_id))
|
|
journal = result.scalar_one_or_none()
|
|
if not journal:
|
|
from fastapi import HTTPException
|
|
raise HTTPException(status_code=404, detail="Journal not found")
|
|
return {
|
|
"id": journal.id,
|
|
"trade_id": journal.trade_id,
|
|
"stock_code": journal.stock_code,
|
|
"stock_name": journal.stock_name,
|
|
"journal_date": journal.journal_date.isoformat() if journal.journal_date else None,
|
|
"content": journal.content,
|
|
"strategy_name": journal.strategy_name,
|
|
"mood": journal.mood,
|
|
"tags": journal.tags,
|
|
"created_at": journal.created_at.isoformat() if journal.created_at else None,
|
|
}
|