Establishes foundation for migrating baseball simulation from Discord bot to web application using service-oriented architecture pattern. Key components: - FastAPI application structure with dependency injection - Service layer foundation with base classes and container - Comprehensive directory documentation with README files - PostgreSQL containerization with Docker Compose - Testing structure for unit/integration/e2e tests - Migration planning documentation - Rotating log configuration per user requirements Architecture follows Model/Service/Controller pattern to improve testability, maintainability, and scalability over original monolithic Discord app. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
"""
|
|
Base service class for the Model/Service Architecture.
|
|
Provides common functionality and patterns for all services.
|
|
"""
|
|
|
|
import logging
|
|
from abc import ABC
|
|
from typing import Any, Optional
|
|
from sqlmodel import Session
|
|
|
|
|
|
class BaseService(ABC):
|
|
"""
|
|
Base class for all services in the application.
|
|
Provides common functionality like logging and database session management.
|
|
"""
|
|
|
|
def __init__(self, session: Session):
|
|
"""
|
|
Initialize the base service.
|
|
|
|
Args:
|
|
session: SQLModel database session
|
|
"""
|
|
self.session = session
|
|
self.logger = logging.getLogger(f'{__name__}.{self.__class__.__name__}')
|
|
|
|
def _log_operation(self, operation: str, details: Optional[str] = None) -> None:
|
|
"""
|
|
Log a service operation with standardized format.
|
|
|
|
Args:
|
|
operation: The operation being performed
|
|
details: Optional additional details
|
|
"""
|
|
message = f"{self.__class__.__name__}.{operation}"
|
|
if details:
|
|
message += f" - {details}"
|
|
self.logger.info(message)
|
|
|
|
def _log_error(self, operation: str, error: Exception) -> None:
|
|
"""
|
|
Log a service error with standardized format.
|
|
|
|
Args:
|
|
operation: The operation that failed
|
|
error: The exception that occurred
|
|
"""
|
|
self.logger.error(
|
|
f"{self.__class__.__name__}.{operation} - Failed: {str(error)}"
|
|
)
|
|
|
|
def _validate_required_fields(self, data: dict, required_fields: list[str]) -> None:
|
|
"""
|
|
Validate that required fields are present in data.
|
|
|
|
Args:
|
|
data: Dictionary to validate
|
|
required_fields: List of required field names
|
|
|
|
Raises:
|
|
ValueError: If any required field is missing
|
|
"""
|
|
missing_fields = [field for field in required_fields if field not in data or data[field] is None]
|
|
if missing_fields:
|
|
raise ValueError(f"Missing required fields: {', '.join(missing_fields)}") |