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:
@@ -20,7 +20,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.order_service import order_service
|
||||
from models.database.user import User
|
||||
from models.schema.auth import UserContext
|
||||
from app.modules.orders.schemas import (
|
||||
AdminOrderItem,
|
||||
AdminOrderListResponse,
|
||||
@@ -50,7 +50,7 @@ def get_all_orders(
|
||||
channel: str | None = Query(None, description="Filter by channel"),
|
||||
search: str | None = Query(None, description="Search by order number or customer"),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Get orders across all vendors with filtering.
|
||||
@@ -78,7 +78,7 @@ def get_all_orders(
|
||||
@router.get("/stats", response_model=AdminOrderStats)
|
||||
def get_order_stats(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get platform-wide order statistics."""
|
||||
return order_service.get_order_stats_admin(db)
|
||||
@@ -87,7 +87,7 @@ def get_order_stats(
|
||||
@router.get("/vendors", response_model=AdminVendorsWithOrdersResponse)
|
||||
def get_vendors_with_orders(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get list of vendors that have orders."""
|
||||
vendors = order_service.get_vendors_with_orders_admin(db)
|
||||
@@ -103,7 +103,7 @@ def get_vendors_with_orders(
|
||||
def get_order_detail(
|
||||
order_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get order details including items and addresses."""
|
||||
order = order_service.get_order_by_id_admin(db, order_id)
|
||||
@@ -122,7 +122,7 @@ def update_order_status(
|
||||
order_id: int,
|
||||
status_update: AdminOrderStatusUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Update order status.
|
||||
@@ -152,7 +152,7 @@ def mark_order_as_shipped(
|
||||
order_id: int,
|
||||
ship_request: MarkAsShippedRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Mark an order as shipped with optional tracking information.
|
||||
@@ -182,7 +182,7 @@ def mark_order_as_shipped(
|
||||
def get_shipping_label_info(
|
||||
order_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Get shipping label information for an order.
|
||||
|
||||
Reference in New Issue
Block a user