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:
@@ -11,7 +11,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.admin_customer_service import admin_customer_service
|
||||
from models.database.user import User
|
||||
from models.schema.auth import UserContext
|
||||
from app.modules.customers.schemas import (
|
||||
CustomerDetailResponse,
|
||||
CustomerListResponse,
|
||||
@@ -35,7 +35,7 @@ def list_customers(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(20, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerListResponse:
|
||||
"""
|
||||
Get paginated list of customers across all vendors.
|
||||
@@ -68,7 +68,7 @@ def list_customers(
|
||||
def get_customer_stats(
|
||||
vendor_id: int | None = Query(None, description="Filter by vendor ID"),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerStatisticsResponse:
|
||||
"""Get customer statistics."""
|
||||
stats = admin_customer_service.get_customer_stats(db=db, vendor_id=vendor_id)
|
||||
@@ -84,7 +84,7 @@ def get_customer_stats(
|
||||
def get_customer(
|
||||
customer_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerDetailResponse:
|
||||
"""Get customer details by ID."""
|
||||
customer = admin_customer_service.get_customer(db=db, customer_id=customer_id)
|
||||
@@ -100,7 +100,7 @@ def get_customer(
|
||||
def toggle_customer_status(
|
||||
customer_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
) -> CustomerMessageResponse:
|
||||
"""Toggle customer active status."""
|
||||
result = admin_customer_service.toggle_customer_status(
|
||||
|
||||
Reference in New Issue
Block a user