refactor(api): introduce UserContext schema for API dependency injection
Replace direct User database model imports in API endpoints with UserContext schema, following the architecture principle that API routes should not import database models directly. Changes: - Create UserContext schema in models/schema/auth.py with from_user() factory - Update app/api/deps.py to return UserContext from all auth dependencies - Add _get_user_model() helper for functions needing User model access - Update 58 API endpoint files to use UserContext instead of User - Add noqa comments for 4 legitimate edge cases (enums, internal helpers) Architecture validation: 0 errors (down from 61), 11 warnings remain Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,7 +17,7 @@ from sqlalchemy.orm import Session
|
||||
from app.api.deps import get_current_admin_api
|
||||
from app.core.database import get_db
|
||||
from app.services.platform_health_service import platform_health_service
|
||||
from models.database.user import User
|
||||
from models.schema.auth import UserContext
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -115,7 +115,7 @@ class CapacityMetricsResponse(BaseModel):
|
||||
@router.get("/health", response_model=PlatformHealthResponse)
|
||||
async def get_platform_health(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get comprehensive platform health status.
|
||||
|
||||
@@ -139,7 +139,7 @@ async def get_platform_health(
|
||||
@router.get("/capacity", response_model=CapacityMetricsResponse)
|
||||
async def get_capacity_metrics(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get capacity-focused metrics for planning."""
|
||||
metrics = platform_health_service.get_capacity_metrics(db)
|
||||
@@ -149,7 +149,7 @@ async def get_capacity_metrics(
|
||||
@router.get("/subscription-capacity")
|
||||
async def get_subscription_capacity(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Get subscription-based capacity metrics.
|
||||
@@ -163,7 +163,7 @@ async def get_subscription_capacity(
|
||||
async def get_growth_trends(
|
||||
days: int = 30,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Get growth trends over the specified period.
|
||||
@@ -178,7 +178,7 @@ async def get_growth_trends(
|
||||
@router.get("/recommendations")
|
||||
async def get_scaling_recommendations(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Get scaling recommendations based on current capacity and growth.
|
||||
@@ -193,7 +193,7 @@ async def get_scaling_recommendations(
|
||||
@router.post("/snapshot")
|
||||
async def capture_snapshot(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Manually capture a capacity snapshot.
|
||||
|
||||
Reference in New Issue
Block a user