add create get exchange endpoint
All checks were successful
Backend CI / unit-test (push) Successful in 34s

This commit is contained in:
2025-09-22 23:07:28 +02:00
parent 1750401278
commit b68249f9f1
3 changed files with 76 additions and 10 deletions

View File

@@ -12,7 +12,7 @@ from fastapi.responses import JSONResponse
import settings
from trading_journal import db, service
from trading_journal.db import Database
from trading_journal.dto import SessionsBase, SessionsCreate, UserCreate, UserLogin, UserRead
from trading_journal.dto import ExchangesBase, SessionsBase, SessionsCreate, UserCreate, UserLogin, UserRead
_db = db.create_database(settings.settings.database_url)
@@ -96,12 +96,37 @@ async def login(request: Request, user_in: UserLogin) -> SessionsBase:
# Exchange
# @app.post(f"{settings.settings.api_base}/exchanges")
# async def create_exchange(request: Request, name: str, notes: str | None) -> dict:
@app.get(f"{settings.settings.api_base}/trades")
async def get_trades(request: Request) -> list:
@app.post(f"{settings.settings.api_base}/exchanges")
async def create_exchange(request: Request, exchange_data: ExchangesBase) -> dict:
db_factory: Database = request.app.state.db_factory
with db_factory.get_session_ctx_manager() as db:
return service.get_trades_service(db, request.state.user_id)
def sync_work() -> ExchangesBase:
with db_factory.get_session_ctx_manager() as db:
return service.create_exchange_service(db, request.state.user_id, exchange_data.name, exchange_data.notes)
try:
exchange = await asyncio.to_thread(sync_work)
return JSONResponse(status_code=status.HTTP_201_CREATED, content=exchange.model_dump())
except service.ExchangeAlreadyExistsError as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) from e
except Exception as e:
logger.exception("Failed to create exchange: \n")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal server error") from e
@app.get(f"{settings.settings.api_base}/exchanges")
async def get_exchanges(request: Request) -> list[ExchangesBase]:
db_factory: Database = request.app.state.db_factory
def sync_work() -> list[ExchangesBase]:
with db_factory.get_session_ctx_manager() as db:
return service.get_exchanges_by_user_service(db, request.state.user_id)
try:
return await asyncio.to_thread(sync_work)
except Exception as e:
logger.exception("Failed to get exchanges: \n")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal server error") from e
# Trade