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:
2026-01-30 20:47:33 +01:00
parent 1ad30bd77e
commit cad862f469
60 changed files with 755 additions and 589 deletions

View File

@@ -18,7 +18,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.feature_service import feature_service
from models.database.user import User
from models.schema.auth import UserContext
router = APIRouter(prefix="/features")
logger = logging.getLogger(__name__)
@@ -145,7 +145,7 @@ def _feature_to_response(feature) -> FeatureResponse:
def list_features(
category: str | None = Query(None, description="Filter by category"),
active_only: bool = Query(False, description="Only active features"),
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""List all features with their tier assignments."""
@@ -161,7 +161,7 @@ def list_features(
@router.get("/categories", response_model=CategoryListResponse)
def list_categories(
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""List all feature categories."""
@@ -171,7 +171,7 @@ def list_categories(
@router.get("/tiers", response_model=TierListWithFeaturesResponse)
def list_tiers_with_features(
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""List all tiers with their feature assignments."""
@@ -195,7 +195,7 @@ def list_tiers_with_features(
@router.get("/{feature_code}", response_model=FeatureResponse)
def get_feature(
feature_code: str,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -217,7 +217,7 @@ def get_feature(
def update_feature(
feature_code: str,
request: UpdateFeatureRequest,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -253,7 +253,7 @@ def update_feature(
def update_tier_features(
tier_code: str,
request: UpdateTierFeaturesRequest,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""
@@ -282,7 +282,7 @@ def update_tier_features(
@router.get("/tiers/{tier_code}/features", response_model=TierFeatureDetailResponse)
def get_tier_features(
tier_code: str,
current_user: User = Depends(get_current_admin_api),
current_user: UserContext = Depends(get_current_admin_api),
db: Session = Depends(get_db),
):
"""