""" 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)}")