
    7>h                     h    d dl mZ d dlmZ d dlmZ d dlZ ej                  e      Z	 G d d      Z
y)    )Optional)Session)HTTPExceptionNc            	       |    e Zd ZdZdefdZdededdfdZd	e	d
edede
e	   fdZd	e	dede
e	   fdZd	e	dede	fdZy)BaseServicezFBase service class that provides common functionality for all servicesdbc                     || _         y )N)r   )selfr   s     K/var/www/html/DP/alpha_backend/app/advisor_service/services/base_service.py__init__zBaseService.__init__   s	        error	operationreturnNc                 j    t         j                  d| dt        |              t        dd|       )z
        Handle database errors in a consistent way
        
        Args:
            error: The exception that was raised
            operation: Description of the operation that failed
        zDatabase error during z: i  zDatabase error occurred during )status_codedetail)loggerr   strr   )r
   r   r   s      r   handle_db_errorzBaseService.handle_db_error   s9     	-i[3u:,GH6UV_U`4abbr   model_classfieldvaluec                     	 | j                   j                  |      j                  t        ||      |k(        j	                         S # t
        $ r,}| j                  |d|j                   d|        Y d}~yd}~ww xY w)z<
        Generic method to get an entity by a field
        getting z by N)r   queryfiltergetattrfirst	Exceptionr   __name__)r
   r   r   r   es        r   get_by_fieldzBaseService.get_by_field   su    	R77==-44W[%5PTY5YZ``bb 	R  h{/C/C.DD$PQQ	Rs   AA 	A<"A77A<idc                     	 | j                  |d|      S # t        $ r*}| j                  |d|j                   d       Y d}~yd}~ww xY w)z
        Generic method to get an entity by ID
        
        Args:
            model_class: The SQLAlchemy model class
            id: The ID to look up
            
        Returns:
            The found entity or None
        r$   r   z by IDN)r#   r    r   r!   )r
   r   r$   r"   s       r   	get_by_idzBaseService.get_by_id"   sR    	M$$[$;; 	M  h{/C/C.DF$KLL	Ms    	A AAdatac                 V   	  |di |}| j                   j                  |       | j                   j                          | j                   j                  |       |S # t        $ rC}| j                   j                          | j                  |d|j                          Y d}~yd}~ww xY w)z
        Generic method to create an entity
        
        Args:
            model_class: The SQLAlchemy model class
            data: Dict containing the entity data
            
        Returns:
            The created entity
        z	creating N )r   addcommitrefreshr    rollbackr   r!   )r
   r   r'   entityr"   s        r   createzBaseService.create2   s    	H (4(FGGKKGGNNGGOOF#M 	HGG  i0D0D/E$FGG	Hs   AA 	B(%9B##B()r!   
__module____qualname____doc__r   r   r    r   r   anyr   r#   intr&   dictr/   r)   r   r   r   r      s    P7 	cY 	c3 	c4 	cR RC R RQT RMS Mc Mhsm M H# HT Hc Hr   r   )typingr   sqlalchemy.ormr   fastapir   logging	getLoggerr!   r   r   r)   r   r   <module>r;      s/     " ! 			8	$=H =Hr   