wip user reg
All checks were successful
Backend CI / unit-test (push) Successful in 34s

This commit is contained in:
2025-09-22 17:35:10 +02:00
parent e70a63e4f9
commit 466e6ce653
7 changed files with 163 additions and 8 deletions

View File

@@ -2,13 +2,12 @@ import asyncio
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from fastapi import FastAPI, status
from fastapi import FastAPI, HTTPException, Request
import settings
from trading_journal import db
from trading_journal.dto import TradeCreate, TradeRead
API_BASE = "/api/v1"
from trading_journal import db, service
from trading_journal.db import Database
from trading_journal.dto import UserCreate, UserRead
_db = db.create_database(settings.settings.database_url)
@@ -23,8 +22,31 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: # noqa: ARG001
app = FastAPI(lifespan=lifespan)
app.add_middleware(service.AuthMiddleWare)
app.state.db_factory = _db
@app.get(f"{API_BASE}/status")
@app.get(f"{settings.settings.api_base}/status")
async def get_status() -> dict[str, str]:
return {"status": "ok"}
@app.post(f"{settings.settings.api_base}/register")
async def register_user(request: Request, user_in: UserCreate) -> UserRead:
db_factory: Database = request.app.state.db_factory
def sync_work() -> UserRead:
with db_factory.get_session_ctx_manager() as db:
return service.register_user_service(db, user_in)
try:
return await asyncio.to_thread(sync_work)
except service.UserAlreadyExistsError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
except Exception as e:
raise HTTPException(status_code=500, detail="Internal server error" + str(e)) from e
@app.get(f"{settings.settings.api_base}/trades")
async def get_trades() -> dict[str, str]:
return {"trades": []}