giga_tcg/app/dependencies.py
2025-02-07 20:54:55 -05:00

124 lines
4.2 KiB
Python

from typing import Annotated
from sqlalchemy.orm import Session
from fastapi import Depends, Form
from app.services.box import BoxService
from app.services.tcgplayer import TCGPlayerService
from app.services.pricing import PricingService
from app.services.file import FileService
from app.services.product import ProductService
from app.services.inventory import InventoryService
from app.services.task import TaskService
from app.services.storage import StorageService
from app.db.database import get_db
from app.schemas.file import CreateFileRequest
from app.schemas.box import CreateBoxRequest, UpdateBoxRequest, CreateOpenBoxRequest
# Common type annotation for database dependency
DB = Annotated[Session, Depends(get_db)]
# Base Services (no dependencies besides DB)
def get_file_service(db: DB) -> FileService:
"""FileService with only database dependency"""
return FileService(db)
def get_storage_service(db: DB) -> StorageService:
"""StorageService with only database dependency"""
return StorageService(db)
def get_inventory_service(db: DB) -> InventoryService:
"""InventoryService with only database dependency"""
return InventoryService(db)
# Services with dependencies on other services
def get_tcgplayer_service(
db: DB,
file_service: Annotated[FileService, Depends(get_file_service)]
) -> TCGPlayerService:
"""TCGPlayerService depends on PricingService"""
return TCGPlayerService(db, file_service)
def get_pricing_service(db: DB, file_service: Annotated[FileService, Depends(get_file_service)], tcgplayer_service: Annotated[TCGPlayerService, Depends(get_tcgplayer_service)]) -> PricingService:
"""PricingService with only database dependency"""
return PricingService(db, file_service, tcgplayer_service)
def get_product_service(
db: DB,
file_service: Annotated[FileService, Depends(get_file_service)],
tcgplayer_service: Annotated[TCGPlayerService, Depends(get_tcgplayer_service)],
storage_service: Annotated[StorageService, Depends(get_storage_service)]
) -> ProductService:
"""ProductService with multiple service dependencies"""
return ProductService(db, file_service, tcgplayer_service, storage_service)
def get_box_service(
db: DB,
inventory_service: Annotated[InventoryService, Depends(get_inventory_service)]
) -> BoxService:
"""BoxService depends on InventoryService"""
return BoxService(db, inventory_service)
def get_task_service(
db: DB,
product_service: Annotated[ProductService, Depends(get_product_service)],
pricing_service: Annotated[PricingService, Depends(get_pricing_service)]
) -> TaskService:
"""TaskService depends on ProductService and TCGPlayerService"""
return TaskService(db, product_service, pricing_service)
# Form data dependencies
def get_create_file_metadata(
type: str = Form(...),
source: str = Form(...),
service: str = Form(None),
filename: str = Form(None)
) -> CreateFileRequest:
"""Form dependency for file creation"""
return CreateFileRequest(
type=type,
source=source,
service=service,
filename=filename
)
def get_box_data(
type: str = Form(...),
sku: str = Form(None),
set_code: str = Form(...),
num_cards_expected: int = Form(None)
) -> CreateBoxRequest:
"""Form dependency for box creation"""
return CreateBoxRequest(
type=type,
sku=sku,
set_code=set_code,
num_cards_expected=num_cards_expected
)
def get_box_update_data(
type: str = Form(None),
sku: str = Form(None),
set_code: str = Form(None),
num_cards_expected: int = Form(None)
) -> UpdateBoxRequest:
"""Form dependency for box updates"""
return UpdateBoxRequest(
type=type,
sku=sku,
set_code=set_code,
num_cards_expected=num_cards_expected
)
def get_open_box_data(
product_id: str = Form(...),
file_ids: list[str] = Form(None),
num_cards_actual: int = Form(None),
date_opened: str = Form(None)
) -> CreateOpenBoxRequest:
"""Form dependency for opening boxes"""
return CreateOpenBoxRequest(
product_id=product_id,
file_ids=file_ids,
num_cards_actual=num_cards_actual,
date_opened=date_opened
)