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:
@@ -14,7 +14,7 @@ from app.services.marketplace_import_job_service import marketplace_import_job_s
|
||||
from app.services.stats_service import stats_service
|
||||
from app.services.vendor_service import vendor_service
|
||||
from app.tasks.background_tasks import process_marketplace_import
|
||||
from models.database.user import User
|
||||
from models.schema.auth import UserContext
|
||||
from app.modules.marketplace.schemas import (
|
||||
AdminMarketplaceImportJobListResponse,
|
||||
AdminMarketplaceImportJobRequest,
|
||||
@@ -37,7 +37,7 @@ def get_all_marketplace_import_jobs(
|
||||
page: int = Query(1, ge=1),
|
||||
limit: int = Query(100, 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),
|
||||
):
|
||||
"""Get all marketplace import jobs with pagination (Admin only)."""
|
||||
jobs, total = marketplace_import_job_service.get_all_import_jobs_paginated(
|
||||
@@ -64,7 +64,7 @@ async def create_marketplace_import_job(
|
||||
request: AdminMarketplaceImportJobRequest,
|
||||
background_tasks: BackgroundTasks,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Create a new marketplace import job (Admin only).
|
||||
@@ -122,7 +122,7 @@ async def create_marketplace_import_job(
|
||||
@router.get("/stats", response_model=ImportStatsResponse)
|
||||
def get_import_statistics(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get marketplace import statistics (Admin only)."""
|
||||
stats = stats_service.get_import_statistics(db)
|
||||
@@ -133,7 +133,7 @@ def get_import_statistics(
|
||||
def get_marketplace_import_job(
|
||||
job_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get a single marketplace import job by ID (Admin only)."""
|
||||
job = marketplace_import_job_service.get_import_job_by_id_admin(db, job_id)
|
||||
@@ -147,7 +147,7 @@ def get_import_job_errors(
|
||||
limit: int = Query(50, ge=1, le=100),
|
||||
error_type: str | None = Query(None, description="Filter by error type"),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get import errors for a specific job (Admin only).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user