
    BIh                     |    d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlm	Z	  ej                  e      Z G d d      Zy)    N)Optional)Session)HTTPException)Userc                       e Zd ZdZd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d	e	de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)BaseRepoz@Base repo class that provides common functionality for all reposNdbc                      || _         || _        y N)r	   current_user)selfr	   r   s      </var/www/html/DP/alpha_backend/app/repositories/base_repo.py__init__zBaseRepo.__init__   s    (    error	operationreturnc                 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BaseRepo.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BaseRepo.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<page	page_sizeorder_byorder_directionc                    |dz
  |z  }| j                   j                  |      }||j                  |      }|dk(  r*|j                  t	        ||      j                               }n)|j                  t	        ||      j                               }|j                  |      j                  |      j                         }	|j                         }
t        j                  |
|z        }|	|fS )aw  
        Generic method to get all entities with pagination
        
        Args:
            model_class: The SQLAlchemy model class
            page: Page number (1-based)
            page_size: Number of items per page
            order_by: Column to order by
            order_direction: 'asc' or 'desc'
            filter_condition: SQLAlchemy filter condition
           desc)r	   r   r    r)   r!   r-   ascoffsetlimitallcountmathceil)r   r   r'   r(   r)   r*   filter_conditionr/   r   entitiestotal_entitiestotal_pagess               r   get_all_paginatedzBaseRepo.get_all_paginated&   s     (i'k*'LL!12Ef$NN7;#A#F#F#HIENN7;#A#E#E#GHE<<'--i8<<>ii :;$$r   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BaseRepo.get_by_idD   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BaseRepo.createT   s    	H (4(FGGKKGGNNGGOOF#M 	HGG  i0D0D/E$FGG	Hs   AA 	B(%9B##B(r   )
created_atr-   N)r$   
__module____qualname____doc__r   r   r#   r   r   anyr   r&   intr9   r<   dictrE   r?   r   r   r   r      s    J)7 )	cY 	c3 	c4 	cR RC R RQT R%S % % %WZ %| %  go  ps  gt %<MS Mc Mhsm M H# HT Hc Hr   r   )r3   typingr   sqlalchemy.ormr   fastapir   loggingapp.common.modelsr   	getLoggerr$   r   r   r?   r   r   <module>rS      s7      " !  "			8	$\H \Hr   