Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions backend/app/trade/controller/trade_controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Optional

from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from app.common.model.base import get_session
from app.trade.dto.transaction_response import TransactionsResponse
from app.trade.service.trade_service import TradeService
from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

trade_router = APIRouter(prefix="/trade", tags=["Trade"])

Expand All @@ -32,6 +31,10 @@ async def get_transactions(
None,
description="이전 페이지의 마지막 거래 ID (첫 페이지 조회 시 생략)",
),
trade_type: Optional[str] = Query(
None,
description="거래 유형 필터 (예: 'buy', 'sell', 'hold)",
),
limit: int = Query(
20,
ge=1,
Expand Down Expand Up @@ -60,4 +63,6 @@ async def get_transactions(
- 거래 실행 사유 (execution_reason): 잔고, 현재 가격, 수수료, 실패 원인 등 상세 정보
"""
trade_service = TradeService(session)
return await trade_service.get_transactions(cursor=cursor, limit=limit)
return await trade_service.get_transactions(
cursor=cursor, limit=limit, trade_type=trade_type
)
10 changes: 6 additions & 4 deletions backend/app/trade/repository/trade_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

from typing import List, Optional

from app.common.repository.base_repository import BaseRepository
from app.trade.model.trade import Trade
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from app.common.repository.base_repository import BaseRepository
from app.trade.model.trade import Trade


class TradeRepository(BaseRepository[Trade]):
"""Trade CRUD 연산"""
Expand Down Expand Up @@ -41,7 +40,7 @@ async def get_all_with_coin(self) -> List[Trade]:
return list(result.scalars().all())

async def get_all_with_coin_paginated(
self, cursor: Optional[int], limit: int
self, cursor: Optional[int], limit: int, trade_type: Optional[str]
) -> List[Trade]:
"""
거래 내역을 커서 기반 페이지네이션으로 조회
Expand All @@ -56,6 +55,9 @@ async def get_all_with_coin_paginated(
if cursor is not None:
query = query.where(Trade.id < cursor)

if trade_type is not None:
query = query.where(Trade.trade_type == trade_type)

# created_at 기준 내림차순 정렬
query = query.order_by(Trade.created_at.desc()).limit(limit)

Expand Down
12 changes: 7 additions & 5 deletions backend/app/trade/service/trade_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from logging import Logger
from typing import List, Optional

from sqlalchemy.ext.asyncio import AsyncSession

from app.ai.client.open_ai_client import OpenAIClient
from app.ai.dto.ai_analysis_response import AiAnalysisResponse, Decision, RiskLevel
from app.ballance.model.balance import Balance
Expand All @@ -19,6 +17,7 @@
from app.trade.model.trade import Trade
from app.trade.repository.trade_repository import TradeRepository
from app.upbit.client.upbit_client import UpbitClient
from sqlalchemy.ext.asyncio import AsyncSession

logger = Logger(__name__)

Expand Down Expand Up @@ -48,7 +47,7 @@ async def execute(

# 1. 활성화된 모든 코인 조회
active_coins = await self.coin_service.get_all_active()

# 2. 거래 전 잔고 기록
await self._record_balance()

Expand Down Expand Up @@ -379,7 +378,10 @@ async def _record_balance(self) -> None:
await self.balance_repository.create(balance)

async def get_transactions(
self, cursor: Optional[int] = None, limit: int = 20
self,
cursor: Optional[int] = None,
limit: int = 20,
trade_type: Optional[str] = None,
) -> TransactionsResponse:
"""
거래 내역을 커서 기반 페이지네이션으로 조회
Expand All @@ -390,7 +392,7 @@ async def get_transactions(
"""
# limit + 1개를 조회하여 다음 페이지 존재 여부 확인
trades = await self.trade_repository.get_all_with_coin_paginated(
cursor=cursor, limit=limit + 1
cursor=cursor, limit=limit + 1, trade_type=trade_type
)

# 다음 페이지 존재 여부 판단
Expand Down