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

@@ -10,6 +10,15 @@ if TYPE_CHECKING:
from trading_journal.models import TradeStrategy, TradeType, UnderlyingCurrency
class ExchangesBase(SQLModel):
name: str
notes: str | None = None
class ExchangesCreate(ExchangesBase):
user_id: int
class TradeBase(SQLModel):
user_id: int
friendly_name: str | None

View File

@@ -12,7 +12,7 @@ from starlette.middleware.base import BaseHTTPMiddleware
import settings
from trading_journal import crud, security
from trading_journal.db import Database
from trading_journal.dto import SessionsCreate, SessionsUpdate, UserCreate, UserLogin, UserRead
from trading_journal.dto import ExchangesBase, ExchangesCreate, SessionsCreate, SessionsUpdate, UserCreate, UserLogin, UserRead
from trading_journal.models import Sessions
SessionsCreate.model_rebuild()
@@ -88,6 +88,10 @@ class UserAlreadyExistsError(ServiceError):
pass
class ExchangeAlreadyExistsError(ServiceError):
pass
def register_user_service(db_session: Session, user_in: UserCreate) -> UserRead:
if crud.get_user_by_username(db_session, user_in.username):
raise UserAlreadyExistsError("username already exists")
@@ -133,5 +137,33 @@ def authenticate_user_service(db_session: Session, user_in: UserLogin) -> tuple[
return SessionsCreate.model_validate(session), token
# Exchanges service
def create_exchange_service(db_session: Session, user_id: int, name: str, notes: str | None) -> ExchangesCreate:
existing_exchange = crud.get_exchange_by_name_and_user_id(db_session, name, user_id)
if existing_exchange:
raise ExchangeAlreadyExistsError("Exchange with the same name already exists for this user")
exchange_data = ExchangesCreate(
user_id=user_id,
name=name,
notes=notes,
)
try:
exchange = crud.create_exchange(db_session, exchange_data=exchange_data)
try:
exchange_dto = ExchangesCreate.model_validate(exchange)
except Exception as e:
logger.exception("Failed to convert exchange to ExchangesCreate: ")
raise ServiceError("Failed to convert exchange to ExchangesCreate") from e
except Exception as e:
logger.exception("Failed to create exchange:")
raise ServiceError("Failed to create exchange") from e
return exchange_dto
def get_exchanges_by_user_service(db_session: Session, user_id: int) -> list[ExchangesBase]:
exchanges = crud.get_all_exchanges_by_user_id(db_session, user_id)
return [ExchangesBase.model_validate(exchange) for exchange in exchanges]
def get_trades_service(db_session: Session, user_id: int) -> list:
return crud.get_trades_by_user_id(db_session, user_id)